fix: getQueryParamsFromString function updated to use URLSearchParams (#37559)

This commit is contained in:
Aman Agarwal 2024-11-19 21:33:09 +05:30 committed by GitHub
parent 7612e4f14d
commit 7e13fcbe7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 8 deletions

View File

@ -0,0 +1,12 @@
import { getQueryParamsFromString } from "./getQueryParamsObject";
describe("getQueryParamsObject test", () => {
it("Check whether getQueryParamsFromString returns object with input string containing new lines", () => {
const output = getQueryParamsFromString(
"branch=new&text=%3Cp%3EHello%3C%2Fp%3E%0A%3Cp%3EWorld!%3C%2Fp%3E",
);
expect(output.text).toEqual("<p>Hello</p>\n<p>World!</p>");
expect(output.branch).toEqual("new");
});
});

View File

@ -8,14 +8,7 @@ export const getQueryParamsFromString = (search: string | undefined) => {
if (!search) return {}; if (!search) return {};
try { try {
return JSON.parse( return Object.fromEntries(new URLSearchParams(search).entries());
'{"' +
decodeURI(search)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"') +
'"}',
);
} catch (e) { } catch (e) {
log.error(e, "error parsing search string"); log.error(e, "error parsing search string");