ident.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. isName,
  3. isValidEscape,
  4. consumeEscaped,
  5. decodeEscaped
  6. } from '../tokenizer/index.js';
  7. const REVERSE_SOLIDUS = 0x005c; // U+005C REVERSE SOLIDUS (\)
  8. export function decode(str) {
  9. const end = str.length - 1;
  10. let decoded = '';
  11. for (let i = 0; i < str.length; i++) {
  12. let code = str.charCodeAt(i);
  13. if (code === REVERSE_SOLIDUS) {
  14. // special case at the ending
  15. if (i === end) {
  16. // if the next input code point is EOF, do nothing
  17. break;
  18. }
  19. code = str.charCodeAt(++i);
  20. // consume escaped
  21. if (isValidEscape(REVERSE_SOLIDUS, code)) {
  22. const escapeStart = i - 1;
  23. const escapeEnd = consumeEscaped(str, escapeStart);
  24. i = escapeEnd - 1;
  25. decoded += decodeEscaped(str.substring(escapeStart + 1, escapeEnd));
  26. } else {
  27. // \r\n
  28. if (code === 0x000d && str.charCodeAt(i + 1) === 0x000a) {
  29. i++;
  30. }
  31. }
  32. } else {
  33. decoded += str[i];
  34. }
  35. }
  36. return decoded;
  37. }
  38. // https://drafts.csswg.org/cssom/#serialize-an-identifier
  39. // § 2.1. Common Serializing Idioms
  40. export function encode(str) {
  41. let encoded = '';
  42. // If the character is the first character and is a "-" (U+002D),
  43. // and there is no second character, then the escaped character.
  44. // Note: That's means a single dash string "-" return as escaped dash,
  45. // so move the condition out of the main loop
  46. if (str.length === 1 && str.charCodeAt(0) === 0x002D) {
  47. return '\\-';
  48. }
  49. // To serialize an identifier means to create a string represented
  50. // by the concatenation of, for each character of the identifier:
  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 (
  59. // If the character is in the range [\1-\1f] (U+0001 to U+001F) or is U+007F ...
  60. // Note: Do not compare with 0x0001 since 0x0000 is precessed before
  61. code <= 0x001F || code === 0x007F ||
  62. // [or] ... is in the range [0-9] (U+0030 to U+0039),
  63. (code >= 0x0030 && code <= 0x0039 && (
  64. // If the character is the first character ...
  65. i === 0 ||
  66. // If the character is the second character ... and the first character is a "-" (U+002D)
  67. i === 1 && str.charCodeAt(0) === 0x002D
  68. ))
  69. ) {
  70. // ... then the character escaped as code point.
  71. encoded += '\\' + code.toString(16) + ' ';
  72. continue;
  73. }
  74. // If the character is not handled by one of the above rules and is greater
  75. // than or equal to U+0080, is "-" (U+002D) or "_" (U+005F), or is in one
  76. // of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to U+005A),
  77. // or \[a-z] (U+0061 to U+007A), then the character itself.
  78. if (isName(code)) {
  79. encoded += str.charAt(i);
  80. } else {
  81. // Otherwise, the escaped character.
  82. encoded += '\\' + str.charAt(i);
  83. }
  84. }
  85. return encoded;
  86. }