Location-impl.js
5.92 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"use strict";
const whatwgURL = require("whatwg-url");
const DOMException = require("domexception/webidl2js-wrapper");
const { documentBaseURL, parseURLToResultingURLRecord } = require("../helpers/document-base-url");
const { navigate } = require("./navigation");
// Not implemented: use of entry settings object's API base URL in href setter, assign, and replace. Instead we just
// use the document base URL. The difference matters in the case of cross-frame calls.
exports.implementation = class LocationImpl {
constructor(globalObject, args, privateData) {
this._relevantDocument = privateData.relevantDocument;
this.url = null;
this._globalObject = globalObject;
}
get _url() {
return this._relevantDocument._URL;
}
_locationObjectSetterNavigate(url) {
// Not implemented: extra steps here to determine replacement flag.
return this._locationObjectNavigate(url);
}
_locationObjectNavigate(url, { replacement = false } = {}) {
// Not implemented: the setup for calling navigate, which doesn't apply to our stub navigate anyway.
navigate(this._relevantDocument._defaultView, url, { replacement, exceptionsEnabled: true });
}
toString() {
return this.href;
}
get href() {
return whatwgURL.serializeURL(this._url);
}
set href(v) {
const newURL = whatwgURL.parseURL(v, { baseURL: documentBaseURL(this._relevantDocument) });
if (newURL === null) {
throw new TypeError(`Could not parse "${v}" as a URL`);
}
this._locationObjectSetterNavigate(newURL);
}
get origin() {
return whatwgURL.serializeURLOrigin(this._url);
}
get protocol() {
return this._url.scheme + ":";
}
set protocol(v) {
const copyURL = { ...this._url };
const possibleFailure = whatwgURL.basicURLParse(v + ":", { url: copyURL, stateOverride: "scheme start" });
if (possibleFailure === null) {
throw new TypeError(`Could not parse the URL after setting the procol to "${v}"`);
}
if (copyURL.scheme !== "http" && copyURL.scheme !== "https") {
return;
}
this._locationObjectSetterNavigate(copyURL);
}
get host() {
const url = this._url;
if (url.host === null) {
return "";
}
if (url.port === null) {
return whatwgURL.serializeHost(url.host);
}
return whatwgURL.serializeHost(url.host) + ":" + whatwgURL.serializeInteger(url.port);
}
set host(v) {
const copyURL = { ...this._url };
if (copyURL.cannotBeABaseURL) {
return;
}
whatwgURL.basicURLParse(v, { url: copyURL, stateOverride: "host" });
this._locationObjectSetterNavigate(copyURL);
}
get hostname() {
if (this._url.host === null) {
return "";
}
return whatwgURL.serializeHost(this._url.host);
}
set hostname(v) {
const copyURL = { ...this._url };
if (copyURL.cannotBeABaseURL) {
return;
}
whatwgURL.basicURLParse(v, { url: copyURL, stateOverride: "hostname" });
this._locationObjectSetterNavigate(copyURL);
}
get port() {
if (this._url.port === null) {
return "";
}
return whatwgURL.serializeInteger(this._url.port);
}
set port(v) {
const copyURL = { ...this._url };
if (copyURL.host === null || copyURL.cannotBeABaseURL || copyURL.scheme === "file") {
return;
}
whatwgURL.basicURLParse(v, { url: copyURL, stateOverride: "port" });
this._locationObjectSetterNavigate(copyURL);
}
get pathname() {
const url = this._url;
if (url.cannotBeABaseURL) {
return url.path[0];
}
return "/" + url.path.join("/");
}
set pathname(v) {
const copyURL = { ...this._url };
if (copyURL.cannotBeABaseURL) {
return;
}
copyURL.path = [];
whatwgURL.basicURLParse(v, { url: copyURL, stateOverride: "path start" });
this._locationObjectSetterNavigate(copyURL);
}
get search() {
if (this._url.query === null || this._url.query === "") {
return "";
}
return "?" + this._url.query;
}
set search(v) {
const copyURL = { ...this._url };
if (v === "") {
copyURL.query = null;
} else {
const input = v[0] === "?" ? v.substring(1) : v;
copyURL.query = "";
whatwgURL.basicURLParse(input, {
url: copyURL,
stateOverride: "query",
encodingOverride: this._relevantDocument.charset
});
}
this._locationObjectSetterNavigate(copyURL);
}
get hash() {
if (this._url.fragment === null || this._url.fragment === "") {
return "";
}
return "#" + this._url.fragment;
}
set hash(v) {
const copyURL = { ...this._url };
if (copyURL.scheme === "javascript") {
return;
}
if (v === "") {
copyURL.fragment = null;
} else {
const input = v[0] === "#" ? v.substring(1) : v;
copyURL.fragment = "";
whatwgURL.basicURLParse(input, { url: copyURL, stateOverride: "fragment" });
}
this._locationObjectSetterNavigate(copyURL);
}
assign(url) {
// Should be entry settings object; oh well
const parsedURL = parseURLToResultingURLRecord(url, this._relevantDocument);
if (parsedURL === null) {
throw DOMException.create(this._globalObject, [
`Could not resolve the given string "${url}" relative to the base URL "${this._relevantDocument.URL}"`,
"SyntaxError"
]);
}
this._locationObjectNavigate(parsedURL);
}
replace(url) {
// Should be entry settings object; oh well
const parsedURL = parseURLToResultingURLRecord(url, this._relevantDocument);
if (parsedURL === null) {
throw DOMException.create(this._globalObject, [
`Could not resolve the given string "${url}" relative to the base URL "${this._relevantDocument.URL}"`,
"SyntaxError"
]);
}
this._locationObjectNavigate(parsedURL, { replacement: true });
}
reload() {
const flags = { replace: true, reloadTriggered: true, exceptionsEnabled: true };
navigate(this._relevantDocument._defaultView, this._url, flags);
}
};