Skip to content

Commit 940eca5

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

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spe
44

55
## Current Status
66

7-
whatwg-url is currently up to date with the URL spec up to commit [ac6489](https://github.com/whatwg/url/commit/ac6489f9b0cf55b27fd1401ab77bc452c97d1457).
7+
whatwg-url is currently up to date with the URL spec up to commit [cf616f](https://github.com/whatwg/url/commit/cf616f9d3fca44bd5329e992519a4236a39b0cb7).
88

99
## API
1010

@@ -23,7 +23,8 @@ The following methods are exported for use by places like jsdom that need to imp
2323
- [Serialize an integer](https://url.spec.whatwg.org/#serialize-an-integer): `serializeInteger(number)`
2424
- [Origin](https://url.spec.whatwg.org/#concept-url-origin) [Unicode serializer](https://html.spec.whatwg.org/multipage/browsers.html#unicode-serialisation-of-an-origin): `serializeURLToUnicodeOrigin(urlRecord)`
2525
- [Set the username](https://url.spec.whatwg.org/#set-the-username): `setTheUsername(urlRecord, usernameString)`
26-
- [Set the password](https://url.spec.whatwg.org/#set-the-password): `setThePassword(urlRecord, passwordString)`.
26+
- [Set the password](https://url.spec.whatwg.org/#set-the-password): `setThePassword(urlRecord, passwordString)`
27+
- [Cannot have a username/password/port](https://url.spec.whatwg.org/#cannot-have-a-username-password-port): `cannotHaveAUsernamePasswordPort(urlRecord)`
2728

2829
The `stateOverride` parameter is one of the following strings:
2930

lib/URL-impl.js

Lines changed: 3 additions & 3 deletions
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 (usm.cannotHaveAUsernamePasswordPort(this._url)) {
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 (usm.cannotHaveAUsernamePasswordPort(this._url)) {
7070
return;
7171
}
7272

@@ -120,7 +120,7 @@ exports.implementation = class URLImpl {
120120
}
121121

122122
set port(v) {
123-
if (this._url.host === null || this._url.cannotBeABaseURL || this._url.scheme === "file") {
123+
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
124124
return;
125125
}
126126

scripts/get-latest-platform-tests.js

Lines changed: 1 addition & 1 deletion
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 = "3c090ebc321c78a0977e4980c1db707cc6362b93";
17+
const commitHash = "0e6a90f2307694da7cd1e551852d25a75be3be11";
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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,14 @@ function shortenPath(url) {
497497
path.pop();
498498
}
499499

500+
function includesCredentials(url) {
501+
return url.username !== "" || url.password !== "";
502+
}
503+
504+
function cannotHaveAUsernamePasswordPort(url) {
505+
return url.host === null || url.host === "" || url.cannotBeABaseURL || url.scheme === "file";
506+
}
507+
500508
function isNormalizedWindowsDriveLetter(string) {
501509
return /^[A-Za-z]:$/.test(string);
502510
}
@@ -801,7 +809,10 @@ URLStateMachine.prototype["parse authority"] = function parseAuthority(c, cStr)
801809

802810
URLStateMachine.prototype["parse hostname"] =
803811
URLStateMachine.prototype["parse host"] = function parseHostName(c, cStr) {
804-
if (c === p(":") && !this.arrFlag) {
812+
if (this.stateOverride && this.url.scheme === "file") {
813+
--this.pointer;
814+
this.state = "file host";
815+
} else if (c === p(":") && !this.arrFlag) {
805816
if (this.buffer === "") {
806817
this.parseError = true;
807818
return failure;
@@ -824,6 +835,10 @@ URLStateMachine.prototype["parse host"] = function parseHostName(c, cStr) {
824835
if (isSpecial(this.url) && this.buffer === "") {
825836
this.parseError = true;
826837
return failure;
838+
} else if (this.stateOverride && this.buffer === "" &&
839+
(includesCredentials(this.url) || this.url.port !== null)) {
840+
this.parseError = true;
841+
return false;
827842
}
828843

829844
const host = parseURLHost(this.buffer, isSpecial(this.url));
@@ -947,19 +962,31 @@ URLStateMachine.prototype["parse file slash"] = function parseFileSlash(c) {
947962
URLStateMachine.prototype["parse file host"] = function parseFileHost(c, cStr) {
948963
if (isNaN(c) || c === p("/") || c === p("\\") || c === p("?") || c === p("#")) {
949964
--this.pointer;
950-
// don't need to count symbols here since we check ASCII values
951-
if (isWindowsDriveLetterString(this.buffer)) {
965+
if (!this.stateOverride && isWindowsDriveLetterString(this.buffer)) {
952966
this.parseError = true;
953967
this.state = "path";
954968
} else if (this.buffer === "") {
969+
if (this.stateOverride && includesCredentials(this.url)) {
970+
this.parseError = true;
971+
return false;
972+
}
973+
this.url.host = "";
974+
if (this.stateOverride) {
975+
return false;
976+
}
955977
this.state = "path start";
956978
} else {
957-
const host = parseHost(this.buffer);
979+
let host = parseHost(this.buffer);
958980
if (host === failure) {
959981
return failure;
960982
}
961-
if (host !== "localhost") {
962-
this.url.host = host;
983+
if (host === "localhost") {
984+
host = "";
985+
}
986+
this.url.host = host;
987+
988+
if (this.stateOverride) {
989+
return false;
963990
}
964991

965992
this.buffer = "";
@@ -1245,6 +1272,8 @@ module.exports.setThePassword = function (url, password) {
12451272

12461273
module.exports.serializeHost = serializeHost;
12471274

1275+
module.exports.cannotHaveAUsernamePasswordPort = cannotHaveAUsernamePasswordPort;
1276+
12481277
module.exports.serializeInteger = function (integer) {
12491278
return String(integer);
12501279
};

0 commit comments

Comments
 (0)