2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-09-08 05:14:29 -04:00
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
|
|
|
/// <reference path="../webidl/internal.d.ts" />
|
|
|
|
/// <reference path="./internal.d.ts" />
|
|
|
|
/// <reference path="./lib.deno_url.d.ts" />
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const ops = core.ops;
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ObjectKeys,
|
|
|
|
ObjectFromEntries,
|
|
|
|
RegExpPrototypeExec,
|
|
|
|
RegExpPrototypeTest,
|
2023-02-28 18:14:16 -05:00
|
|
|
SafeRegExp,
|
2023-02-07 14:22:46 -05:00
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const _components = Symbol("components");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef Components
|
|
|
|
* @property {Component} protocol
|
|
|
|
* @property {Component} username
|
|
|
|
* @property {Component} password
|
|
|
|
* @property {Component} hostname
|
|
|
|
* @property {Component} port
|
|
|
|
* @property {Component} pathname
|
|
|
|
* @property {Component} search
|
|
|
|
* @property {Component} hash
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef Component
|
|
|
|
* @property {string} patternString
|
|
|
|
* @property {RegExp} regexp
|
|
|
|
* @property {string[]} groupNameList
|
|
|
|
*/
|
|
|
|
|
|
|
|
class URLPattern {
|
|
|
|
/** @type {Components} */
|
|
|
|
[_components];
|
2021-09-08 05:14:29 -04:00
|
|
|
|
|
|
|
/**
|
2023-02-07 14:22:46 -05:00
|
|
|
* @param {URLPatternInput} input
|
|
|
|
* @param {string} [baseURL]
|
2021-09-08 05:14:29 -04:00
|
|
|
*/
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor(input, baseURL = undefined) {
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
const prefix = "Failed to construct 'URLPattern'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-02-07 14:22:46 -05:00
|
|
|
input = webidl.converters.URLPatternInput(input, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
if (baseURL !== undefined) {
|
|
|
|
baseURL = webidl.converters.USVString(baseURL, {
|
2021-09-08 05:14:29 -04:00
|
|
|
prefix,
|
2023-02-07 14:22:46 -05:00
|
|
|
context: "Argument 2",
|
2021-09-08 05:14:29 -04:00
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const components = ops.op_urlpattern_parse(input, baseURL);
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const keys = ObjectKeys(components);
|
|
|
|
for (let i = 0; i < keys.length; ++i) {
|
|
|
|
const key = keys[i];
|
|
|
|
try {
|
2023-02-28 18:14:16 -05:00
|
|
|
components[key].regexp = new SafeRegExp(
|
2023-02-07 14:22:46 -05:00
|
|
|
components[key].regexpString,
|
|
|
|
"u",
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
throw new TypeError(`${prefix}: ${key} is invalid; ${e.message}`);
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_components] = components;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get protocol() {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
return this[_components].protocol.patternString;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get username() {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
return this[_components].username.patternString;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get password() {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
return this[_components].password.patternString;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get hostname() {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
return this[_components].hostname.patternString;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get port() {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
return this[_components].port.patternString;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get pathname() {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
return this[_components].pathname.patternString;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get search() {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
return this[_components].search.patternString;
|
|
|
|
}
|
|
|
|
|
|
|
|
get hash() {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
return this[_components].hash.patternString;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {URLPatternInput} input
|
|
|
|
* @param {string} [baseURL]
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
test(input, baseURL = undefined) {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
const prefix = "Failed to execute 'test' on 'URLPattern'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-02-07 14:22:46 -05:00
|
|
|
input = webidl.converters.URLPatternInput(input, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
if (baseURL !== undefined) {
|
|
|
|
baseURL = webidl.converters.USVString(baseURL, {
|
2021-09-08 05:14:29 -04:00
|
|
|
prefix,
|
2023-02-07 14:22:46 -05:00
|
|
|
context: "Argument 2",
|
2021-09-08 05:14:29 -04:00
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const res = ops.op_urlpattern_process_match_input(
|
|
|
|
input,
|
|
|
|
baseURL,
|
|
|
|
);
|
|
|
|
if (res === null) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const values = res[0];
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const keys = ObjectKeys(values);
|
|
|
|
for (let i = 0; i < keys.length; ++i) {
|
|
|
|
const key = keys[i];
|
|
|
|
if (!RegExpPrototypeTest(this[_components][key].regexp, values[key])) {
|
|
|
|
return false;
|
2021-09-08 05:14:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {URLPatternInput} input
|
|
|
|
* @param {string} [baseURL]
|
|
|
|
* @returns {URLPatternResult | null}
|
|
|
|
*/
|
|
|
|
exec(input, baseURL = undefined) {
|
|
|
|
webidl.assertBranded(this, URLPatternPrototype);
|
|
|
|
const prefix = "Failed to execute 'exec' on 'URLPattern'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-02-07 14:22:46 -05:00
|
|
|
input = webidl.converters.URLPatternInput(input, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
if (baseURL !== undefined) {
|
|
|
|
baseURL = webidl.converters.USVString(baseURL, {
|
2021-09-08 05:14:29 -04:00
|
|
|
prefix,
|
2023-02-07 14:22:46 -05:00
|
|
|
context: "Argument 2",
|
2021-09-08 05:14:29 -04:00
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const res = ops.op_urlpattern_process_match_input(
|
|
|
|
input,
|
|
|
|
baseURL,
|
|
|
|
);
|
|
|
|
if (res === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const { 0: values, 1: inputs } = res;
|
|
|
|
if (inputs[1] === null) {
|
|
|
|
inputs.pop();
|
|
|
|
}
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @type {URLPatternResult} */
|
|
|
|
const result = { inputs };
|
|
|
|
|
|
|
|
const keys = ObjectKeys(values);
|
|
|
|
for (let i = 0; i < keys.length; ++i) {
|
|
|
|
const key = keys[i];
|
|
|
|
/** @type {Component} */
|
|
|
|
const component = this[_components][key];
|
|
|
|
const input = values[key];
|
|
|
|
const match = RegExpPrototypeExec(component.regexp, input);
|
|
|
|
if (match === null) {
|
|
|
|
return null;
|
2021-09-08 05:14:29 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const groupEntries = ArrayPrototypeMap(
|
|
|
|
component.groupNameList,
|
|
|
|
(name, i) => [name, match[i + 1] ?? ""],
|
|
|
|
);
|
|
|
|
const groups = ObjectFromEntries(groupEntries);
|
|
|
|
result[key] = {
|
|
|
|
input,
|
|
|
|
groups,
|
|
|
|
};
|
2021-09-08 05:14:29 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return result;
|
2021-09-08 05:14:29 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.customInspect")](inspect) {
|
|
|
|
return `URLPattern ${
|
|
|
|
inspect({
|
|
|
|
protocol: this.protocol,
|
|
|
|
username: this.username,
|
|
|
|
password: this.password,
|
|
|
|
hostname: this.hostname,
|
|
|
|
port: this.port,
|
|
|
|
pathname: this.pathname,
|
|
|
|
search: this.search,
|
|
|
|
hash: this.hash,
|
|
|
|
})
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
webidl.configurePrototype(URLPattern);
|
|
|
|
const URLPatternPrototype = URLPattern.prototype;
|
|
|
|
|
|
|
|
webidl.converters.URLPatternInit = webidl
|
|
|
|
.createDictionaryConverter("URLPatternInit", [
|
|
|
|
{ key: "protocol", converter: webidl.converters.USVString },
|
|
|
|
{ key: "username", converter: webidl.converters.USVString },
|
|
|
|
{ key: "password", converter: webidl.converters.USVString },
|
|
|
|
{ key: "hostname", converter: webidl.converters.USVString },
|
|
|
|
{ key: "port", converter: webidl.converters.USVString },
|
|
|
|
{ key: "pathname", converter: webidl.converters.USVString },
|
|
|
|
{ key: "search", converter: webidl.converters.USVString },
|
|
|
|
{ key: "hash", converter: webidl.converters.USVString },
|
|
|
|
{ key: "baseURL", converter: webidl.converters.USVString },
|
|
|
|
]);
|
|
|
|
|
|
|
|
webidl.converters["URLPatternInput"] = (V, opts) => {
|
|
|
|
// Union for (URLPatternInit or USVString)
|
|
|
|
if (typeof V == "object") {
|
|
|
|
return webidl.converters.URLPatternInit(V, opts);
|
|
|
|
}
|
|
|
|
return webidl.converters.USVString(V, opts);
|
|
|
|
};
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { URLPattern };
|