From 52c4b9512fa3c26b9ddf0be2eb301452dd465042 Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Thu, 11 Apr 2024 19:06:17 +0200 Subject: [PATCH] fix(utils): make URL search params parsing and serialization WHATWG URL compliant (#9809) Refs #9804 --- src/core/utils/index.js | 26 ++++---------------------- test/unit/core/utils.js | 2 +- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 9ff5770ee2e..862d1c13db3 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -605,31 +605,13 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec } export const parseSearch = () => { - let map = {} - let search = win.location.search - - if(!search) - return {} - - if ( search != "" ) { - let params = search.substr(1).split("&") - - for (let i in params) { - if (!Object.prototype.hasOwnProperty.call(params, i)) { - continue - } - i = params[i].split("=") - map[decodeURIComponent(i[0])] = (i[1] && decodeURIComponent(i[1])) || "" - } - } - - return map + const searchParams = new URLSearchParams(win.location.search) + return Object.fromEntries(searchParams) } export const serializeSearch = (searchMap) => { - return Object.keys(searchMap).map(k => { - return encodeURIComponent(k) + "=" + encodeURIComponent(searchMap[k]) - }).join("&") + const searchParams = new URLSearchParams(Object.entries(searchMap)) + return String(searchParams) } export const btoa = (str) => { diff --git a/test/unit/core/utils.js b/test/unit/core/utils.js index a733543e63e..24e90b9161a 100644 --- a/test/unit/core/utils.js +++ b/test/unit/core/utils.js @@ -1338,7 +1338,7 @@ describe("utils", () => { }) it("encode url components", () => { - expect(serializeSearch({foo: "foo bar"})).toEqual("foo=foo%20bar") + expect(serializeSearch({foo: "foo bar"})).toEqual("foo=foo+bar") }) }) })