summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/common/internal/query/encode_selectively.ts
blob: ab1997b6e458b0b51c2e8b05ff8db9016c8b4345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Encodes a stringified TestQuery so that it can be placed in a `?q=` parameter in a URL.
 *
 * `encodeURIComponent` encodes in accordance with `application/x-www-form-urlencoded`,
 * but URLs don't actually have to be as strict as HTML form encoding
 * (we interpret this purely from JavaScript).
 * So we encode the component, then selectively convert some %-encoded escape codes
 * back to their original form for readability/copyability.
 */
export function encodeURIComponentSelectively(s: string): string {
  let ret = encodeURIComponent(s);
  ret = ret.replace(/%22/g, '"'); // for JSON strings
  ret = ret.replace(/%2C/g, ','); // for path separator, and JSON arrays
  ret = ret.replace(/%3A/g, ':'); // for big separator
  ret = ret.replace(/%3B/g, ';'); // for param separator
  ret = ret.replace(/%3D/g, '='); // for params (k=v)
  ret = ret.replace(/%5B/g, '['); // for JSON arrays
  ret = ret.replace(/%5D/g, ']'); // for JSON arrays
  ret = ret.replace(/%7B/g, '{'); // for JSON objects
  ret = ret.replace(/%7D/g, '}'); // for JSON objects
  ret = ret.replace(/%E2%9C%97/g, '✗'); // for jsUndefinedMagicValue
  return ret;
}