string.cjs 3.2 KB

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