Skip to content

Commit ed3f0fb

Browse files
author
vrichard
committed
migrate url.ts to do-not-use folder because YOU SHOULD NOT USE IT!
1 parent 46b9bc2 commit ed3f0fb

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

url.js renamed to do-not-use/url.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ else {
3131
}
3232
/**
3333
* Encodes a path segment.
34-
* RFC 3986 reserves !, ', (, ), and * as well, but we only escape space,
35-
* like the implementations we are polyfilling.
34+
* RFC 3986 reserves !, ', (, ), and * and the implementation pipes the
35+
* output of encodeURIComponent to a hex encoding pass for these special
36+
* characters.
3637
*/
3738
function encodePathSegment(segment) {
38-
return segment.replace(/ /g, function (c) {
39-
return '%' + c.charCodeAt(0).toString(16)
40-
});
39+
return encodeURIComponent(segment).replace(/[!'()*]/g, function (c) {
40+
return '%' + c.charCodeAt(0).toString(16);
41+
});
4142
}
4243
var URLSearchParams = /** @class */ (function () {
4344
function URLSearchParams(init) {
@@ -142,18 +143,11 @@ var URL = /** @class */ (function () {
142143
password: baseParts.password,
143144
hostname: baseParts.hostname,
144145
port: baseParts.port,
146+
path: urlParts.path || baseParts.path,
145147
query: urlParts.query || baseParts.query,
146148
hash: urlParts.hash,
147149
};
148150
}
149-
if (!urlParts.path) {
150-
this.pathname = baseParts.path;
151-
} else if (urlParts.path[0] !== '/') {
152-
// relative path
153-
this.pathname = baseParts.path + '/' + urlParts.path;
154-
} else {
155-
this.pathname = urlParts.path
156-
}
157151
// console.log(URL.parse(base), URL.parse(url), this._parts);
158152
}
159153
URL.init = function () {
@@ -257,16 +251,12 @@ var URL = /** @class */ (function () {
257251
return this._parts.path ? this._parts.path : '/';
258252
},
259253
set: function (value) {
260-
value = value.toString().split('/');
261-
for (var i = 0; i < value.length; i++) {
262-
value[i] = encodePathSegment(value[i])
263-
}
264-
if (value[0]) {
265-
// ensure joined string starts with slash.
266-
value.unshift('')
254+
var chunks = value.toString().split('/').map(encodePathSegment);
255+
if (chunks[0]) {
256+
// ensure joined string starts with slash.
257+
chunks.unshift('');
267258
}
268-
value = value.join('/');
269-
this._parts.path = value;
259+
this._parts.path = chunks.join('/');
270260
},
271261
enumerable: true,
272262
configurable: true
File renamed without changes.

tests/test-url-class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const originalURL = window.URL;
22

3-
import { URL, URLSearchParams } from '../url';
3+
import { URL, URLSearchParams } from '../do-not-use/url';
44

55
const test = () => {
66
let a = new originalURL('?fr=yset_ie_syc_oracle&type=orcl_hpset#page0', 'https://username:[email protected]:80/path');

0 commit comments

Comments
 (0)