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

url: implement parse method for safer URL parsing #52280

Merged
merged 4 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,16 @@ class URL {
this.#updateContext(bindingUrl.parse(input, base));
}

static parse(input, base = undefined) {
thisalihassan marked this conversation as resolved.
Show resolved Hide resolved
try {
const url = new URL(input, base);
return url;
/* eslint-disable-next-line no-unused-vars */
} catch (_) {
thisalihassan marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}

[inspect.custom](depth, opts) {
if (typeof depth === 'number' && depth < 0)
return this;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/wpt/interfaces/url.idl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
interface URL {
constructor(USVString url, optional USVString base);

static URL? parse(USVString url, optional USVString base);
static boolean canParse(USVString url, optional USVString base);

stringifier attribute USVString href;
Expand Down
Loading