Skip to content

Commit

Permalink
Spec update: treat URLs without a special scheme differently
Browse files Browse the repository at this point in the history
This matches whatwg/url#185.
  • Loading branch information
domenic committed Dec 29, 2016
1 parent 9096654 commit ecb808e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ The following methods are exported for use by places like jsdom that need to imp
- [Serialize an integer](https://url.spec.whatwg.org/#serialize-an-integer): `serializeInteger(number)`
- [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)`
- [Set the username](https://url.spec.whatwg.org/#set-the-username): `setTheUsername(urlRecord, usernameString)`
- [Set the password](https://url.spec.whatwg.org/#set-the-password): `setThePassword(urlRecord, passwordString)`.
- [Set the password](https://url.spec.whatwg.org/#set-the-password): `setThePassword(urlRecord, passwordString)`
- [Is special](https://url.spec.whatwg.org/#is-special): `isSpecial(urlRecord)`

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

Expand Down
4 changes: 4 additions & 0 deletions lib/URL-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ exports.implementation = class URLImpl {
return this._url.path[0];
}

if (!usm.isSpecial(this._url) && this._url.path.length === 0) {
return "";
}

return "/" + this._url.path.join("/");
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/get-latest-platform-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const request = require("request");
// 1. Go to https://github.com/w3c/web-platform-tests/tree/master/url
// 2. Press "y" on your keyboard to get a permalink
// 3. Copy the commit hash
const commitHash = "5be497b5f5a7036e26dc14739aa8d42f643cf94f";
const commitHash = "2c87b7d14b07b9383b837278e475d202969f6b64";

const sourceURL = `https://raw.githubusercontent.com/w3c/web-platform-tests/${commitHash}/url/urltestdata.json`;
const setterSourceURL = `https://raw.githubusercontent.com/w3c/web-platform-tests/${commitHash}/url/setters_tests.json`;
Expand Down
41 changes: 31 additions & 10 deletions src/url-state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,23 @@ function parseHost(input, isUnicode) {
return isUnicode ? tr46.toUnicode(asciiDomain, false).domain : asciiDomain;
}

function parseURLHost(input, isSpecial) {
if (isSpecial) {
if (input === "") {
return failure;
}

return parseHost(input);
}

let output = "";
const decoded = punycode.ucs2.decode(input);
for (let i = 0; i < decoded.length; ++i) {
output += encodeChar(decoded[i], isSimpleEncode);
}
return output;
}

function findLongestZeroSequence(arr) {
let maxIdx = null;
let maxLen = 1; // only find elements > 1
Expand Down Expand Up @@ -750,11 +767,7 @@ URLStateMachine.prototype["parse authority"] = function parseAuthority(c, cStr)
URLStateMachine.prototype["parse hostname"] =
URLStateMachine.prototype["parse host"] = function parseHostName(c, cStr) {
if (c === p(":") && !this.arrFlag) {
if (isSpecial(this.url) && this.buffer === "") {
return failure;
}

const host = parseHost(this.buffer);
const host = parseURLHost(this.buffer, isSpecial(this.url));
if (host === failure) {
return failure;
}
Expand All @@ -768,11 +781,8 @@ URLStateMachine.prototype["parse host"] = function parseHostName(c, cStr) {
} else if (isNaN(c) || c === p("/") || c === p("?") || c === p("#") ||
(isSpecial(this.url) && c === p("\\"))) {
--this.pointer;
if (isSpecial(this.url) && this.buffer === "") {
return failure;
}

const host = parseHost(this.buffer);
const host = parseURLHost(this.buffer, isSpecial(this.url));
if (host === failure) {
return failure;
}
Expand Down Expand Up @@ -1078,7 +1088,13 @@ function serializeURL(url, excludeFragment) {
if (url.username !== "" || url.password !== null) {
output += "@";
}
output += serializeHost(url.host);

if (isSpecial(url)) {
output += serializeHost(url.host);
} else {
output += url.host;
}

if (url.port !== null) {
output += ":" + url.port;
}
Expand All @@ -1089,6 +1105,9 @@ function serializeURL(url, excludeFragment) {
if (url.cannotBeABaseURL) {
output += url.path[0];
} else {
if (!isSpecial(url) && url.path.length === 0) {
return output;
}
output += "/" + url.path.join("/");
}

Expand Down Expand Up @@ -1196,3 +1215,5 @@ module.exports.parseURL = function (input, options) {
// We don't handle blobs, so this just delegates:
return module.exports.basicURLParse(input, { baseURL: options.baseURL, encodingOverride: options.encodingOverride });
};

module.exports.isSpecial = isSpecial;

0 comments on commit ecb808e

Please sign in to comment.