2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2020-09-17 13:13:20 -04:00
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
function requiredArguments(name, length, required) {
|
2020-09-17 13:13:20 -04:00
|
|
|
if (length < required) {
|
|
|
|
const errMsg = `${name} requires at least ${required} argument${
|
|
|
|
required === 1 ? "" : "s"
|
|
|
|
}, but only ${length} present`;
|
|
|
|
throw new TypeError(errMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
const paramLists = new WeakMap();
|
2020-07-19 13:49:44 -04:00
|
|
|
const urls = new WeakMap();
|
|
|
|
|
|
|
|
class URLSearchParams {
|
|
|
|
#params = [];
|
|
|
|
|
|
|
|
constructor(init = "") {
|
|
|
|
if (typeof init === "string") {
|
2021-03-01 20:30:24 -05:00
|
|
|
// Overload: USVString
|
|
|
|
// If init is a string and starts with U+003F (?),
|
|
|
|
// remove the first code point from init.
|
|
|
|
if (init[0] == "?") {
|
|
|
|
init = init.slice(1);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-04-12 15:55:05 -04:00
|
|
|
this.#params = core.opSync("op_url_parse_search_params", init);
|
2021-03-01 20:30:24 -05:00
|
|
|
} else if (
|
|
|
|
Array.isArray(init) ||
|
|
|
|
typeof init?.[Symbol.iterator] == "function"
|
|
|
|
) {
|
|
|
|
// Overload: sequence<sequence<USVString>>
|
|
|
|
for (const pair of init) {
|
|
|
|
// If pair does not contain exactly two items, then throw a TypeError.
|
|
|
|
if (pair.length !== 2) {
|
|
|
|
throw new TypeError(
|
|
|
|
"URLSearchParams.constructor sequence argument must only contain pair elements",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.#params.push([String(pair[0]), String(pair[1])]);
|
|
|
|
}
|
|
|
|
} else if (Object(init) !== init) {
|
|
|
|
// pass
|
|
|
|
} else if (init instanceof URLSearchParams) {
|
2020-07-19 13:49:44 -04:00
|
|
|
this.#params = [...init.#params];
|
2021-03-01 20:30:24 -05:00
|
|
|
} else {
|
|
|
|
// Overload: record<USVString, USVString>
|
|
|
|
for (const key of Object.keys(init)) {
|
|
|
|
this.#params.push([key, String(init[key])]);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
paramLists.set(this, this.#params);
|
2020-07-19 13:49:44 -04:00
|
|
|
urls.set(this, null);
|
|
|
|
}
|
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
#updateUrlSearch = () => {
|
2020-07-19 13:49:44 -04:00
|
|
|
const url = urls.get(this);
|
|
|
|
if (url == null) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-01 20:30:24 -05:00
|
|
|
const parseArgs = { href: url.href, setSearch: this.toString() };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(url, core.opSync("op_url_parse", parseArgs));
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
append(name, value) {
|
|
|
|
requiredArguments("URLSearchParams.append", arguments.length, 2);
|
2021-03-01 20:30:24 -05:00
|
|
|
this.#params.push([String(name), String(value)]);
|
|
|
|
this.#updateUrlSearch();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(name) {
|
|
|
|
requiredArguments("URLSearchParams.delete", arguments.length, 1);
|
|
|
|
name = String(name);
|
|
|
|
let i = 0;
|
|
|
|
while (i < this.#params.length) {
|
|
|
|
if (this.#params[i][0] === name) {
|
|
|
|
this.#params.splice(i, 1);
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 20:30:24 -05:00
|
|
|
this.#updateUrlSearch();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getAll(name) {
|
|
|
|
requiredArguments("URLSearchParams.getAll", arguments.length, 1);
|
|
|
|
name = String(name);
|
|
|
|
const values = [];
|
|
|
|
for (const entry of this.#params) {
|
|
|
|
if (entry[0] === name) {
|
|
|
|
values.push(entry[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
get(name) {
|
|
|
|
requiredArguments("URLSearchParams.get", arguments.length, 1);
|
|
|
|
name = String(name);
|
|
|
|
for (const entry of this.#params) {
|
|
|
|
if (entry[0] === name) {
|
|
|
|
return entry[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
has(name) {
|
|
|
|
requiredArguments("URLSearchParams.has", arguments.length, 1);
|
|
|
|
name = String(name);
|
|
|
|
return this.#params.some((entry) => entry[0] === name);
|
|
|
|
}
|
|
|
|
|
|
|
|
set(name, value) {
|
|
|
|
requiredArguments("URLSearchParams.set", arguments.length, 2);
|
|
|
|
|
|
|
|
// If there are any name-value pairs whose name is name, in list,
|
|
|
|
// set the value of the first such name-value pair to value
|
|
|
|
// and remove the others.
|
|
|
|
name = String(name);
|
|
|
|
value = String(value);
|
|
|
|
let found = false;
|
|
|
|
let i = 0;
|
|
|
|
while (i < this.#params.length) {
|
|
|
|
if (this.#params[i][0] === name) {
|
|
|
|
if (!found) {
|
|
|
|
this.#params[i][1] = value;
|
|
|
|
found = true;
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
this.#params.splice(i, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, append a new name-value pair whose name is name
|
|
|
|
// and value is value, to list.
|
|
|
|
if (!found) {
|
2021-03-01 20:30:24 -05:00
|
|
|
this.#params.push([String(name), String(value)]);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
this.#updateUrlSearch();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sort() {
|
|
|
|
this.#params.sort((a, b) => (a[0] === b[0] ? 0 : a[0] > b[0] ? 1 : -1));
|
2021-03-01 20:30:24 -05:00
|
|
|
this.#updateUrlSearch();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
forEach(callbackfn, thisArg) {
|
2020-07-19 13:49:44 -04:00
|
|
|
requiredArguments("URLSearchParams.forEach", arguments.length, 1);
|
|
|
|
|
|
|
|
if (typeof thisArg !== "undefined") {
|
|
|
|
callbackfn = callbackfn.bind(thisArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [key, value] of this.#params) {
|
|
|
|
callbackfn(value, key, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*keys() {
|
|
|
|
for (const [key] of this.#params) {
|
|
|
|
yield key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*values() {
|
|
|
|
for (const [, value] of this.#params) {
|
|
|
|
yield value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*entries() {
|
|
|
|
yield* this.#params;
|
|
|
|
}
|
|
|
|
|
|
|
|
*[Symbol.iterator]() {
|
|
|
|
yield* this.#params;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_url_stringify_search_params", this.#params);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
const parts = new WeakMap();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
class URL {
|
|
|
|
#searchParams = null;
|
2020-07-23 21:37:11 -04:00
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
constructor(url, base) {
|
|
|
|
new.target;
|
2020-07-23 21:37:11 -04:00
|
|
|
|
2021-03-01 20:30:24 -05:00
|
|
|
if (url instanceof URL && base === undefined) {
|
|
|
|
parts.set(this, parts.get(url));
|
2020-07-23 21:37:11 -04:00
|
|
|
} else {
|
2021-03-01 20:30:24 -05:00
|
|
|
base = base !== undefined ? String(base) : base;
|
|
|
|
const parseArgs = { href: String(url), baseHref: base };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 10:31:46 -05:00
|
|
|
[Symbol.for("Deno.customInspect")](inspect) {
|
2021-01-29 08:08:22 -05:00
|
|
|
const object = {
|
|
|
|
href: this.href,
|
|
|
|
origin: this.origin,
|
|
|
|
protocol: this.protocol,
|
|
|
|
username: this.username,
|
|
|
|
password: this.password,
|
|
|
|
host: this.host,
|
|
|
|
hostname: this.hostname,
|
|
|
|
port: this.port,
|
|
|
|
pathname: this.pathname,
|
|
|
|
hash: this.hash,
|
|
|
|
search: this.search,
|
|
|
|
};
|
2021-02-09 10:31:46 -05:00
|
|
|
return `${this.constructor.name} ${inspect(object)}`;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#updateSearchParams = () => {
|
2021-03-01 20:30:24 -05:00
|
|
|
if (this.#searchParams != null) {
|
|
|
|
const params = paramLists.get(this.#searchParams);
|
2021-04-12 15:55:05 -04:00
|
|
|
const newParams = core.opSync(
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
"op_url_parse_search_params",
|
2021-03-01 20:30:24 -05:00
|
|
|
this.search.slice(1),
|
|
|
|
);
|
|
|
|
params.splice(0, params.length, ...newParams);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
get hash() {
|
|
|
|
return parts.get(this).hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
set hash(value) {
|
2021-03-01 20:30:24 -05:00
|
|
|
try {
|
|
|
|
const parseArgs = { href: this.href, setHash: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2021-03-01 20:30:24 -05:00
|
|
|
} catch {
|
|
|
|
/* pass */
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get host() {
|
2021-03-01 20:30:24 -05:00
|
|
|
return parts.get(this).host;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
set host(value) {
|
2021-03-01 20:30:24 -05:00
|
|
|
try {
|
|
|
|
const parseArgs = { href: this.href, setHost: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2021-03-01 20:30:24 -05:00
|
|
|
} catch {
|
|
|
|
/* pass */
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get hostname() {
|
|
|
|
return parts.get(this).hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
set hostname(value) {
|
|
|
|
try {
|
2021-03-01 20:30:24 -05:00
|
|
|
const parseArgs = { href: this.href, setHostname: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2020-11-03 10:19:29 -05:00
|
|
|
} catch {
|
2021-03-01 20:30:24 -05:00
|
|
|
/* pass */
|
2020-11-03 10:19:29 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get href() {
|
2021-03-01 20:30:24 -05:00
|
|
|
return parts.get(this).href;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
set href(value) {
|
2021-03-01 20:30:24 -05:00
|
|
|
try {
|
|
|
|
const parseArgs = { href: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2021-03-01 20:30:24 -05:00
|
|
|
} catch {
|
|
|
|
throw new TypeError("Invalid URL");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-03-01 20:30:24 -05:00
|
|
|
this.#updateSearchParams();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get origin() {
|
2021-03-01 20:30:24 -05:00
|
|
|
return parts.get(this).origin;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get password() {
|
|
|
|
return parts.get(this).password;
|
|
|
|
}
|
|
|
|
|
|
|
|
set password(value) {
|
2021-03-01 20:30:24 -05:00
|
|
|
try {
|
|
|
|
const parseArgs = { href: this.href, setPassword: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2021-03-01 20:30:24 -05:00
|
|
|
} catch {
|
|
|
|
/* pass */
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get pathname() {
|
2021-03-01 20:30:24 -05:00
|
|
|
return parts.get(this).pathname;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
set pathname(value) {
|
2021-03-01 20:30:24 -05:00
|
|
|
try {
|
|
|
|
const parseArgs = { href: this.href, setPathname: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2021-03-01 20:30:24 -05:00
|
|
|
} catch {
|
|
|
|
/* pass */
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get port() {
|
2021-03-01 20:30:24 -05:00
|
|
|
return parts.get(this).port;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
set port(value) {
|
2021-03-01 20:30:24 -05:00
|
|
|
try {
|
|
|
|
const parseArgs = { href: this.href, setPort: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2021-03-01 20:30:24 -05:00
|
|
|
} catch {
|
|
|
|
/* pass */
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get protocol() {
|
2021-03-01 20:30:24 -05:00
|
|
|
return parts.get(this).protocol;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
set protocol(value) {
|
2021-03-01 20:30:24 -05:00
|
|
|
try {
|
|
|
|
const parseArgs = { href: this.href, setProtocol: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2021-03-01 20:30:24 -05:00
|
|
|
} catch {
|
|
|
|
/* pass */
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get search() {
|
2021-03-01 20:30:24 -05:00
|
|
|
return parts.get(this).search;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
set search(value) {
|
2021-03-01 20:30:24 -05:00
|
|
|
try {
|
|
|
|
const parseArgs = { href: this.href, setSearch: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2021-03-01 20:30:24 -05:00
|
|
|
this.#updateSearchParams();
|
|
|
|
} catch {
|
|
|
|
/* pass */
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get username() {
|
|
|
|
return parts.get(this).username;
|
|
|
|
}
|
|
|
|
|
|
|
|
set username(value) {
|
2021-03-01 20:30:24 -05:00
|
|
|
try {
|
|
|
|
const parseArgs = { href: this.href, setUsername: String(value) };
|
2021-04-12 15:55:05 -04:00
|
|
|
parts.set(this, core.opSync("op_url_parse", parseArgs));
|
2021-03-01 20:30:24 -05:00
|
|
|
} catch {
|
|
|
|
/* pass */
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get searchParams() {
|
2021-03-01 20:30:24 -05:00
|
|
|
if (this.#searchParams == null) {
|
|
|
|
this.#searchParams = new URLSearchParams(this.search);
|
|
|
|
urls.set(this.#searchParams, this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-03-01 20:30:24 -05:00
|
|
|
return this.#searchParams;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return this.href;
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return this.href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
/**
|
|
|
|
* This function implements application/x-www-form-urlencoded parsing.
|
|
|
|
* https://url.spec.whatwg.org/#concept-urlencoded-parser
|
|
|
|
* @param {Uint8Array} bytes
|
2021-04-28 10:08:51 -04:00
|
|
|
* @returns {[string, string][]}
|
2021-04-20 08:47:22 -04:00
|
|
|
*/
|
|
|
|
function parseUrlEncoded(bytes) {
|
|
|
|
return core.opSync("op_url_parse_search_params", null, bytes);
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.url = {
|
|
|
|
URL,
|
|
|
|
URLSearchParams,
|
2021-04-20 08:47:22 -04:00
|
|
|
parseUrlEncoded,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|