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 URL.canParse #47179

Merged
merged 6 commits into from
Mar 22, 2023
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
Next Next commit
url: implement URL.canParse
  • Loading branch information
KhafraDev committed Mar 20, 2023
commit 1e5619d93a431482a06b0a610db50ea2b89d064e
19 changes: 19 additions & 0 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,16 @@ class URL {
toJSON() {
return this.#context.href;
}

static canParse(url, base = undefined) {
url = `${url}`;
anonrig marked this conversation as resolved.
Show resolved Hide resolved

if (base !== undefined) {
base = `${base}`;
}

return parse(url, base, () => {});
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
}
}

ObjectDefineProperties(URL.prototype, {
Expand All @@ -733,6 +743,15 @@ ObjectDefineProperties(URL.prototype, {
toJSON: kEnumerableProperty,
});

ObjectDefineProperties(URL, {
canParse: {
__proto__: null,
configurable: true,
writable: true,
enumerable: true,
},
});

function installObjectURLMethods() {
const {
storeDataObject,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Last update:
- resource-timing: https://github.com/web-platform-tests/wpt/tree/22d38586d0/resource-timing
- resources: https://github.com/web-platform-tests/wpt/tree/919874f84f/resources
- streams: https://github.com/web-platform-tests/wpt/tree/51750bc8d7/streams
- url: https://github.com/web-platform-tests/wpt/tree/84caeb6fbd/url
- url: https://github.com/web-platform-tests/wpt/tree/7c5c3cc125/url
- user-timing: https://github.com/web-platform-tests/wpt/tree/df24fb604e/user-timing
- wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/d8dbe6990b/wasm/jsapi
- wasm/webapi: https://github.com/web-platform-tests/wpt/tree/fd1b23eeaa/wasm/webapi
Expand Down
42 changes: 42 additions & 0 deletions test/fixtures/wpt/url/url-statics-canparse.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This intentionally does not use resources/urltestdata.json to preserve resources.
anonrig marked this conversation as resolved.
Show resolved Hide resolved
[
{
"url": undefined,
"base": undefined,
"expected": false
},
{
"url": "a:b",
"base": undefined,
"expected": true
},
{
"url": undefined,
"base": "a:b",
"expected": false
},
{
"url": "a:/b",
"base": undefined,
"expected": true
},
{
"url": undefined,
"base": "a:/b",
"expected": true
},
{
"url": "https://test:test",
"base": undefined,
"expected": false
},
{
"url": "a",
"base": "https://b/",
"expected": true
}
].forEach(({ url, base, expected }) => {
test(() => {
assert_equals(URL.canParse(url, base), expected);
}, `URL.canParse(${url}, ${base})`);
});
2 changes: 1 addition & 1 deletion test/fixtures/wpt/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"path": "streams"
},
"url": {
"commit": "84caeb6fbdf45129f57c67448e6113ee1ced9fb3",
"commit": "7c5c3cc125979b4768d414471e6ab655b473aae8",
"path": "url"
},
"user-timing": {
Expand Down