|
| 1 | +/* eslint-disable regexp/control-character-escape */ |
| 2 | +/* eslint-disable no-control-regex */ |
| 3 | +/* eslint-disable regexp/no-optional-assertion */ |
| 4 | +/* eslint-disable regexp/no-useless-escape */ |
| 5 | +/* eslint-disable regexp/no-obscure-range */ |
| 6 | +// ESM vendored version of cssesc: https://github.com/mathiasbynens/cssesc/blob/cb894eb42f27c8d3cd793f16afe35b3ab38000a1/cssesc.js |
| 7 | +// See https://github.com/vanilla-extract-css/vanilla-extract/pull/1710 |
| 8 | + |
| 9 | +const regexAnySingleEscape = /[ -,\.\/:-@\[-\^`\{-~]/; |
| 10 | +const regexSingleEscape = /[ -,\.\/:-@\[\]\^`\{-~]/; |
| 11 | +const regexExcessiveSpaces = |
| 12 | + /(^|\\+)?(\\[A-F0-9]{1,6})\x20(?![a-fA-F0-9\x20])/g; |
| 13 | + |
| 14 | +interface Options { |
| 15 | + escapeEverything: boolean; |
| 16 | + isIdentifier: boolean; |
| 17 | + quotes: 'single' | 'double'; |
| 18 | + wrap: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +const DEFAULT_OPTIONS: Options = { |
| 22 | + escapeEverything: false, |
| 23 | + isIdentifier: false, |
| 24 | + quotes: 'single', |
| 25 | + wrap: false, |
| 26 | +}; |
| 27 | + |
| 28 | +export function cssesc(string: string, options: Partial<Options> = {}) { |
| 29 | + options = { ...DEFAULT_OPTIONS, ...options }; |
| 30 | + const quote = options.quotes === 'double' ? '"' : "'"; |
| 31 | + const { isIdentifier } = options; |
| 32 | + |
| 33 | + const firstChar = string.charAt(0); |
| 34 | + let output = ''; |
| 35 | + let counter = 0; |
| 36 | + const length = string.length; |
| 37 | + while (counter < length) { |
| 38 | + const character = string.charAt(counter++); |
| 39 | + let codePoint = character.charCodeAt(0); |
| 40 | + let value: string; |
| 41 | + // If it’s not a printable ASCII character… |
| 42 | + if (codePoint < 0x20 || codePoint > 0x7e) { |
| 43 | + if (codePoint >= 0xd800 && codePoint <= 0xdbff && counter < length) { |
| 44 | + // It’s a high surrogate, and there is a next character. |
| 45 | + const extra = string.charCodeAt(counter++); |
| 46 | + if ((extra & 0xfc00) === 0xdc00) { |
| 47 | + // next character is low surrogate |
| 48 | + codePoint = ((codePoint & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000; |
| 49 | + } else { |
| 50 | + // It’s an unmatched surrogate; only append this code unit, in case |
| 51 | + // the next code unit is the high surrogate of a surrogate pair. |
| 52 | + counter--; |
| 53 | + } |
| 54 | + } |
| 55 | + value = '\\' + codePoint.toString(16).toUpperCase() + ' '; |
| 56 | + } else { |
| 57 | + if (options.escapeEverything) { |
| 58 | + if (regexAnySingleEscape.test(character)) { |
| 59 | + value = '\\' + character; |
| 60 | + } else { |
| 61 | + value = '\\' + codePoint.toString(16).toUpperCase() + ' '; |
| 62 | + } |
| 63 | + } else if (/[\t\n\f\r\x0B]/.test(character)) { |
| 64 | + value = '\\' + codePoint.toString(16).toUpperCase() + ' '; |
| 65 | + } else if ( |
| 66 | + character === '\\' || |
| 67 | + (!isIdentifier && |
| 68 | + ((character === '"' && quote === character) || |
| 69 | + (character === "'" && quote === character))) || |
| 70 | + (isIdentifier && regexSingleEscape.test(character)) |
| 71 | + ) { |
| 72 | + value = '\\' + character; |
| 73 | + } else { |
| 74 | + value = character; |
| 75 | + } |
| 76 | + } |
| 77 | + output += value; |
| 78 | + } |
| 79 | + |
| 80 | + if (isIdentifier) { |
| 81 | + if (/^-[-\d]/.test(output)) { |
| 82 | + output = '\\-' + output.slice(1); |
| 83 | + } else if (/\d/.test(firstChar)) { |
| 84 | + output = '\\3' + firstChar + ' ' + output.slice(1); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Remove spaces after `\HEX` escapes that are not followed by a hex digit, |
| 89 | + // since they’re redundant. Note that this is only possible if the escape |
| 90 | + // sequence isn’t preceded by an odd number of backslashes. |
| 91 | + output = output.replace(regexExcessiveSpaces, function ($0, $1, $2) { |
| 92 | + if ($1 && $1.length % 2) { |
| 93 | + // It’s not safe to remove the space, so don’t. |
| 94 | + return $0; |
| 95 | + } |
| 96 | + // Strip the space. |
| 97 | + return ($1 || '') + $2; |
| 98 | + }); |
| 99 | + |
| 100 | + if (!isIdentifier && options.wrap) { |
| 101 | + return quote + output + quote; |
| 102 | + } |
| 103 | + return output; |
| 104 | +} |
0 commit comments