This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathURL-shim.js
647 lines (614 loc) · 19.8 KB
/
URL-shim.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*******************************************************************************
* @license
* Copyright (c) 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (https://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (https://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env browser, amd*/
/*global StopIteration*/
// URL Shim -- see https://url.spec.whatwg.org/ and https://dvcs.w3.org/hg/url/raw-file/tip/Overview.html
/*eslint-disable no-extra-parens */
(function() {
try {
var testURL;
if (typeof self.URL === "function" && self.URL.length !== 0 &&
(testURL = new self.URL("https://www.w3.org?q")).protocol === "https:" && testURL.query) {
return;
}
} catch (e) {}
//[1]scheme, [2]authority, [3]path, [4]query, [5]fragment
var _URI_RE = /^(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?$/;
//[ userinfo "@" ] host [ ":" port ]
var _AUTHORITY_RE = /^(?:(.*)@)?(\[[^\]]*\]|[^:]*)(?::(.*))?$/;
var _NO_WS_RE = /^\S*$/;
var _SCHEME_RE = /^([a-zA-Z](?:[a-zA-Z0-9+-.])*)$/;
var _PORT_RE = /^\d*$/;
var _HOST_RE = /^(\[[^\]\/?#\s]*\]|[^:\/?#\s]*)$/;
var _HOSTPORT_RE = /^(\[[^\]\/?#\s]*\]|[^:\/?#\s]*)(?::(\d*))?$/;
var _PATH_RE = /^([^?#\s]*)$/;
var _QUERY_RE = /^([^\s]*)$/;
var _FRAGMENT_RE = _NO_WS_RE;
var _USERNAME_PASSWORD_RE = /([^:]*):?(.*)/;
var STOP_ITERATION = typeof StopIteration !== "undefined" ? StopIteration : new Error("Stop Iteration");
var DEFAULT_PORTS = {
"ftp:": "21",
"gopher:": "70",
"http:": "80",
"https:": "443",
"ws:": "80",
"wss:": "443"
};
function _checkString(txt) {
if (typeof txt !== "string") {
throw new TypeError();
}
}
function _parseQuery(query) {
return query ? query.split("&") : [];
}
function _stringifyQuery(pairs) {
if (pairs.length === 0) {
return "";
}
return pairs.join("&");
}
function _parsePair(pair) {
var parsed = /([^=]*)(?:=?)(.*)/.exec(pair);
var key = parsed[1] ? decodeURIComponent(parsed[1]) : "";
var value = parsed[2] ? decodeURIComponent(parsed[2]) : "";
return [key, value];
}
function _stringifyPair(entry) {
var pair = encodeURIComponent(entry[0]);
if (entry[1]) {
pair += "=" + encodeURIComponent(entry[1]);
}
return pair;
}
function _createMapIterator(url, kind) {
var query = "";
var pairs = [];
var index = 0;
return {
next: function() {
if (query !== url.query) {
query = url.query;
pairs = _parseQuery(query);
}
if (index < pairs.length) {
var entry = _parsePair(pairs[index++]);
switch (kind) {
case "keys":
return entry[0];
case "values":
return entry[1];
case "keys+values":
return [entry[0], entry[1]];
default:
throw new TypeError();
}
}
throw STOP_ITERATION;
}
};
}
// See https://url.spec.whatwg.org/#interface-urlquery
function URLQuery(url) {
Object.defineProperty(this, "_url", {
get: function() {
return url._url;
}
});
}
Object.defineProperties(URLQuery.prototype, {
get: {
value: function(key) {
_checkString(key);
var result;
var pairs = _parseQuery(this._url.query);
pairs.some(function(pair) {
var entry = _parsePair(pair);
if (entry[0] === key) {
result = entry[1];
return true;
}
});
return result;
},
enumerable: true
},
set: {
value: function(key, value) {
_checkString(key);
_checkString(value);
var pairs = _parseQuery(this._url.query);
var found = pairs.some(function(pair, i) {
var entry = _parsePair(pair);
if (entry[0] === key) {
entry[1] = value;
pairs[i] = _stringifyPair(entry);
return true;
}
});
if (!found) {
pairs.push(_stringifyPair([key, value]));
}
this._url.query = _stringifyQuery(pairs);
},
enumerable: true
},
has: {
value: function(key) {
_checkString(key);
var pairs = _parseQuery(this._url.query);
return pairs.some(function(pair) {
var entry = _parsePair(pair);
if (entry[0] === key) {
return true;
}
});
},
enumerable: true
},
"delete": {
value: function(key) {
_checkString(key);
var pairs = _parseQuery(this._url.query);
var filtered = pairs.filter(function(pair) {
var entry = _parsePair(pair);
return entry[0] !== key;
});
if (filtered.length !== pairs.length) {
this._url.query = _stringifyQuery(filtered);
return true;
}
return false;
},
enumerable: true
},
clear: {
value: function() {
this._url.query = "";
},
enumerable: true
},
forEach: {
value: function(callback, thisArg) {
if (typeof callback !== "function") {
throw new TypeError();
}
var iterator = _createMapIterator(this._url, "keys+values");
try {
while (true) {
var entry = iterator.next();
callback.call(thisArg, entry[1], entry[0], this);
}
} catch (e) {
if (e !== STOP_ITERATION) {
throw e;
}
}
},
enumerable: true
},
keys: {
value: function() {
return _createMapIterator(this._url, "keys");
},
enumerable: true
},
values: {
value: function() {
return _createMapIterator(this._url, "values");
},
enumerable: true
},
items: {
value: function() {
return _createMapIterator(this._url, "keys+values");
}
},
size: {
get: function() {
return _parseQuery(this._url.query).length;
},
enumerable: true
},
getAll: {
value: function(key) {
_checkString(key);
var result = [];
var pairs = _parseQuery(this._url.query);
pairs.forEach(function(pair) {
var entry = _parsePair(pair);
if (entry[0] === key) {
result.push(entry[1]);
}
});
return result;
},
enumerable: true
},
append: {
value: function(key, value) {
_checkString(key);
_checkString(value);
var pairs = _parseQuery(this._url.query);
pairs.push(_stringifyPair([key, value]));
this._url.query = _stringifyQuery(pairs);
},
enumerable: true
}
});
function _makeAbsoluteURL(url, base) {
if (!url.scheme && base) {
url.scheme = base.scheme;
if (!url.host && base.host) {
url.userinfo = base.userinfo;
url.host = base.host;
url.port = base.port;
url.pathRelative = true;
}
}
if (url.pathRelative) {
if (!url.path) {
url.path = base.path;
} else if (url.path[0] !== "/") {
var basePath = /^(.*\/)[^\/]*$/.exec(base.path)[1] || "/";
url.path = basePath + url.path;
}
}
}
function _normalizeScheme(scheme) {
return scheme.toLowerCase();
}
function _normalizePort(port) {
return port ? (/[1-9]\d*$/).exec(port)[0] : "";
}
function _normalizePath(path) {
var result = [];
path.split("/").forEach(function(segment) {
if (segment === "..") {
if (result.length > 1) {
result.pop();
}
} else if (segment !== ".") {
result.push(segment);
}
});
return result.join("/");
}
function _normalizeURL(url) {
if (url.scheme) {
url.scheme = _normalizeScheme(url.scheme);
}
if (url.port) {
url.port = _normalizePort(url.port);
}
if (url.host && url.path) {
url.path = _normalizePath(url.path);
}
}
function _encodeWhitespace(text) {
return text.replace(/\s/g, function(c) {
return "%" + c.charCodeAt(0).toString(16);
});
}
function _parseURL(input, base) {
if (typeof input !== "string") {
throw new TypeError();
}
input = _encodeWhitespace(input);
var parsedURI = _URI_RE.exec(input);
if (!parsedURI) {
return null;
}
var url = {};
url.scheme = parsedURI[1] || "";
if (url.scheme && !_SCHEME_RE.test(url.scheme)) {
return null;
}
var authority = parsedURI[2];
if (authority) {
var parsedAuthority = _AUTHORITY_RE.exec(authority);
url.userinfo = parsedAuthority[1];
url.host = parsedAuthority[2];
url.port = parsedAuthority[3];
if (url.port && !_PORT_RE.test(url.port)) {
return null;
}
}
url.path = parsedURI[3];
url.query = parsedURI[4];
url.fragment = parsedURI[5];
_makeAbsoluteURL(url, base);
_normalizeURL(url);
return url;
}
function _serialize(url) {
var result = (url.scheme ? url.scheme + ":" : "");
if (url.host) {
result += "//";
if (url.userinfo) {
result += url.userinfo + "@";
}
result += url.host;
if (url.port) {
result += ":" + url.port;
}
}
result += url.path;
if (url.query) {
result += "?" + url.query;
}
if (url.fragment) {
result += "#" + url.fragment;
}
return result;
}
// See https://url.spec.whatwg.org/#api
function URL(input, base) {
var baseURL;
if (base) {
base = base.href || base;
baseURL = _parseURL(base);
if (!baseURL || !baseURL.scheme) {
throw new SyntaxError();
}
Object.defineProperty(this, "_baseURL", {
value: baseURL
});
}
var url = _parseURL(input, baseURL);
if (!url) {
throw new SyntaxError();
}
Object.defineProperty(this, "_input", {
value: input,
writable: true
});
Object.defineProperty(this, "_url", {
value: url,
writable: true
});
var query = new URLQuery(this);
Object.defineProperty(this, "query", {
get: function() {
return this._url ? query : null;
},
enumerable: true
});
}
Object.defineProperties(URL.prototype, {
toString: {
value: function() {
return this.href;
}
},
href: {
get: function() {
return this._url ? _serialize(this._url) : this._input;
},
set: function(value) {
_checkString(value);
this._input = value;
this._url = _parseURL(this._input, this._baseURL);
},
enumerable: true
},
origin: {
get: function() {
return (this._url && this._url.host ? this.protocol + "//" + this.host : "");
},
enumerable: true
},
protocol: {
get: function() {
return this._url ? this._url.scheme + ":" : ":";
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
var scheme = (value.slice(-1) === ":") ? value.substring(0, value.length - 1) : value;
if (scheme === "" || _SCHEME_RE.test(scheme)) {
this._url.scheme = _normalizeScheme(scheme);
}
},
enumerable: true
},
_userinfo: { // note: not part of spec so not enumerable
get: function() {
return this._url ? this._url.userinfo : "";
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
this._url.userinfo = value;
}
},
username: {
get: function() {
if (!this._url) {
return "";
}
var parsed = _USERNAME_PASSWORD_RE.exec(this._userinfo);
var username = decodeURIComponent(parsed[1] || "");
return username;
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
var parsed = _USERNAME_PASSWORD_RE.exec(this._userinfo);
var userpass = [encodeURIComponent(value || "")];
if (parsed[2]) {
userpass.push(parsed[2]);
}
this._userinfo = userpass.join(":");
},
enumerable: true
},
password: {
get: function() {
if (!this._url) {
return "";
}
var parsed = _USERNAME_PASSWORD_RE.exec(this._userinfo);
var password = decodeURIComponent(parsed[2] || "");
return password;
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
var parsed = _USERNAME_PASSWORD_RE.exec(this._userinfo);
var userpass = [parsed[1] || ""];
if (value) {
userpass.push(encodeURIComponent(value));
}
this._userinfo = userpass.join(":");
},
enumerable: true
},
host: {
get: function() {
var result = "";
if (this._url && this._url.host) {
result += this._url.host;
if (this._url.port) {
result += ":" + this._url.port;
}
}
return result;
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
var result = _HOSTPORT_RE.exec(value);
if (result) {
this._url.host = result[1];
this._url.port = _normalizePort(result[2]);
}
},
enumerable: true
},
hostname: {
get: function() {
return this._url ? this._url.host : "";
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
var result = _HOST_RE.exec(value);
if (result) {
this._url.host = value;
}
},
enumerable: true
},
port: {
get: function() {
var port = this._url ? this._url.port || "" : "";
if (port && port === DEFAULT_PORTS[this.protocol]) {
port = "";
}
return port;
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
var result = _PORT_RE.exec(value);
if (result) {
this._url.port = _normalizePort(value);
}
},
enumerable: true
},
pathname: {
get: function() {
return this._url ? this._url.path : "";
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
var result = _PATH_RE.exec(value);
if (result) {
if (this._url.host && value && value[0] !== "/") {
value = "/" + value;
}
this._url.path = value ? _normalizePath(value) : "";
}
},
enumerable: true
},
search: {
get: function() {
return (this._url && this._url.query ? "?" + this._url.query : "");
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
if (value && value[0] === "?") {
value = value.substring(1);
}
var result = _QUERY_RE.exec(value);
if (result) {
this._url.query = value;
}
},
enumerable: true
},
hash: {
get: function() {
return (this._url && this._url.fragment ? "#" + this._url.fragment : "");
},
set: function(value) {
_checkString(value);
if (!this._url) {
return;
}
if (value && value[0] === "#") {
value = value.substring(1);
}
var result = _FRAGMENT_RE.exec(value);
if (result) {
this._url.fragment = value;
}
},
enumerable: true
}
});
var _URL = self.URL || self.webkitURL;
if (_URL && _URL.createObjectURL) {
Object.defineProperty(URL, "createObjectURL", {
value: _URL.createObjectURL.bind(_URL),
enumerable: false
});
Object.defineProperty(URL, "revokeObjectURL", {
value: _URL.revokeObjectURL.bind(_URL),
enumerable: false
});
}
self.URL = URL;
}());