/
URLUtils-impl.js
76 lines (64 loc) · 1.81 KB
/
URLUtils-impl.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
66
67
68
69
70
71
72
73
74
75
76
import * as privateMethods from '../private-methods.js';
// Reimplementing URLUtils mixin in terms of URL, somewhat ironically.
var privates = new WeakMap();
export default class URLUtilsImpl {
get href() {
const url = p(this).url;
return url === null ? p(this).input : url.href;
}
set href(v) {
// Working around the exception in https://url.spec.whatwg.org/#dfnReturnLink-12
try {
p(this).url.href = v;
} catch (e) {}
runPreUpdateSteps(this, v);
}
get origin() {
return p(this).url.origin;
}
}
var simpleProps = ['protocol', 'username', 'password', 'host', 'hostname', 'port', 'pathname', 'search',
'searchParams', 'hash'];
for (const prop of simpleProps) {
Object.defineProperty(URLUtilsImpl.prototype, prop, {
enumerable: true,
configurable: true,
get() {
return p(this).url[prop];
},
set(v) {
p(this).url[prop] = v;
runPreUpdateSteps(this);
}
});
}
// The URLUtils mixin provides a "set the input" operation which can be called on any mixin target instance.
export function setTheInput(target, input, url) {
if (url !== undefined) {
p(target).url = url;
p(target).input = input;
} else {
p(target).url = null;
if (input === null) {
p(target).input = '';
} else {
p(target).input = input;
p(target).url = new URL(input, privateMethods.call(target, 'getTheBase'));
}
}
}
function runPreUpdateSteps(obj, value = p(obj).url.href) {
const updateSteps = privateMethods.get(obj, 'updateSteps');
if (updateSteps) {
updateSteps.call(obj, value);
}
}
function p(obj) {
ensureInitialized(obj);
return privates.get(obj);
}
function ensureInitialized(obj) {
if (!privates.has(obj)) {
privates.set(obj, { url: new URL(privateMethods.call(obj, 'getTheBase')) });
}
}