Skip to content
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 4 commits into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ npm-debug.log
# project specific
coverage/
test/web-platform-tests/
lib/
lib/URL.js
lib/utils.js
lib/url-state-machine.js
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"undef": true,
"unused": true,
"maxlen": 120,
"sub": true,

"globalstrict": true,
"multistr": true
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.npmignore
.gitattributes
.jshint*
.jscsrc
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
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
63 changes: 60 additions & 3 deletions README.md
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 removed lib/.gitkeep
Empty file.
206 changes: 206 additions & 0 deletions lib/URL-impl.js
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing check for failure?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, right.

}

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" });
}
};
11 changes: 11 additions & 0 deletions lib/public-api.js
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;
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
11 changes: 11 additions & 0 deletions scripts/convert-idl.js
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" });
Loading