(value: string)
| 1 | // https://drafts.csswg.org/cssom/#serialize-an-identifier |
| 2 | export function escape(value: string) { |
| 3 | if (arguments.length === 0) { |
| 4 | throw new TypeError('`CSS.escape` requires an argument.') |
| 5 | } |
| 6 | let string = String(value) |
| 7 | let length = string.length |
| 8 | let index = -1 |
| 9 | let codeUnit: number |
| 10 | let result = '' |
| 11 | let firstCodeUnit = string.charCodeAt(0) |
| 12 | |
| 13 | if ( |
| 14 | // If the character is the first character and is a `-` (U+002D), and |
| 15 | // there is no second character, […] |
| 16 | length === 1 && |
| 17 | firstCodeUnit === 0x002d |
| 18 | ) { |
| 19 | return '\\' + string |
| 20 | } |
| 21 | |
| 22 | while (++index < length) { |
| 23 | codeUnit = string.charCodeAt(index) |
| 24 | // Note: there’s no need to special-case astral symbols, surrogate |
| 25 | // pairs, or lone surrogates. |
| 26 | |
| 27 | // If the character is NULL (U+0000), then the REPLACEMENT CHARACTER |
| 28 | // (U+FFFD). |
| 29 | if (codeUnit === 0x0000) { |
| 30 | result += '\uFFFD' |
| 31 | continue |
| 32 | } |
| 33 | |
| 34 | if ( |
| 35 | // If the character is in the range [\1-\1F] (U+0001 to U+001F) or is |
| 36 | // U+007F, […] |
| 37 | (codeUnit >= 0x0001 && codeUnit <= 0x001f) || |
| 38 | codeUnit === 0x007f || |
| 39 | // If the character is the first character and is in the range [0-9] |
| 40 | // (U+0030 to U+0039), […] |
| 41 | (index === 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) || |
| 42 | // If the character is the second character and is in the range [0-9] |
| 43 | // (U+0030 to U+0039) and the first character is a `-` (U+002D), […] |
| 44 | (index === 1 && codeUnit >= 0x0030 && codeUnit <= 0x0039 && firstCodeUnit === 0x002d) |
| 45 | ) { |
| 46 | // https://drafts.csswg.org/cssom/#escape-a-character-as-code-point |
| 47 | result += '\\' + codeUnit.toString(16) + ' ' |
| 48 | continue |
| 49 | } |
| 50 | |
| 51 | // If the character is not handled by one of the above rules and is |
| 52 | // greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or |
| 53 | // is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to |
| 54 | // U+005A), or [a-z] (U+0061 to U+007A), […] |
| 55 | if ( |
| 56 | codeUnit >= 0x0080 || |
| 57 | codeUnit === 0x002d || |
| 58 | codeUnit === 0x005f || |
| 59 | (codeUnit >= 0x0030 && codeUnit <= 0x0039) || |
| 60 | (codeUnit >= 0x0041 && codeUnit <= 0x005a) || |
no outgoing calls
no test coverage detected