From 89b697a74aebd9b2c8644bc87f69b85c0b2b491f Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Wed, 4 Dec 2024 10:21:37 +0900 Subject: [PATCH] Implement URL.parse() Closes #277. --- README.md | 2 +- lib/URL-impl.js | 13 +++++++++---- lib/URL.webidl | 1 + scripts/get-latest-platform-tests.js | 3 ++- test/testharness.js | 4 ++++ 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e51d71a..1e1bcd9 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spe ## Specification conformance -whatwg-url is currently up to date with the URL spec up to commit [ffee2e2](https://github.com/whatwg/url/commit/ffee2e21c7034cc72d813d45dd85637e70bbeeec). +whatwg-url is currently up to date with the URL spec up to commit [302c5af](https://github.com/whatwg/url/commit/302c5afb376b7fb1d5943634ad6c3a2e71c37f37). 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"`). diff --git a/lib/URL-impl.js b/lib/URL-impl.js index eb66cae..f1138eb 100644 --- a/lib/URL-impl.js +++ b/lib/URL-impl.js @@ -6,10 +6,7 @@ const URLSearchParams = require("./URLSearchParams"); exports.implementation = class URLImpl { // Unlike the spec, we duplicate some code between the constructor and canParse, because we want to give useful error // messages in the constructor that distinguish between the different causes of failure. - constructor(globalObject, constructorArgs) { - const url = constructorArgs[0]; - const base = constructorArgs[1]; - + constructor(globalObject, [url, base]) { let parsedBase = null; if (base !== undefined) { parsedBase = usm.basicURLParse(base); @@ -33,6 +30,14 @@ exports.implementation = class URLImpl { this._query._url = this; } + static parse(globalObject, input, base) { + try { + return new URLImpl(globalObject, [input, base]); + } catch { + return null; + } + } + static canParse(url, base) { let parsedBase = null; if (base !== undefined) { diff --git a/lib/URL.webidl b/lib/URL.webidl index 3a376a3..45446bc 100644 --- a/lib/URL.webidl +++ b/lib/URL.webidl @@ -3,6 +3,7 @@ interface URL { constructor(USVString url, optional USVString base); + [WebIDL2JSCallWithGlobal] static URL? parse(USVString url, optional USVString base); static boolean canParse(USVString url, optional USVString base); stringifier attribute USVString href; diff --git a/scripts/get-latest-platform-tests.js b/scripts/get-latest-platform-tests.js index 50e2401..fb0a936 100644 --- a/scripts/get-latest-platform-tests.js +++ b/scripts/get-latest-platform-tests.js @@ -13,7 +13,7 @@ const path = require("path"); // 1. Go to https://github.com/web-platform-tests/wpt/tree/master/url // 2. Press "y" on your keyboard to get a permalink // 3. Copy the commit hash -const commitHash = "72b915d4b3754f081ef5899bf6a777efe71b2fc5"; +const commitHash = "48178346fe87222856812842fb7af7b01baa1530"; const urlPrefix = `https://raw.githubusercontent.com/web-platform-tests/wpt/${commitHash}/url/`; const targetDir = path.resolve(__dirname, "..", "test", "web-platform-tests"); @@ -32,6 +32,7 @@ exports.directlyRunnableTests = [ "url-searchparams.any.js", "url-setters-stripping.any.js", "url-statics-canparse.any.js", + "url-statics-parse.any.js", "url-tojson.any.js", "urlencoded-parser.any.js", "urlsearchparams-append.any.js", diff --git a/test/testharness.js b/test/testharness.js index 0c2bf8b..3b4c2eb 100644 --- a/test/testharness.js +++ b/test/testharness.js @@ -29,6 +29,10 @@ module.exports = { assert.strictEqual(actual, expected); }, + assert_not_equals(actual, expected) { + assert.notStrictEqual(actual, expected); + }, + assert_array_equals(actual, expected) { assert.deepStrictEqual([...actual], [...expected]); },