forked from vaadin/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.polyfilled.js
65 lines (57 loc) · 2.18 KB
/
index.polyfilled.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {Router, Resolver} from './index.js';
let isUrlAvailable, urlDocument, urlBase, urlAnchor;
Resolver.__ensureUrlAvailableOrPolyfilled = () => {
if (isUrlAvailable === undefined) {
try {
const url = new URL('b', 'http://a');
url.pathname = 'c';
isUrlAvailable = (url.href === 'http://a/c');
} catch (e) {
isUrlAvailable = false;
}
if (!isUrlAvailable) {
// The URL constructor is not available in IE11. Polyfill it by creating
// an HTMLAnchorElement in an in-memory HTML document.
urlDocument = document.implementation.createHTMLDocument('url');
urlBase = urlDocument.createElement('base');
urlDocument.head.appendChild(urlBase);
urlAnchor = urlDocument.createElement('a');
if (!urlAnchor.origin) {
// IE11: HTMLAnchorElement does not have the `origin` property
Object.defineProperty(urlAnchor, 'origin', {
get: () => {
// IE11: on HTTP and HTTPS the default port is not included into
// window.location.origin, so won't include it here either.
const protocol = urlAnchor.protocol;
const port = urlAnchor.port;
const defaultHttp = protocol === 'http:' && port === '80';
const defaultHttps = protocol === 'https:' && port === '443';
const host = (defaultHttp || defaultHttps)
? urlAnchor.hostname
: urlAnchor.host;
return `${protocol}//${host}`;
}
});
// IE11: HTMLAnchorElement pathname does not start with a leading slash
Object.defineProperty(urlAnchor, 'pathname', {
get: () => {
return urlAnchor.href.slice(urlAnchor.origin.length);
}
});
}
}
}
};
Resolver.__createUrl = (path, base) => {
Resolver.__ensureUrlAvailableOrPolyfilled();
if (isUrlAvailable) {
return new URL(path, base);
}
urlBase.href = base;
urlAnchor.href = path.replace(/ /g, '%20');
// IE11: only absolute href setting results in correct part properties
// (`protocol`, `host`, `port`, and such), otherwise they are empty.
urlAnchor.href = urlAnchor.href;
return urlAnchor;
};
export {Router, Resolver};