Skip to content

Commit

Permalink
Spec update: cleanup API for file and non-special URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Feb 8, 2017
1 parent c85a57c commit 940eca5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spe

## Current Status

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

## API

Expand All @@ -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)`
- [Cannot have a username/password/port](https://url.spec.whatwg.org/#cannot-have-a-username-password-port): `cannotHaveAUsernamePasswordPort(urlRecord)`

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

Expand Down
6 changes: 3 additions & 3 deletions lib/URL-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ exports.implementation = class URLImpl {
}

set username(v) {
if (this._url.host === null || this._url.cannotBeABaseURL) {
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
return;
}

Expand All @@ -66,7 +66,7 @@ exports.implementation = class URLImpl {
}

set password(v) {
if (this._url.host === null || this._url.cannotBeABaseURL) {
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
return;
}

Expand Down Expand Up @@ -120,7 +120,7 @@ exports.implementation = class URLImpl {
}

set port(v) {
if (this._url.host === null || this._url.cannotBeABaseURL || this._url.scheme === "file") {
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
return;
}

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 @@ -14,7 +14,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 = "3c090ebc321c78a0977e4980c1db707cc6362b93";
const commitHash = "0e6a90f2307694da7cd1e551852d25a75be3be11";

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: 35 additions & 6 deletions src/url-state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,14 @@ function shortenPath(url) {
path.pop();
}

function includesCredentials(url) {
return url.username !== "" || url.password !== "";
}

function cannotHaveAUsernamePasswordPort(url) {
return url.host === null || url.host === "" || url.cannotBeABaseURL || url.scheme === "file";
}

function isNormalizedWindowsDriveLetter(string) {
return /^[A-Za-z]:$/.test(string);
}
Expand Down Expand Up @@ -801,7 +809,10 @@ 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 (this.stateOverride && this.url.scheme === "file") {
--this.pointer;
this.state = "file host";
} else if (c === p(":") && !this.arrFlag) {
if (this.buffer === "") {
this.parseError = true;
return failure;
Expand All @@ -824,6 +835,10 @@ URLStateMachine.prototype["parse host"] = function parseHostName(c, cStr) {
if (isSpecial(this.url) && this.buffer === "") {
this.parseError = true;
return failure;
} else if (this.stateOverride && this.buffer === "" &&
(includesCredentials(this.url) || this.url.port !== null)) {
this.parseError = true;
return false;
}

const host = parseURLHost(this.buffer, isSpecial(this.url));
Expand Down Expand Up @@ -947,19 +962,31 @@ URLStateMachine.prototype["parse file slash"] = function parseFileSlash(c) {
URLStateMachine.prototype["parse file host"] = function parseFileHost(c, cStr) {
if (isNaN(c) || c === p("/") || c === p("\\") || c === p("?") || c === p("#")) {
--this.pointer;
// don't need to count symbols here since we check ASCII values
if (isWindowsDriveLetterString(this.buffer)) {
if (!this.stateOverride && isWindowsDriveLetterString(this.buffer)) {
this.parseError = true;
this.state = "path";
} else if (this.buffer === "") {
if (this.stateOverride && includesCredentials(this.url)) {
this.parseError = true;
return false;
}
this.url.host = "";
if (this.stateOverride) {
return false;
}
this.state = "path start";
} else {
const host = parseHost(this.buffer);
let host = parseHost(this.buffer);
if (host === failure) {
return failure;
}
if (host !== "localhost") {
this.url.host = host;
if (host === "localhost") {
host = "";
}
this.url.host = host;

if (this.stateOverride) {
return false;
}

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

module.exports.serializeHost = serializeHost;

module.exports.cannotHaveAUsernamePasswordPort = cannotHaveAUsernamePasswordPort;

module.exports.serializeInteger = function (integer) {
return String(integer);
};
Expand Down

0 comments on commit 940eca5

Please sign in to comment.