feat(ext/url): URL.canParse (#18286)

This commit is contained in:
Kenta Moriuchi 2023-04-04 20:34:12 +09:00 committed by GitHub
parent c341dbee5d
commit 2dc2016837
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 15 deletions

View file

@ -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"
},

View file

@ -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();
}

View file

@ -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;

View file

@ -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": {