Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit 0094358

Browse files
committed
update url polyfill to handle hostless URLs
1 parent 7f408d5 commit 0094358

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

src/url-polyfill.js

+12-17
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ function URLPolyfill(url, baseURL) {
44
if (typeof url != 'string')
55
throw new TypeError('URL must be a string');
66
var m = String(url).replace(/^\s+|\s+$/g, "").match(/^([^:\/?#]+:)?(?:\/\/(?:([^:@\/?#]*)(?::([^:@\/?#]*))?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
7-
if (!m) {
8-
throw new RangeError();
9-
}
7+
if (!m)
8+
throw new RangeError('Invalid URL format');
109
var protocol = m[1] || "";
1110
var username = m[2] || "";
1211
var password = m[3] || "";
@@ -18,44 +17,40 @@ function URLPolyfill(url, baseURL) {
1817
var hash = m[9] || "";
1918
if (baseURL !== undefined) {
2019
var base = baseURL instanceof URLPolyfill ? baseURL : new URLPolyfill(baseURL);
21-
var flag = protocol === "" && host === "" && username === "";
22-
if (flag && pathname === "" && search === "") {
20+
var flag = !protocol && !host && !username;
21+
if (flag && !pathname && !search)
2322
search = base.search;
24-
}
25-
if (flag && pathname.charAt(0) !== "/") {
26-
pathname = (pathname !== "" ? (((base.host !== "" || base.username !== "") && base.pathname === "" ? "/" : "") + base.pathname.slice(0, base.pathname.lastIndexOf("/") + 1) + pathname) : base.pathname);
27-
}
23+
if (flag && pathname[0] !== "/")
24+
pathname = (pathname ? (((base.host || base.username) && !base.pathname ? "/" : "") + base.pathname.slice(0, base.pathname.lastIndexOf("/") + 1) + pathname) : base.pathname);
2825
// dot segments removal
2926
var output = [];
3027
pathname.replace(/^(\.\.?(\/|$))+/, "")
3128
.replace(/\/(\.(\/|$))+/g, "/")
3229
.replace(/\/\.\.$/, "/../")
3330
.replace(/\/?[^\/]*/g, function (p) {
34-
if (p === "/..") {
31+
if (p === "/..")
3532
output.pop();
36-
} else {
33+
else
3734
output.push(p);
38-
}
3935
});
40-
pathname = output.join("").replace(/^\//, pathname.charAt(0) === "/" ? "/" : "");
36+
pathname = output.join("").replace(/^\//, pathname[0] === "/" ? "/" : "");
4137
if (flag) {
4238
port = base.port;
4339
hostname = base.hostname;
4440
host = base.host;
4541
password = base.password;
4642
username = base.username;
4743
}
48-
if (protocol === "") {
44+
if (!protocol)
4945
protocol = base.protocol;
50-
}
5146
}
5247

5348
// convert windows file URLs to use /
5449
if (protocol == 'file:')
5550
pathname = pathname.replace(/\\/g, '/');
5651

57-
this.origin = protocol + (protocol !== "" || host !== "" ? "//" : "") + host;
58-
this.href = protocol + (protocol !== "" || host !== "" ? "//" : "") + (username !== "" ? username + (password !== "" ? ":" + password : "") + "@" : "") + host + pathname + search + hash;
52+
this.origin = host ? protocol + (protocol !== "" || host !== "" ? "//" : "") + host : "";
53+
this.href = protocol + (protocol && host || protocol == "file:" ? "//" : "") + (username !== "" ? username + (password !== "" ? ":" + password : "") + "@" : "") + host + pathname + search + hash;
5954
this.protocol = protocol;
6055
this.username = username;
6156
this.password = password;

0 commit comments

Comments
 (0)