1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

fix: URLSearchParams.toString() behaviour is different from browsers (#7017)

This commit is contained in:
Prabhanjan 2020-08-12 15:38:24 +05:30 committed by GitHub
parent 81ce4499e7
commit b344a7f81f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View file

@ -222,7 +222,7 @@
return this.#params
.map(
(tuple) =>
`${encodeURIComponent(tuple[0])}=${encodeURIComponent(tuple[1])}`,
`${encodeSearchParam(tuple[0])}=${encodeSearchParam(tuple[1])}`,
)
.join("&");
}
@ -792,6 +792,11 @@
return ["\u0000", "\u0009", "\u000A", "\u000D", "\u0020", "\u0023", "\u0025", "\u002F", "\u003A", "\u003C", "\u003E", "\u003F", "\u0040", "\u005B", "\u005C", "\u005D", "\u005E"].includes(c);
}
function charInFormUrlencodedSet(c) {
// deno-fmt-ignore
return charInUserinfoSet(c) || ["\u0021", "\u0024", "\u0025", "\u0026", "\u0027", "\u0028", "\u0029", "\u002B", "\u002C", "\u007E"].includes(c);
}
const encoder = new TextEncoder();
function encodeChar(c) {
@ -876,6 +881,11 @@
);
}
function encodeSearchParam(s) {
return [...s].map((c) => (charInFormUrlencodedSet(c) ? encodeChar(c) : c))
.join("").replace("%20", "+");
}
window.__bootstrap.url = {
URL,
URLSearchParams,

View file

@ -1,6 +1,44 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts";
unitTest(function urlSearchParamsWithSpace(): void {
const init = { str: "hello world" };
const searchParams = new URLSearchParams(init).toString();
assertEquals(searchParams, "str=hello+world");
});
unitTest(function urlSearchParamsWithExclamation(): void {
const init = [
["str", "hello, world!"],
];
const searchParams = new URLSearchParams(init).toString();
assertEquals(searchParams, "str=hello%2C+world%21");
});
unitTest(function urlSearchParamsWithQuotes(): void {
const init = [
["str", "'hello world'"],
];
const searchParams = new URLSearchParams(init).toString();
assertEquals(searchParams, "str=%27hello+world%27");
});
unitTest(function urlSearchParamsWithBraket(): void {
const init = [
["str", "(hello world)"],
];
const searchParams = new URLSearchParams(init).toString();
assertEquals(searchParams, "str=%28hello+world%29");
});
unitTest(function urlSearchParamsWithTilde(): void {
const init = [
["str", "hello~world"],
];
const searchParams = new URLSearchParams(init).toString();
assertEquals(searchParams, "str=hello%7Eworld");
});
unitTest(function urlSearchParamsInitString(): void {
const init = "c=4&a=2&b=3&%C3%A1=1";
const searchParams = new URLSearchParams(init);