|
1 |
| -const PATTERNS = [ |
2 |
| - /(?:\['([^'\s]+[^<>"'`\s:\\])')/.source, // ['text-lg' -> text-lg |
3 |
| - /(?:\["([^"\s]+[^<>"'`\s:\\])")/.source, // ["text-lg" -> text-lg |
4 |
| - /(?:\[`([^`\s]+[^<>"'`\s:\\])`)/.source, // [`text-lg` -> text-lg |
5 |
| - /([^${(<>"'`\s]*\[\w*'[^"`\s]*'?\])/.source, // font-['some_font',sans-serif] |
6 |
| - /([^${(<>"'`\s]*\[\w*"[^'`\s]*"?\])/.source, // font-["some_font",sans-serif] |
7 |
| - /([^<>"'`\s]*\[\w*\('[^"'`\s]*'\)\])/.source, // bg-[url('...')] |
8 |
| - /([^<>"'`\s]*\[\w*\("[^"'`\s]*"\)\])/.source, // bg-[url("...")] |
9 |
| - /([^<>"'`\s]*\[\w*\('[^"`\s]*'\)\])/.source, // bg-[url('...'),url('...')] |
10 |
| - /([^<>"'`\s]*\[\w*\("[^'`\s]*"\)\])/.source, // bg-[url("..."),url("...")] |
11 |
| - /([^<>"'`\s]*\[[^<>"'`\s]*\('[^"`\s]*'\)+\])/.source, // h-[calc(100%-theme('spacing.1'))] |
12 |
| - /([^<>"'`\s]*\[[^<>"'`\s]*\("[^'`\s]*"\)+\])/.source, // h-[calc(100%-theme("spacing.1"))] |
13 |
| - /([^${(<>"'`\s]*\['[^"'`\s]*'\])/.source, // `content-['hello']` but not `content-['hello']']` |
14 |
| - /([^${(<>"'`\s]*\["[^"'`\s]*"\])/.source, // `content-["hello"]` but not `content-["hello"]"]` |
15 |
| - /([^<>"'`\s]*\[[^<>"'`\s]*:[^\]\s]*\])/.source, // `[attr:value]` |
16 |
| - /([^<>"'`\s]*\[[^<>"'`\s]*:'[^"'`\s]*'\])/.source, // `[content:'hello']` but not `[content:"hello"]` |
17 |
| - /([^<>"'`\s]*\[[^<>"'`\s]*:"[^"'`\s]*"\])/.source, // `[content:"hello"]` but not `[content:'hello']` |
18 |
| - /([^<>"'`\s]*\[[^"'`\s]+\][^<>"'`\s]*)/.source, // `fill-[#bada55]`, `fill-[#bada55]/50` |
19 |
| - /([^"'`\s]*[^<>"'`\s:\\])/.source, // `<sm:underline`, `md>:font-bold` |
20 |
| - /([^<>"'`\s]*[^"'`\s:\\])/.source, // `px-1.5`, `uppercase` but not `uppercase:` |
21 |
| - |
22 |
| - // Arbitrary properties |
23 |
| - // /([^"\s]*\[[^\s]+?\][^"\s]*)/.source, |
24 |
| - // /([^'\s]*\[[^\s]+?\][^'\s]*)/.source, |
25 |
| - // /([^`\s]*\[[^\s]+?\][^`\s]*)/.source, |
26 |
| -].join('|') |
27 |
| - |
28 |
| -const BROAD_MATCH_GLOBAL_REGEXP = new RegExp(PATTERNS, 'g') |
29 |
| -const INNER_MATCH_GLOBAL_REGEXP = /[^<>"'`\s.(){}[\]#=%$]*[^<>"'`\s.(){}[\]#=%:$]/g |
| 1 | +import * as regex from './regex' |
| 2 | + |
| 3 | +let patterns = Array.from(buildRegExps()) |
30 | 4 |
|
31 | 5 | /**
|
32 | 6 | * @param {string} content
|
33 | 7 | */
|
34 | 8 | export function defaultExtractor(content) {
|
35 |
| - let broadMatches = content.matchAll(BROAD_MATCH_GLOBAL_REGEXP) |
36 |
| - let innerMatches = content.match(INNER_MATCH_GLOBAL_REGEXP) || [] |
37 |
| - let results = [...broadMatches, ...innerMatches].flat().filter((v) => v !== undefined) |
| 9 | + /** @type {(string|string)[]} */ |
| 10 | + let results = [] |
| 11 | + |
| 12 | + for (let pattern of patterns) { |
| 13 | + results.push(...(content.match(pattern) ?? [])) |
| 14 | + } |
| 15 | + |
| 16 | + return results.filter((v) => v !== undefined).map(clipAtBalancedParens) |
| 17 | +} |
| 18 | + |
| 19 | +function* buildRegExps() { |
| 20 | + yield regex.pattern([ |
| 21 | + // Variants |
| 22 | + /((?=([^\s"'\\\[]+:))\2)?/, |
| 23 | + |
| 24 | + // Important (optional) |
| 25 | + /!?/, |
| 26 | + |
| 27 | + regex.any([ |
| 28 | + // Arbitrary properties |
| 29 | + /\[[^\s:'"]+:[^\s\]]+\]/, |
| 30 | + |
| 31 | + // Utilities |
| 32 | + regex.pattern([ |
| 33 | + // Utility Name / Group Name |
| 34 | + /-?(?:\w+)/, |
| 35 | + |
| 36 | + // Normal/Arbitrary values |
| 37 | + regex.optional( |
| 38 | + regex.any([ |
| 39 | + regex.pattern([ |
| 40 | + // Arbitrary values |
| 41 | + /-\[[^\s:]+\]/, |
| 42 | + |
| 43 | + // Not immediately followed by an `{[(` |
| 44 | + /(?![{([]])/, |
| 45 | + |
| 46 | + // optionally followed by an opacity modifier |
| 47 | + /(?:\/[^\s'"\\$]*)?/, |
| 48 | + ]), |
| 49 | + |
| 50 | + regex.pattern([ |
| 51 | + // Arbitrary values |
| 52 | + /-\[[^\s]+\]/, |
| 53 | + |
| 54 | + // Not immediately followed by an `{[(` |
| 55 | + /(?![{([]])/, |
| 56 | + |
| 57 | + // optionally followed by an opacity modifier |
| 58 | + /(?:\/[^\s'"\\$]*)?/, |
| 59 | + ]), |
| 60 | + |
| 61 | + // Normal values w/o quotes — may include an opacity modifier |
| 62 | + /[-\/][^\s'"\\$={]*/, |
| 63 | + ]) |
| 64 | + ), |
| 65 | + ]), |
| 66 | + ]), |
| 67 | + ]) |
| 68 | + |
| 69 | + // 5. Inner matches |
| 70 | + // yield /[^<>"'`\s.(){}[\]#=%$]*[^<>"'`\s.(){}[\]#=%:$]/g |
| 71 | +} |
| 72 | + |
| 73 | +// We want to capture any "special" characters |
| 74 | +// AND the characters immediately following them (if there is one) |
| 75 | +let SPECIALS = /([\[\]'"`])([^\[\]'"`])?/g |
| 76 | +let ALLOWED_CLASS_CHARACTERS = /[^"'`\s<>\]]+/ |
| 77 | + |
| 78 | +/** |
| 79 | + * Clips a string ensuring that parentheses, quotes, etc… are balanced |
| 80 | + * Used for arbitrary values only |
| 81 | + * |
| 82 | + * We will go past the end of the balanced parens until we find a non-class character |
| 83 | + * |
| 84 | + * Depth matching behavior: |
| 85 | + * w-[calc(100%-theme('spacing[some_key][1.5]'))]'] |
| 86 | + * ┬ ┬ ┬┬ ┬ ┬┬ ┬┬┬┬┬┬┬ |
| 87 | + * 1 2 3 4 34 3 210 END |
| 88 | + * ╰────┴──────────┴────────┴────────┴┴───┴─┴┴┴ |
| 89 | + * |
| 90 | + * @param {string} input |
| 91 | + */ |
| 92 | +function clipAtBalancedParens(input) { |
| 93 | + // We are care about this for arbitrary values |
| 94 | + if (!input.includes('-[')) { |
| 95 | + return input |
| 96 | + } |
| 97 | + |
| 98 | + let depth = 0 |
| 99 | + let openStringTypes = [] |
| 100 | + |
| 101 | + // Find all parens, brackets, quotes, etc |
| 102 | + // Stop when we end at a balanced pair |
| 103 | + // This is naive and will treat mismatched parens as balanced |
| 104 | + // This shouldn't be a problem in practice though |
| 105 | + let matches = input.matchAll(SPECIALS) |
| 106 | + |
| 107 | + // We can't use lookbehind assertions because we have to support Safari |
| 108 | + // So, instead, we've emulated it using capture groups and we'll re-work the matches to accommodate |
| 109 | + matches = Array.from(matches).flatMap((match) => { |
| 110 | + const [, ...groups] = match |
| 111 | + |
| 112 | + return groups.map((group, idx) => |
| 113 | + Object.assign([], match, { |
| 114 | + index: match.index + idx, |
| 115 | + 0: group, |
| 116 | + }) |
| 117 | + ) |
| 118 | + }) |
| 119 | + |
| 120 | + for (let match of matches) { |
| 121 | + let char = match[0] |
| 122 | + let inStringType = openStringTypes[openStringTypes.length - 1] |
| 123 | + |
| 124 | + if (char === inStringType) { |
| 125 | + openStringTypes.pop() |
| 126 | + } else if (char === "'" || char === '"' || char === '`') { |
| 127 | + openStringTypes.push(char) |
| 128 | + } |
| 129 | + |
| 130 | + if (inStringType) { |
| 131 | + continue |
| 132 | + } else if (char === '[') { |
| 133 | + depth++ |
| 134 | + continue |
| 135 | + } else if (char === ']') { |
| 136 | + depth-- |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + // We've gone one character past the point where we should stop |
| 141 | + // This means that there was an extra closing `]` |
| 142 | + // We'll clip to just before it |
| 143 | + if (depth < 0) { |
| 144 | + return input.substring(0, match.index) |
| 145 | + } |
| 146 | + |
| 147 | + // We've finished balancing the brackets but there still may be characters that can be included |
| 148 | + // For example in the class `text-[#336699]/[.35]` |
| 149 | + // The depth goes to `0` at the closing `]` but goes up again at the `[` |
| 150 | + |
| 151 | + // If we're at zero and encounter a non-class character then we clip the class there |
| 152 | + if (depth === 0 && !ALLOWED_CLASS_CHARACTERS.test(char)) { |
| 153 | + return input.substring(0, match.index) |
| 154 | + } |
| 155 | + } |
38 | 156 |
|
39 |
| - return results |
| 157 | + return input |
40 | 158 | }
|
41 | 159 |
|
42 | 160 | // Regular utilities
|
|
0 commit comments