From 8834089c8694a667cd960759a2d765e716c74487 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Tue, 24 Jan 2017 15:20:37 -0500 Subject: [PATCH] Spec update: change path parsing for non-special URLs Follows https://github.com/whatwg/url/pull/213. --- lib/URL-impl.js | 4 ++++ src/url-state-machine.js | 35 ++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/URL-impl.js b/lib/URL-impl.js index 9e7d67c..1f88121 100644 --- a/lib/URL-impl.js +++ b/lib/URL-impl.js @@ -136,6 +136,10 @@ exports.implementation = class URLImpl { return this._url.path[0]; } + if (this.url.path.length === 0) { + return ""; + } + return "/" + this._url.path.join("/"); } diff --git a/src/url-state-machine.js b/src/url-state-machine.js index 2e24d1c..ce46d69 100644 --- a/src/url-state-machine.js +++ b/src/url-state-machine.js @@ -703,11 +703,12 @@ URLStateMachine.prototype["parse relative"] = function parseRelative(c) { }; URLStateMachine.prototype["parse relative slash"] = function parseRelativeSlash(c) { - if (c === p("/") || (isSpecial(this.url) && c === p("\\"))) { + if (isSpecial(this.url) && (c === p("/") || c === p("\\"))) { if (c === p("\\")) { this.parseError = true; + } else if (c === p("/")) { + this.state = "special authority ignore slashes"; } - this.state = "special authority ignore slashes"; } else { this.url.username = this.base.username; this.url.password = this.base.password; @@ -959,12 +960,26 @@ URLStateMachine.prototype["parse file host"] = function parseFileHost(c, cStr) { }; URLStateMachine.prototype["parse path start"] = function parsePathStart(c) { - if (isSpecial(this.url) && c === p("\\")) { - this.parseError = true; - } - this.state = "path"; - if (c !== p("/") && !(isSpecial(this.url) && c === p("\\"))) { - --this.pointer; + if (isSpecial(this.url)) { + if (c === p("\\")) { + this.parseError = true; + } + this.state = "path"; + + if (c !== p("/") && c !== p("\\")) { + --this.pointer; + } + } else if (!this.stateOverride && c === p("?")) { + this.url.query = ""; + this.state = "query"; + } else if (!this.stateOverride && c === p("#")) { + this.url.fragment = ""; + this.state = "fragment"; + } else if (c !== undefined) { + this.state = "path"; + if (c !== p("/")) { + --this.pointer; + } } return true; @@ -1126,7 +1141,9 @@ function serializeURL(url, excludeFragment) { if (url.cannotBeABaseURL) { output += url.path[0]; } else { - output += "/" + url.path.join("/"); + for (const string of url.path) { + output += "/" + string; + } } if (url.query !== null) {