|
1 | 1 | /*\ |
2 | 2 | title: $:/core/modules/utils/escapecss.js |
3 | 3 | type: application/javascript |
4 | | -module-type: utils |
| 4 | +module-type: utils-browser |
5 | 5 |
|
6 | 6 | Provides CSS.escape() functionality. |
7 | 7 |
|
8 | 8 | \*/ |
9 | 9 |
|
10 | 10 | "use strict"; |
11 | 11 |
|
12 | | -// TODO -- resolve this construction |
13 | 12 | exports.escapeCSS = (function() { |
14 | | - // use browser's native CSS.escape() function if available |
15 | | - if ($tw.browser && window.CSS && window.CSS.escape) { |
16 | | - return window.CSS.escape; |
17 | | - } |
18 | | - |
19 | | - // otherwise, a utility method is provided |
20 | | - // see also https://drafts.csswg.org/cssom/#serialize-an-identifier |
21 | | - |
22 | | - /*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */ |
23 | | - return function(value) { |
24 | | - if (arguments.length == 0) { |
25 | | - throw new TypeError('`CSS.escape` requires an argument.'); |
26 | | - } |
27 | | - var string = String(value); |
28 | | - var length = string.length; |
29 | | - var index = -1; |
30 | | - var codeUnit; |
31 | | - var result = ''; |
32 | | - var firstCodeUnit = string.charCodeAt(0); |
33 | | - while (++index < length) { |
34 | | - codeUnit = string.charCodeAt(index); |
35 | | - // Note: there’s no need to special-case astral symbols, surrogate |
36 | | - // pairs, or lone surrogates. |
37 | | - |
38 | | - // If the character is NULL (U+0000), then the REPLACEMENT CHARACTER |
39 | | - // (U+FFFD). |
40 | | - if (codeUnit == 0x0000) { |
41 | | - result += '\uFFFD'; |
42 | | - continue; |
43 | | - } |
44 | | - |
45 | | - if ( |
46 | | - // If the character is in the range [\1-\1F] (U+0001 to U+001F) or is |
47 | | - // U+007F, […] |
48 | | - (codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F || |
49 | | - // If the character is the first character and is in the range [0-9] |
50 | | - // (U+0030 to U+0039), […] |
51 | | - (index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) || |
52 | | - // If the character is the second character and is in the range [0-9] |
53 | | - // (U+0030 to U+0039) and the first character is a `-` (U+002D), […] |
54 | | - ( |
55 | | - index == 1 && |
56 | | - codeUnit >= 0x0030 && codeUnit <= 0x0039 && |
57 | | - firstCodeUnit == 0x002D |
58 | | - ) |
59 | | - ) { |
60 | | - // https://drafts.csswg.org/cssom/#escape-a-character-as-code-point |
61 | | - result += '\\' + codeUnit.toString(16) + ' '; |
62 | | - continue; |
63 | | - } |
64 | | - |
65 | | - if ( |
66 | | - // If the character is the first character and is a `-` (U+002D), and |
67 | | - // there is no second character, […] |
68 | | - index == 0 && |
69 | | - length == 1 && |
70 | | - codeUnit == 0x002D |
71 | | - ) { |
72 | | - result += '\\' + string.charAt(index); |
73 | | - continue; |
74 | | - } |
75 | | - |
76 | | - // If the character is not handled by one of the above rules and is |
77 | | - // greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or |
78 | | - // is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to |
79 | | - // U+005A), or [a-z] (U+0061 to U+007A), […] |
80 | | - if ( |
81 | | - codeUnit >= 0x0080 || |
82 | | - codeUnit == 0x002D || |
83 | | - codeUnit == 0x005F || |
84 | | - codeUnit >= 0x0030 && codeUnit <= 0x0039 || |
85 | | - codeUnit >= 0x0041 && codeUnit <= 0x005A || |
86 | | - codeUnit >= 0x0061 && codeUnit <= 0x007A |
87 | | - ) { |
88 | | - // the character itself |
89 | | - result += string.charAt(index); |
90 | | - continue; |
91 | | - } |
92 | | - |
93 | | - // Otherwise, the escaped character. |
94 | | - // https://drafts.csswg.org/cssom/#escape-a-character |
95 | | - result += '\\' + string.charAt(index); |
96 | | - |
97 | | - } |
98 | | - return result; |
99 | | - }; |
| 13 | + return window.CSS.escape; |
100 | 14 | })(); |
0 commit comments