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

Implement URL.parse() #283

Merged
merged 1 commit into from
Dec 4, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`).

Expand Down
13 changes: 9 additions & 4 deletions lib/URL-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions lib/URL.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion scripts/get-latest-platform-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions test/testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
},
Expand Down
Loading