whatwg.d.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * WHATWG helpers
  3. *
  4. * ```js
  5. * import '@exodus/bytes/encoding.js' // For full legacy multi-byte encodings support
  6. * import { percentEncodeAfterEncoding } from '@exodus/bytes/whatwg.js'
  7. * ```
  8. *
  9. * @module @exodus/bytes/whatwg.js
  10. */
  11. /**
  12. * Implements [percent-encode after encoding](https://url.spec.whatwg.org/#string-percent-encode-after-encoding)
  13. * per WHATWG URL specification.
  14. *
  15. * > [!IMPORTANT]
  16. * > You must import `@exodus/bytes/encoding.js` for this API to accept legacy multi-byte encodings.
  17. *
  18. * Encodings `utf16-le`, `utf16-be`, and `replacement` are not accepted.
  19. *
  20. * [C0 control percent-encode set](https://url.spec.whatwg.org/#c0-control-percent-encode-set) is
  21. * always percent-encoded.
  22. *
  23. * `percentEncodeSet` is an addition to that, and must be a string of unique increasing codepoints
  24. * in range 0x20 - 0x7e, e.g. `' "#<>'`.
  25. *
  26. * This method accepts [DOMStrings](https://webidl.spec.whatwg.org/#idl-DOMString) and converts them
  27. * to [USVStrings](https://webidl.spec.whatwg.org/#idl-USVString).
  28. * This is different from e.g. `encodeURI` and `encodeURIComponent` which throw on surrogates:
  29. * ```js
  30. * > percentEncodeAfterEncoding('utf8', '\ud800', ' "#$%&+,/:;<=>?@[\\]^`{|}') // component
  31. * '%EF%BF%BD'
  32. * > encodeURIComponent('\ud800')
  33. * Uncaught URIError: URI malformed
  34. * ```
  35. *
  36. * @param encoding - The encoding label per WHATWG Encoding spec
  37. * @param input - Input scalar-value string to encode
  38. * @param percentEncodeSet - A string of ASCII chars to escape in addition to C0 control percent-encode set
  39. * @param spaceAsPlus - Whether to encode space as `'+'` instead of `'%20'` or `' '` (default: false)
  40. * @returns The percent-encoded string
  41. */
  42. export function percentEncodeAfterEncoding(
  43. encoding: string,
  44. input: string,
  45. percentEncodeSet: string,
  46. spaceAsPlus?: boolean
  47. ): string;