string.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {
  2. isHexDigit,
  3. isWhiteSpace,
  4. isValidEscape,
  5. consumeEscaped,
  6. decodeEscaped
  7. } from '../tokenizer/index.js';
  8. const REVERSE_SOLIDUS = 0x005c; // U+005C REVERSE SOLIDUS (\)
  9. const QUOTATION_MARK = 0x0022; // "
  10. const APOSTROPHE = 0x0027; // '
  11. export function decode(str) {
  12. const len = str.length;
  13. const firstChar = str.charCodeAt(0);
  14. const start = firstChar === QUOTATION_MARK || firstChar === APOSTROPHE ? 1 : 0;
  15. const end = start === 1 && len > 1 && str.charCodeAt(len - 1) === firstChar ? len - 2 : len - 1;
  16. let decoded = '';
  17. for (let i = start; i <= end; i++) {
  18. let code = str.charCodeAt(i);
  19. if (code === REVERSE_SOLIDUS) {
  20. // special case at the ending
  21. if (i === end) {
  22. // if the next input code point is EOF, do nothing
  23. // otherwise include last quote as escaped
  24. if (i !== len - 1) {
  25. decoded = str.substr(i + 1);
  26. }
  27. break;
  28. }
  29. code = str.charCodeAt(++i);
  30. // consume escaped
  31. if (isValidEscape(REVERSE_SOLIDUS, code)) {
  32. const escapeStart = i - 1;
  33. const escapeEnd = consumeEscaped(str, escapeStart);
  34. i = escapeEnd - 1;
  35. decoded += decodeEscaped(str.substring(escapeStart + 1, escapeEnd));
  36. } else {
  37. // \r\n
  38. if (code === 0x000d && str.charCodeAt(i + 1) === 0x000a) {
  39. i++;
  40. }
  41. }
  42. } else {
  43. decoded += str[i];
  44. }
  45. }
  46. return decoded;
  47. }
  48. // https://drafts.csswg.org/cssom/#serialize-a-string
  49. // § 2.1. Common Serializing Idioms
  50. export function encode(str, apostrophe) {
  51. const quote = apostrophe ? '\'' : '"';
  52. const quoteCode = apostrophe ? APOSTROPHE : QUOTATION_MARK;
  53. let encoded = '';
  54. let wsBeforeHexIsNeeded = false;
  55. for (let i = 0; i < str.length; i++) {
  56. const code = str.charCodeAt(i);
  57. // If the character is NULL (U+0000), then the REPLACEMENT CHARACTER (U+FFFD).
  58. if (code === 0x0000) {
  59. encoded += '\uFFFD';
  60. continue;
  61. }
  62. // If the character is in the range [\1-\1f] (U+0001 to U+001F) or is U+007F,
  63. // the character escaped as code point.
  64. // Note: Do not compare with 0x0001 since 0x0000 is precessed before
  65. if (code <= 0x001f || code === 0x007F) {
  66. encoded += '\\' + code.toString(16);
  67. wsBeforeHexIsNeeded = true;
  68. continue;
  69. }
  70. // If the character is '"' (U+0022) or "\" (U+005C), the escaped character.
  71. if (code === quoteCode || code === REVERSE_SOLIDUS) {
  72. encoded += '\\' + str.charAt(i);
  73. wsBeforeHexIsNeeded = false;
  74. } else {
  75. if (wsBeforeHexIsNeeded && (isHexDigit(code) || isWhiteSpace(code))) {
  76. encoded += ' ';
  77. }
  78. // Otherwise, the character itself.
  79. encoded += str.charAt(i);
  80. wsBeforeHexIsNeeded = false;
  81. }
  82. }
  83. return quote + encoded + quote;
  84. }