css.escape.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */
  2. ;(function(root, factory) {
  3. // https://github.com/umdjs/umd/blob/master/returnExports.js
  4. if (typeof exports == 'object') {
  5. // For Node.js.
  6. module.exports = factory(root);
  7. } else if (typeof define == 'function' && define.amd) {
  8. // For AMD. Register as an anonymous module.
  9. define([], factory.bind(root, root));
  10. } else {
  11. // For browser globals (not exposing the function separately).
  12. factory(root);
  13. }
  14. }(typeof global != 'undefined' ? global : this, function(root) {
  15. if (root.CSS && root.CSS.escape) {
  16. return root.CSS.escape;
  17. }
  18. // https://drafts.csswg.org/cssom/#serialize-an-identifier
  19. var cssEscape = function(value) {
  20. if (arguments.length == 0) {
  21. throw new TypeError('`CSS.escape` requires an argument.');
  22. }
  23. var string = String(value);
  24. var length = string.length;
  25. var index = -1;
  26. var codeUnit;
  27. var result = '';
  28. var firstCodeUnit = string.charCodeAt(0);
  29. while (++index < length) {
  30. codeUnit = string.charCodeAt(index);
  31. // Note: there’s no need to special-case astral symbols, surrogate
  32. // pairs, or lone surrogates.
  33. // If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
  34. // (U+FFFD).
  35. if (codeUnit == 0x0000) {
  36. result += '\uFFFD';
  37. continue;
  38. }
  39. if (
  40. // If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
  41. // U+007F, […]
  42. (codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F ||
  43. // If the character is the first character and is in the range [0-9]
  44. // (U+0030 to U+0039), […]
  45. (index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
  46. // If the character is the second character and is in the range [0-9]
  47. // (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
  48. (
  49. index == 1 &&
  50. codeUnit >= 0x0030 && codeUnit <= 0x0039 &&
  51. firstCodeUnit == 0x002D
  52. )
  53. ) {
  54. // https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
  55. result += '\\' + codeUnit.toString(16) + ' ';
  56. continue;
  57. }
  58. if (
  59. // If the character is the first character and is a `-` (U+002D), and
  60. // there is no second character, […]
  61. index == 0 &&
  62. length == 1 &&
  63. codeUnit == 0x002D
  64. ) {
  65. result += '\\' + string.charAt(index);
  66. continue;
  67. }
  68. // If the character is not handled by one of the above rules and is
  69. // greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
  70. // is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
  71. // U+005A), or [a-z] (U+0061 to U+007A), […]
  72. if (
  73. codeUnit >= 0x0080 ||
  74. codeUnit == 0x002D ||
  75. codeUnit == 0x005F ||
  76. codeUnit >= 0x0030 && codeUnit <= 0x0039 ||
  77. codeUnit >= 0x0041 && codeUnit <= 0x005A ||
  78. codeUnit >= 0x0061 && codeUnit <= 0x007A
  79. ) {
  80. // the character itself
  81. result += string.charAt(index);
  82. continue;
  83. }
  84. // Otherwise, the escaped character.
  85. // https://drafts.csswg.org/cssom/#escape-a-character
  86. result += '\\' + string.charAt(index);
  87. }
  88. return result;
  89. };
  90. if (!root.CSS) {
  91. root.CSS = {};
  92. }
  93. root.CSS.escape = cssEscape;
  94. return cssEscape;
  95. }));