Skip to content

Commit

Permalink
Encode commas in clauses and labelKey / labelValue pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
apata committed Nov 15, 2024
1 parent 14f77ae commit 0f16e70
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions assets/js/dashboard/util/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,22 @@ export function trimURL(url: string, maxLength: number): string {
}
}

export function encodeURIComponentPermissive(input: string): string {
return (
encodeURIComponent(input)
/* @ts-expect-error API supposedly not present in compilation target */
.replaceAll('%2C', ',')
.replaceAll('%3A', ':')
.replaceAll('%2F', '/')
)
export function encodeURIComponentPermissive(
input: string,
permittedCharacters: string
): string {
return Array.from(permittedCharacters)
.map((character) => [encodeURIComponent(character), character])
.reduce(
(acc, [encodedCharacter, character]) =>
/* @ts-expect-error API supposedly not present in compilation target, but works in major browsers */
acc.replaceAll(encodedCharacter, character),
encodeURIComponent(input)
)
}

export function encodeSearchParamEntry([k, v]: [string, string]): string {
return `${encodeURIComponentPermissive(k)}=${encodeURIComponentPermissive(v)}`
return `${encodeURIComponentPermissive(k, ',:/')}=${encodeURIComponentPermissive(v, ',:/')}`
}

export function isSearchEntryDefined(
Expand Down Expand Up @@ -114,7 +118,7 @@ export function stringifySearch(
return encodedSearchEntries.length ? `?${encodedSearchEntries.join('&')}` : ''
}
function serializeLabelsEntry([k, v]: [string, string]) {
return `${encodeURIComponentPermissive(k)},${encodeURIComponentPermissive(v)}`
return `${encodeURIComponentPermissive(k, ':/')},${encodeURIComponentPermissive(v, ':/')}`
}

function parseLabelsEntry(labelString: string) {
Expand All @@ -126,7 +130,7 @@ function serializeFilter(f: Filter) {
const serializedFilter = [
operator,
dimension,
...clauses.map((c) => encodeURIComponentPermissive(c.toString()))
...clauses.map((c) => encodeURIComponentPermissive(c.toString(), ':/'))
].join(',')
return serializedFilter
}
Expand Down

0 comments on commit 0f16e70

Please sign in to comment.