Skip to content

Commit

Permalink
fix(utils): make URL search params parsing and serialization WHATWG U…
Browse files Browse the repository at this point in the history
…RL compliant (#9809)

Refs #9804
  • Loading branch information
glowcloud authored Apr 11, 2024
1 parent 9e02f47 commit 52c4b95
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
26 changes: 4 additions & 22 deletions src/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})
})
Expand Down

0 comments on commit 52c4b95

Please sign in to comment.