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 |
|
---|
16 | if (root.CSS && root.CSS.escape) {
|
---|
17 | return root.CSS.escape;
|
---|
18 | }
|
---|
19 |
|
---|
20 | // https://drafts.csswg.org/cssom/#serialize-an-identifier
|
---|
21 | var cssEscape = function(value) {
|
---|
22 | if (arguments.length == 0) {
|
---|
23 | throw new TypeError('`CSS.escape` requires an argument.');
|
---|
24 | }
|
---|
25 | var string = String(value);
|
---|
26 | var length = string.length;
|
---|
27 | var index = -1;
|
---|
28 | var codeUnit;
|
---|
29 | var result = '';
|
---|
30 | var firstCodeUnit = string.charCodeAt(0);
|
---|
31 | while (++index < length) {
|
---|
32 | codeUnit = string.charCodeAt(index);
|
---|
33 | // Note: there’s no need to special-case astral symbols, surrogate
|
---|
34 | // pairs, or lone surrogates.
|
---|
35 |
|
---|
36 | // If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
|
---|
37 | // (U+FFFD).
|
---|
38 | if (codeUnit == 0x0000) {
|
---|
39 | result += '\uFFFD';
|
---|
40 | continue;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (
|
---|
44 | // If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
|
---|
45 | // U+007F, […]
|
---|
46 | (codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F ||
|
---|
47 | // If the character is the first character and is in the range [0-9]
|
---|
48 | // (U+0030 to U+0039), […]
|
---|
49 | (index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
|
---|
50 | // If the character is the second character and is in the range [0-9]
|
---|
51 | // (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
|
---|
52 | (
|
---|
53 | index == 1 &&
|
---|
54 | codeUnit >= 0x0030 && codeUnit <= 0x0039 &&
|
---|
55 | firstCodeUnit == 0x002D
|
---|
56 | )
|
---|
57 | ) {
|
---|
58 | // https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
|
---|
59 | result += '\\' + codeUnit.toString(16) + ' ';
|
---|
60 | continue;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (
|
---|
64 | // If the character is the first character and is a `-` (U+002D), and
|
---|
65 | // there is no second character, […]
|
---|
66 | index == 0 &&
|
---|
67 | length == 1 &&
|
---|
68 | codeUnit == 0x002D
|
---|
69 | ) {
|
---|
70 | result += '\\' + string.charAt(index);
|
---|
71 | continue;
|
---|
72 | }
|
---|
73 |
|
---|
74 | // If the character is not handled by one of the above rules and is
|
---|
75 | // greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
|
---|
76 | // is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
|
---|
77 | // U+005A), or [a-z] (U+0061 to U+007A), […]
|
---|
78 | if (
|
---|
79 | codeUnit >= 0x0080 ||
|
---|
80 | codeUnit == 0x002D ||
|
---|
81 | codeUnit == 0x005F ||
|
---|
82 | codeUnit >= 0x0030 && codeUnit <= 0x0039 ||
|
---|
83 | codeUnit >= 0x0041 && codeUnit <= 0x005A ||
|
---|
84 | codeUnit >= 0x0061 && codeUnit <= 0x007A
|
---|
85 | ) {
|
---|
86 | // the character itself
|
---|
87 | result += string.charAt(index);
|
---|
88 | continue;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // Otherwise, the escaped character.
|
---|
92 | // https://drafts.csswg.org/cssom/#escape-a-character
|
---|
93 | result += '\\' + string.charAt(index);
|
---|
94 |
|
---|
95 | }
|
---|
96 | return result;
|
---|
97 | };
|
---|
98 |
|
---|
99 | if (!root.CSS) {
|
---|
100 | root.CSS = {};
|
---|
101 | }
|
---|
102 |
|
---|
103 | root.CSS.escape = cssEscape;
|
---|
104 | return cssEscape;
|
---|
105 |
|
---|
106 | }));
|
---|