Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make parseSearch and serializeSearch compliant with the WHATWG specification #9804

Closed
glowcloud opened this issue Apr 11, 2024 · 2 comments
Closed

Comments

@glowcloud
Copy link
Contributor

glowcloud commented Apr 11, 2024

Right now the parseSearch and serializeSearch functions in utils use custom string parsing:

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
}
export const serializeSearch = (searchMap) => {
return Object.keys(searchMap).map(k => {
return encodeURIComponent(k) + "=" + encodeURIComponent(searchMap[k])
}).join("&")
}

Instead, we should move to using URLSearchParams which will make these functions compliant with the WHATWG specification.

The functions should be rewritten into:

export const parseSearch = () => {
  const searchParams = new URLSearchParams(win.location.search)
  return Object.fromEntries(searchParams)
}

export const serializeSearch = (searchMap) => {
  const searchParams = new URLSearchParams(Object.entries(searchMap))
  return String(searchParams)
}

Based on: #9722 (comment)

@char0n
Copy link
Member

char0n commented Apr 11, 2024

Addressed in #9809 (review)

@char0n char0n closed this as completed Apr 11, 2024
@glowcloud
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants