From 2dc20168371e827b86e2ce0d1d7787139fba68f3 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Tue, 4 Apr 2023 20:34:12 +0900 Subject: [PATCH] feat(ext/url): `URL.canParse` (#18286) --- cli/tests/integration/lsp_tests.rs | 2 +- ext/url/00_url.js | 57 +++++++++++++++++++++++------- ext/url/lib.deno_url.d.ts | 1 + tools/wpt/expectation.json | 4 +-- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs index fb95a17de2..aa69b0dafa 100644 --- a/cli/tests/integration/lsp_tests.rs +++ b/cli/tests/integration/lsp_tests.rs @@ -4713,7 +4713,7 @@ fn lsp_completions_auto_import() { "source": "./b.ts", "data": { "exportName": "foo", - "exportMapKey": "foo|6843|file:///a/b", + "exportMapKey": "foo|6845|file:///a/b", "moduleSpecifier": "./b.ts", "fileName": "file:///a/b.ts" }, diff --git a/ext/url/00_url.js b/ext/url/00_url.js index de4fb0ec86..984e487ce2 100644 --- a/ext/url/00_url.js +++ b/ext/url/00_url.js @@ -41,6 +41,12 @@ const SET_SEARCH = 7; const SET_USERNAME = 8; // Helper functions +/** + * @param {string} href + * @param {number} setter + * @param {string} value + * @returns {string} + */ function opUrlReparse(href, setter, value) { const status = ops.op_url_reparse( href, @@ -51,20 +57,28 @@ function opUrlReparse(href, setter, value) { return getSerialization(status, href); } +/** + * @param {string} href + * @param {string} [maybeBase] + * @returns {number} + */ function opUrlParse(href, maybeBase) { - let status; if (maybeBase === undefined) { - status = ops.op_url_parse(href, componentsBuf); - } else { - status = ops.op_url_parse_with_base( - href, - maybeBase, - componentsBuf, - ); + return ops.op_url_parse(href, componentsBuf); } - return getSerialization(status, href, maybeBase); + return ops.op_url_parse_with_base( + href, + maybeBase, + componentsBuf, + ); } +/** + * @param {number} status + * @param {string} href + * @param {string} [maybeBase] + * @returns {string} + */ function getSerialization(status, href, maybeBase) { if (status === 0) { return href; @@ -353,7 +367,7 @@ class URL { /** * @param {string} url - * @param {string} base + * @param {string} [base] */ constructor(url, base = undefined) { const prefix = "Failed to construct 'URL'"; @@ -365,10 +379,28 @@ class URL { }); } this[webidl.brand] = webidl.brand; - this.#serialization = opUrlParse(url, base); + const status = opUrlParse(url, base); + this.#serialization = getSerialization(status, url, base); this.#updateComponents(); } + /** + * @param {string} url + * @param {string} [base] + */ + static canParse(url, base = undefined) { + const prefix = "Failed to call 'URL.canParse'"; + url = webidl.converters.DOMString(url, { prefix, context: "Argument 1" }); + if (base !== undefined) { + base = webidl.converters.DOMString(base, { + prefix, + context: "Argument 2", + }); + } + const status = opUrlParse(url, base); + return status === 0 || status === 1; + } + #updateComponents() { ({ 0: this.#schemeEnd, @@ -520,7 +552,8 @@ class URL { prefix, context: "Argument 1", }); - this.#serialization = opUrlParse(value); + const status = opUrlParse(value); + this.#serialization = getSerialization(status, value); this.#updateComponents(); this.#updateSearchParams(); } diff --git a/ext/url/lib.deno_url.d.ts b/ext/url/lib.deno_url.d.ts index 0181c6fb34..1d5f840199 100644 --- a/ext/url/lib.deno_url.d.ts +++ b/ext/url/lib.deno_url.d.ts @@ -167,6 +167,7 @@ declare class URLSearchParams { */ declare class URL { constructor(url: string | URL, base?: string | URL); + static canParse(url: string | URL, base?: string | URL): boolean; static createObjectURL(blob: Blob): string; static revokeObjectURL(url: string): void; diff --git a/tools/wpt/expectation.json b/tools/wpt/expectation.json index 7367b773ee..8f45901836 100644 --- a/tools/wpt/expectation.json +++ b/tools/wpt/expectation.json @@ -3024,8 +3024,8 @@ ], "url-setters.any.worker.html?include=javascript": true, "url-setters.any.worker.html?include=mailto": true, - "url-statics-canparse.any.html": false, - "url-statics-canparse.any.worker.html": false, + "url-statics-canparse.any.html": true, + "url-statics-canparse.any.worker.html": true, "urlsearchparams-size.any.worker.html": true }, "fetch": {