fix: unescaped double quotes in combobox search (#21161)

Combobox also uses `cmdk` under the hood, which in the last update seems
to have changed the way it creates the query selector, so double quotes
now need to be escaped.
This commit is contained in:
Charis
2024-02-09 15:27:09 -05:00
committed by GitHub
parent 045dafcef7
commit 8a3201de35

View File

@@ -8,6 +8,26 @@ export type Branch = BranchesData[number]
export type Variable = 'url' | 'anonKey'
function removeDoubleQuotes(str: string) {
return str.replaceAll('"', '')
}
function escapeDoubleQuotes(str: string) {
return str.replaceAll('"', '\\"')
}
function unescapeDoubleQuotes(str: string) {
/**
* Regex matches a backslash followed by a double quote, except when the
* backslash is escaped.
*
* Negative lookbehind:
* \\(?:\\\\)* -> An odd number of backslashes (means the next backslash is escaped)
* (?:^|[^\\]) -> Backslash group is at start or follows non-backslash
*/
return str.replace(/(?<!(?:^|[^\\])\\(?:\\\\)*)\\"/g, '"')
}
export const prettyFormatVariable: Record<Variable, string> = {
url: 'Project URL',
anonKey: 'Anon key',
@@ -18,15 +38,19 @@ export function toDisplayNameOrgProject(org: Org, project: Project) {
}
export function toOrgProjectValue(org: Org, project: Project) {
// @ts-ignore -- problem in OpenAPI spec -- project has ref property
return JSON.stringify([org.id, project.ref, toDisplayNameOrgProject(org, project)])
return escapeDoubleQuotes(
// @ts-ignore -- problem in OpenAPI spec -- project has ref property
JSON.stringify([org.id, project.ref, removeDoubleQuotes(toDisplayNameOrgProject(org, project))])
)
}
export function fromOrgProjectValue(
maybeOrgProject: string
): [string, string, string] | [null, null, null] {
try {
const data = JSON.parse(maybeOrgProject)
// not restoring the double quotes on the display name is fine because it's only used for
// command fuzzy search, not for exact/literal matching
const data = JSON.parse(unescapeDoubleQuotes(maybeOrgProject))
if (!Array.isArray(data) || data.length !== 3) {
throw Error("Shape of parsed JSON doesn't match form of org and project value")
}
@@ -37,12 +61,14 @@ export function fromOrgProjectValue(
}
export function toBranchValue(branch: Branch) {
return JSON.stringify([branch.id, branch.name])
return escapeDoubleQuotes(JSON.stringify([branch.id, removeDoubleQuotes(branch.name)]))
}
export function fromBranchValue(maybeBranch: string): [string, string] | [null, null] {
try {
const data = JSON.parse(maybeBranch)
// not restoring the double quotes on the branch name is fine because it's only used for
// command fuzzy search, not for exact/literal matching
const data = JSON.parse(unescapeDoubleQuotes(maybeBranch))
if (!Array.isArray(data) || data.length !== 2) {
throw Error("Shape of parsed JSON doesn't match form of branch value")
}