-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose new public API in light of recent spec changes #36
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"undef": true, | ||
"unused": true, | ||
"maxlen": 120, | ||
"sub": true, | ||
|
||
"globalstrict": true, | ||
"multistr": true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.npmignore | ||
.gitattributes | ||
.jshint* | ||
.jscsrc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
language: node_js | ||
|
||
node_js: iojs | ||
node_js: stable | ||
sudo: false | ||
|
||
|
||
script: | ||
- npm run test | ||
- npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,64 @@ | ||
# whatwg-url | ||
|
||
whatwg-url is a full implementation of the [WHATWG URL](https://url.spec.whatwg.org/) specification. | ||
whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spec.whatwg.org/). It can be used standalone, but it also exposes a lot of the internal algorithms that are useful for integrating a URL parser into a project like [jsdom](https://github.com/tmpvar/jsdom). | ||
|
||
## Current State | ||
## Current Status | ||
|
||
whatwg-url is currently up to date with the URL spec up to commit [e67f9dd](https://github.com/whatwg/url/tree/e67f9dd7a1106d867862d3b5d9f044388f84909e). | ||
whatwg-url is currently up to date with the URL spec up to commit [af76ff](https://github.com/whatwg/url/commit/af76ff3aa15be6310b9408d416854c2370175a8f). | ||
|
||
## API | ||
|
||
### The `URL` Constructor | ||
|
||
The main API is the [`URL`](https://url.spec.whatwg.org/#url) export, which follows the spec's behavior in all ways (including e.g. `USVString` conversion). Most consumers of this library will want to use this. | ||
|
||
### Low-level URL Standard API | ||
|
||
The following methods are exported for use by places like jsdom that need to implement things like [`HTMLHyperlinkElementUtils`](https://html.spec.whatwg.org/#htmlhyperlinkelementutils). They operate on or return an "internal URL" or ["URL record"](https://url.spec.whatwg.org/#concept-url) type. | ||
|
||
- [URL parser](https://url.spec.whatwg.org/#concept-url-parser): `parseURL(input, { baseURL, encodingOverride })` | ||
- [Basic URL parser](https://url.spec.whatwg.org/#concept-basic-url-parser): `basicURLParse(input, { baseURL, encodingOverride, url, stateOverride })` | ||
- [URL serializer](https://url.spec.whatwg.org/#concept-url-serializer): `serializeURL(urlRecord, excludeFragment)` | ||
- [Host serializer](https://url.spec.whatwg.org/#concept-host-serializer): `serializeHost(hostFromURLRecord)` | ||
- [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)`. | ||
|
||
The `stateOverride` parameter is one of the following strings: | ||
|
||
- [`"scheme start"`](https://url.spec.whatwg.org/#scheme-start-state) | ||
- [`"scheme"`](https://url.spec.whatwg.org/#scheme-state) | ||
- [`"no scheme"`](https://url.spec.whatwg.org/#no-scheme-state) | ||
- [`"special relative or authority"`](https://url.spec.whatwg.org/#special-relative-or-authority-state) | ||
- [`"path or authority"`](https://url.spec.whatwg.org/#path-or-authority-state) | ||
- [`"relative"`](https://url.spec.whatwg.org/#relative-state) | ||
- [`"relative slash"`](https://url.spec.whatwg.org/#relative-slash-state) | ||
- [`"special authority slashes"`](https://url.spec.whatwg.org/#special-authority-slashes-state) | ||
- [`"special authority ignore slashes"`](https://url.spec.whatwg.org/#special-authority-ignore-slashes-state) | ||
- [`"authority"`](https://url.spec.whatwg.org/#authority-state) | ||
- [`"host"`](https://url.spec.whatwg.org/#host-state) | ||
- [`"hostname"`](https://url.spec.whatwg.org/#hostname-state) | ||
- [`"port"`](https://url.spec.whatwg.org/#port-state) | ||
- [`"file"`](https://url.spec.whatwg.org/#file-state) | ||
- [`"file slash"`](https://url.spec.whatwg.org/#file-slash-state) | ||
- [`"file host"`](https://url.spec.whatwg.org/#file-host-state) | ||
- [`"path start"`](https://url.spec.whatwg.org/#path-start-state) | ||
- [`"path"`](https://url.spec.whatwg.org/#path-state) | ||
- [`"non-relative path"`](https://url.spec.whatwg.org/#non-relative-path-state) | ||
- [`"query"`](https://url.spec.whatwg.org/#query-state) | ||
- [`"fragment"`](https://url.spec.whatwg.org/#fragment-state) | ||
|
||
The URL record type has the following API: | ||
|
||
- [`scheme`](https://url.spec.whatwg.org/#concept-url-scheme) | ||
- [`username`](https://url.spec.whatwg.org/#concept-url-username) | ||
- [`password`](https://url.spec.whatwg.org/#concept-url-password) | ||
- [`host`](https://url.spec.whatwg.org/#concept-url-host) | ||
- [`port`](https://url.spec.whatwg.org/#concept-url-port) | ||
- [`path`](https://url.spec.whatwg.org/#concept-url-path) (as an array) | ||
- [`query`](https://url.spec.whatwg.org/#concept-url-query) | ||
- [`fragment`](https://url.spec.whatwg.org/#concept-url-fragment) | ||
- [`nonRelative`](https://url.spec.whatwg.org/#non-relative-flag) (as a boolean) | ||
|
||
These properties should be treated with care, as in general changing them will cause the URL record to be in an inconsistent state until the appropriate invocation of `basicURLParse` is used to fix it up. You can see examples of this in the URL Standard, where there are many step sequences like "4. Set context object’s url’s fragment to the empty string. 5. Basic URL parse _input_ with context object’s url as _url_ and fragment state as _state override_." In between those two steps, a URL record is in an unusable state. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
"use strict"; | ||
const usm = require("./url-state-machine"); | ||
|
||
exports.implementation = class URLImpl { | ||
constructor(constructorArgs) { | ||
const url = constructorArgs[0]; | ||
const base = constructorArgs[1]; | ||
|
||
let parsedBase = null; | ||
if (base !== undefined) { | ||
parsedBase = usm.basicURLParse(base); | ||
if (parsedBase.failure) { | ||
throw new TypeError("Invalid base URL"); | ||
} | ||
} | ||
|
||
const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase }); | ||
if (parsedURL.failure) { | ||
throw new TypeError("Invalid URL"); | ||
} | ||
|
||
this._url = parsedURL; | ||
|
||
// TODO: query stuff | ||
} | ||
|
||
static domainToASCII(domain) { | ||
const asciiDomain = usm.parseHost(domain); | ||
if (typeof asciiDomain !== "string") { | ||
return ""; | ||
} | ||
return asciiDomain; | ||
} | ||
|
||
static domainToUnicode(domain) { | ||
const unicodeDomain = usm.parseHost(domain, true); | ||
if (typeof unicodeDomain !== "string") { | ||
return ""; | ||
} | ||
return unicodeDomain; | ||
} | ||
|
||
get href() { | ||
return usm.serializeURL(this._url); | ||
} | ||
|
||
set href(v) { | ||
this._url = usm.basicURLParse(v); | ||
} | ||
|
||
get origin() { | ||
return usm.serializeURLToUnicodeOrigin(this._url); | ||
} | ||
|
||
get protocol() { | ||
return this._url.scheme + ":"; | ||
} | ||
|
||
set protocol(v) { | ||
usm.basicURLParse(v + ":", { url: this._url, stateOverride: "scheme start" }); | ||
} | ||
|
||
get username() { | ||
return this._url.username; | ||
} | ||
|
||
set username(v) { | ||
if (this._url.host === null || this._url.nonRelative) { | ||
return; | ||
} | ||
|
||
usm.setTheUsername(this._url, v); | ||
} | ||
|
||
get password() { | ||
if (this._url.password === null) { | ||
return ""; | ||
} | ||
|
||
return this._url.password; | ||
} | ||
|
||
set password(v) { | ||
if (this._url.host === null || this._url.nonRelative) { | ||
return; | ||
} | ||
|
||
usm.setThePassword(this._url, v); | ||
} | ||
|
||
get host() { | ||
const url = this._url; | ||
|
||
if (url.host === null) { | ||
return ""; | ||
} | ||
|
||
if (url.port === null) { | ||
return usm.serializeHost(url.host); | ||
} | ||
|
||
return usm.serializeHost(url.host) + ":" + usm.serializeInteger(url.port); | ||
} | ||
|
||
set host(v) { | ||
if (this._url.nonRelative) { | ||
return; | ||
} | ||
|
||
usm.basicURLParse(v, { url: this._url, stateOverride: "host" }); | ||
} | ||
|
||
get hostname() { | ||
if (this._url.host === null) { | ||
return ""; | ||
} | ||
|
||
return usm.serializeHost(this._url.host); | ||
} | ||
|
||
set hostname(v) { | ||
if (this._url.nonRelative) { | ||
return; | ||
} | ||
|
||
usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" }); | ||
} | ||
|
||
get port() { | ||
if (this._url.port === null) { | ||
return ""; | ||
} | ||
|
||
return usm.serializeInteger(this._url.port); | ||
} | ||
|
||
set port(v) { | ||
if (this._url.host === null || this._url.nonRelative || this._url.scheme === "file") { | ||
return; | ||
} | ||
|
||
usm.basicURLParse(v, { url: this._url, stateOverride: "port" }); | ||
} | ||
|
||
get pathname() { | ||
if (this._url.nonRelative) { | ||
return this._url.path[0]; | ||
} | ||
|
||
return "/" + this._url.path.join("/"); | ||
} | ||
|
||
set pathname(v) { | ||
if (this._url.nonRelative) { | ||
return; | ||
} | ||
|
||
this._url.path = []; | ||
usm.basicURLParse(v, { url: this._url, stateOverride: "path start" }); | ||
} | ||
|
||
get search() { | ||
if (this._url.query === null || this._url.query === "") { | ||
return ""; | ||
} | ||
|
||
return "?" + this._url.query; | ||
} | ||
|
||
set search(v) { | ||
// TODO: query stuff | ||
|
||
const url = this._url; | ||
|
||
if (v === "") { | ||
url.query = null; | ||
} | ||
|
||
const input = v[0] === "?" ? v.substring(1) : v; | ||
url.query = ""; | ||
usm.basicURLParse(input, { url, stateOverride: "query" }); | ||
} | ||
|
||
get hash() { | ||
if (this._url.fragment === null || this._url.fragment === "") { | ||
return ""; | ||
} | ||
|
||
return "#" + this._url.fragment; | ||
} | ||
|
||
set hash(v) { | ||
if (this._url.scheme === "javascript") { | ||
return; | ||
} | ||
|
||
if (v === "") { | ||
this._url.fragment = null; | ||
return; | ||
} | ||
|
||
const input = v[0] === "#" ? v.substring(1) : v; | ||
this._url.fragment = ""; | ||
usm.basicURLParse(input, { url: this._url, stateOverride: "fragment" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
|
||
exports.URL = require("./URL").interface; | ||
exports.serializeURL = require("./url-state-machine").serializeURL; | ||
exports.serializeURLToUnicodeOrigin = require("./url-state-machine").serializeURLToUnicodeOrigin; | ||
exports.basicURLParse = require("./url-state-machine").basicURLParse; | ||
exports.setTheUsername = require("./url-state-machine").setTheUsername; | ||
exports.setThePassword = require("./url-state-machine").setThePassword; | ||
exports.serializeHost = require("./url-state-machine").serializeHost; | ||
exports.serializeInteger = require("./url-state-machine").serializeInteger; | ||
exports.parseURL = require("./url-state-machine").parseURL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,27 +2,29 @@ | |
"name": "whatwg-url", | ||
"version": "0.6.4", | ||
"description": "An implementation of the WHATWG URL algorithm", | ||
"main": "lib/url.js", | ||
"main": "lib/public-api.js", | ||
"author": "Sebastian Mayr <[email protected]>", | ||
"license": "MIT", | ||
"repository": "jsdom/whatwg-url", | ||
"dependencies": { | ||
"tr46": "~0.0.1" | ||
"tr46": "~0.0.2", | ||
"webidl-conversions": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "~0.3.14", | ||
"jscs": "^1.13.0", | ||
"jshint": "^2.7.0", | ||
"mocha": "^2.2.4", | ||
"recast": "~0.10.29", | ||
"request": "^2.55.0", | ||
"recast": "~0.10.29" | ||
"webidl2js": "^3.0.2" | ||
}, | ||
"scripts": { | ||
"build": "iojs bin/transform.js", | ||
"build": "node scripts/transform.js && node scripts/convert-idl.js", | ||
"coverage": "istanbul cover node_modules/mocha/bin/_mocha", | ||
"lint": "jscs src/ test/ scripts/ && jshint src/ test/ scripts/", | ||
"prepublish": "npm run build", | ||
"pretest": "iojs scripts/get-latest-platform-tests.js && npm run build", | ||
"pretest": "node scripts/get-latest-platform-tests.js && npm run build", | ||
"test": "npm run lint && mocha" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const webidl2js = require("webidl2js"); | ||
|
||
const idlFilePath = path.resolve(__dirname, "../src/URL.idl"); | ||
const outputDir = path.resolve(__dirname, "../lib"); | ||
const implDir = path.resolve(__dirname, "../lib"); | ||
|
||
const src = fs.readFileSync(idlFilePath, { encoding: "utf-8" }); | ||
webidl2js.generate(src, outputDir, implDir, { implSuffix: "-impl" }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing check for failure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
basicURLParse will throw for us it seems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, right.