Skip to content

Commit f3de3fa

Browse files
committed
Spec update: cleanup API for file and non-special URLs
Follows whatwg/url#224.
1 parent cd44d02 commit f3de3fa

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

lib/URL-impl.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ exports.implementation = class URLImpl {
5454
}
5555

5656
set username(v) {
57-
if (this._url.host === null || this._url.cannotBeABaseURL) {
57+
if (this._url.host === null || this._url.host === "" || this._url.cannotBeABaseURL || this._url.scheme === "file") {
5858
return;
5959
}
6060

@@ -66,7 +66,7 @@ exports.implementation = class URLImpl {
6666
}
6767

6868
set password(v) {
69-
if (this._url.host === null || this._url.cannotBeABaseURL) {
69+
if (this._url.host === null || this._url.host === "" || this._url.cannotBeABaseURL || this._url.scheme === "file") {
7070
return;
7171
}
7272

@@ -92,7 +92,9 @@ exports.implementation = class URLImpl {
9292
return;
9393
}
9494

95-
usm.basicURLParse(v, { url: this._url, stateOverride: "host" });
95+
const hostState = this._url.scheme === "file" ? "file host" : "host";
96+
97+
usm.basicURLParse(v, { url: this._url, stateOverride: hostState });
9698
}
9799

98100
get hostname() {
@@ -108,7 +110,9 @@ exports.implementation = class URLImpl {
108110
return;
109111
}
110112

111-
usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" });
113+
const hostState = this._url.scheme === "file" ? "file host" : "hostname";
114+
115+
usm.basicURLParse(v, { url: this._url, stateOverride: hostState });
112116
}
113117

114118
get port() {
@@ -120,7 +124,7 @@ exports.implementation = class URLImpl {
120124
}
121125

122126
set port(v) {
123-
if (this._url.host === null || this._url.cannotBeABaseURL || this._url.scheme === "file") {
127+
if (this._url.host === null || this._url.host === "" || this._url.cannotBeABaseURL || this._url.scheme === "file") {
124128
return;
125129
}
126130

scripts/get-latest-platform-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const request = require("request");
1414
// 1. Go to https://github.com/w3c/web-platform-tests/tree/master/url
1515
// 2. Press "y" on your keyboard to get a permalink
1616
// 3. Copy the commit hash
17-
const commitHash = "8be87380efb01f5fde485511b8df80fce93090d5";
17+
const commitHash = "512b95eae6cbfdef4c6faf4d167aaf72671f35ea";
1818

1919
const sourceURL = `https://raw.githubusercontent.com/w3c/web-platform-tests/${commitHash}/url/urltestdata.json`;
2020
const setterSourceURL = `https://raw.githubusercontent.com/w3c/web-platform-tests/${commitHash}/url/setters_tests.json`;

src/url-state-machine.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,10 @@ URLStateMachine.prototype["parse host"] = function parseHostName(c, cStr) {
824824
if (isSpecial(this.url) && this.buffer === "") {
825825
this.parseError = true;
826826
return failure;
827+
} else if (this.stateOverride && this.buffer === "" &&
828+
(this.url.username === "" || this.url.password === "" || this.url.port !== null)) {
829+
this.parseError = true;
830+
return false;
827831
}
828832

829833
const host = parseURLHost(this.buffer, isSpecial(this.url));
@@ -948,19 +952,31 @@ URLStateMachine.prototype["parse file slash"] = function parseFileSlash(c) {
948952
URLStateMachine.prototype["parse file host"] = function parseFileHost(c, cStr) {
949953
if (isNaN(c) || c === p("/") || c === p("\\") || c === p("?") || c === p("#")) {
950954
--this.pointer;
951-
// don't need to count symbols here since we check ASCII values
952-
if (isWindowsDriveLetterString(this.buffer)) {
955+
if (!this.stateOverride && isWindowsDriveLetterString(this.buffer)) {
953956
this.parseError = true;
954957
this.state = "path";
955958
} else if (this.buffer === "") {
959+
if (this.stateOverride && (this.url.username !== "" || this.url.password !== "")) {
960+
this.parseError = true;
961+
return false;
962+
}
963+
this.url.host = "";
964+
if (this.stateOverride) {
965+
return false;
966+
}
956967
this.state = "path start";
957968
} else {
958-
const host = parseHost(this.buffer);
969+
let host = parseHost(this.buffer);
959970
if (host === failure) {
960971
return failure;
961972
}
962-
if (host !== "localhost") {
963-
this.url.host = host;
973+
if (host === "localhost") {
974+
host = "";
975+
}
976+
this.url.host = host;
977+
978+
if (this.stateOverride) {
979+
return false;
964980
}
965981

966982
this.buffer = "";

0 commit comments

Comments
 (0)