Skip to content

Commit 89b697a

Browse files
committed
Implement URL.parse()
Closes #277.
1 parent 5115e7e commit 89b697a

5 files changed

Lines changed: 17 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spe
44

55
## Specification conformance
66

7-
whatwg-url is currently up to date with the URL spec up to commit [ffee2e2](https://github.com/whatwg/url/commit/ffee2e21c7034cc72d813d45dd85637e70bbeeec).
7+
whatwg-url is currently up to date with the URL spec up to commit [302c5af](https://github.com/whatwg/url/commit/302c5afb376b7fb1d5943634ad6c3a2e71c37f37).
88

99
For `file:` URLs, whose [origin is left unspecified](https://url.spec.whatwg.org/#concept-url-origin), whatwg-url chooses to use a new opaque origin (which serializes to `"null"`).
1010

lib/URL-impl.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ const URLSearchParams = require("./URLSearchParams");
66
exports.implementation = class URLImpl {
77
// Unlike the spec, we duplicate some code between the constructor and canParse, because we want to give useful error
88
// messages in the constructor that distinguish between the different causes of failure.
9-
constructor(globalObject, constructorArgs) {
10-
const url = constructorArgs[0];
11-
const base = constructorArgs[1];
12-
9+
constructor(globalObject, [url, base]) {
1310
let parsedBase = null;
1411
if (base !== undefined) {
1512
parsedBase = usm.basicURLParse(base);
@@ -33,6 +30,14 @@ exports.implementation = class URLImpl {
3330
this._query._url = this;
3431
}
3532

33+
static parse(globalObject, input, base) {
34+
try {
35+
return new URLImpl(globalObject, [input, base]);
36+
} catch {
37+
return null;
38+
}
39+
}
40+
3641
static canParse(url, base) {
3742
let parsedBase = null;
3843
if (base !== undefined) {

lib/URL.webidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
interface URL {
44
constructor(USVString url, optional USVString base);
55

6+
[WebIDL2JSCallWithGlobal] static URL? parse(USVString url, optional USVString base);
67
static boolean canParse(USVString url, optional USVString base);
78

89
stringifier attribute USVString href;

scripts/get-latest-platform-tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const path = require("path");
1313
// 1. Go to https://github.com/web-platform-tests/wpt/tree/master/url
1414
// 2. Press "y" on your keyboard to get a permalink
1515
// 3. Copy the commit hash
16-
const commitHash = "72b915d4b3754f081ef5899bf6a777efe71b2fc5";
16+
const commitHash = "48178346fe87222856812842fb7af7b01baa1530";
1717

1818
const urlPrefix = `https://raw.githubusercontent.com/web-platform-tests/wpt/${commitHash}/url/`;
1919
const targetDir = path.resolve(__dirname, "..", "test", "web-platform-tests");
@@ -32,6 +32,7 @@ exports.directlyRunnableTests = [
3232
"url-searchparams.any.js",
3333
"url-setters-stripping.any.js",
3434
"url-statics-canparse.any.js",
35+
"url-statics-parse.any.js",
3536
"url-tojson.any.js",
3637
"urlencoded-parser.any.js",
3738
"urlsearchparams-append.any.js",

test/testharness.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ module.exports = {
2929
assert.strictEqual(actual, expected);
3030
},
3131

32+
assert_not_equals(actual, expected) {
33+
assert.notStrictEqual(actual, expected);
34+
},
35+
3236
assert_array_equals(actual, expected) {
3337
assert.deepStrictEqual([...actual], [...expected]);
3438
},

0 commit comments

Comments
 (0)