| 21 | var regex = /(\0)|^(-)$|([\x01-\x1f\x7f]|^-?[0-9])|([\x80-\uffff0-9a-zA-Z_-]+)|[^]/g; |
| 22 | |
| 23 | function escaper(match, nil, dash, hexEscape, chars) { |
| 24 | // Chars is the legal-chars (group 4) capture |
| 25 | if (chars) { |
| 26 | return chars; |
| 27 | } |
| 28 | // Nil is the null terminator (group 1) capture |
| 29 | if (nil) { |
| 30 | return '\uFFFD'; |
| 31 | } |
| 32 | // Both UTF control chars, and leading numbers (with optional leading dash) |
| 33 | // (group 3) must be backslash escaped with a trailing space. Funnily, the |
| 34 | // leading dash must not be escaped, but the number. :shrug: |
| 35 | if (hexEscape) { |
| 36 | return match.slice(0, -1) + '\\' + match.slice(-1).charCodeAt(0).toString(16) + ' ' |
| 37 | } |
| 38 | // Finally, the solitary dash and the catch-all chars require backslash |
| 39 | // escaping. |
| 40 | return '\\' + match; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * https://drafts.csswg.org/cssom/#serialize-an-identifier |