ident.cjs 3.4 KB

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