|
| 1 | +import SelectorParser from 'postcss-selector-parser' |
| 2 | +import type { Config } from 'tailwindcss' |
| 3 | +import { parseCandidate, type Candidate, type Variant } from '../../../../tailwindcss/src/candidate' |
| 4 | +import type { DesignSystem } from '../../../../tailwindcss/src/design-system' |
| 5 | +import { isPositiveInteger } from '../../../../tailwindcss/src/utils/infer-data-type' |
| 6 | +import { printCandidate } from '../candidates' |
| 7 | + |
| 8 | +export function modernizeArbitraryValues( |
| 9 | + designSystem: DesignSystem, |
| 10 | + _userConfig: Config, |
| 11 | + rawCandidate: string, |
| 12 | +): string { |
| 13 | + for (let candidate of parseCandidate(rawCandidate, designSystem)) { |
| 14 | + let clone = structuredClone(candidate) |
| 15 | + let changed = false |
| 16 | + |
| 17 | + for (let [variant, parent] of variants(clone)) { |
| 18 | + // Expecting an arbitrary variant |
| 19 | + if (variant.kind !== 'arbitrary') continue |
| 20 | + |
| 21 | + // Expecting a non-relative arbitrary variant |
| 22 | + if (variant.relative) continue |
| 23 | + |
| 24 | + let ast = SelectorParser().astSync(variant.selector) |
| 25 | + |
| 26 | + // Expecting a single selector node |
| 27 | + if (ast.nodes.length !== 1) continue |
| 28 | + |
| 29 | + let prefixedVariant: Variant | null = null |
| 30 | + |
| 31 | + // Track whether we need to add a `**:` variant |
| 32 | + let addStarStarVariant = false |
| 33 | + |
| 34 | + // Handling a child combinator. E.g.: `[&>[data-visible]]` => `*:data-visible` |
| 35 | + if ( |
| 36 | + // Only top-level, so `has-[&>[data-visible]]` is not supported |
| 37 | + parent === null && |
| 38 | + // [&_>_[data-visible]]:flex |
| 39 | + // ^ ^ ^^^^^^^^^^^^^^ |
| 40 | + ast.nodes[0].length === 3 && |
| 41 | + ast.nodes[0].nodes[0].type === 'nesting' && |
| 42 | + ast.nodes[0].nodes[0].value === '&' && |
| 43 | + ast.nodes[0].nodes[1].type === 'combinator' && |
| 44 | + ast.nodes[0].nodes[1].value === '>' && |
| 45 | + ast.nodes[0].nodes[2].type === 'attribute' |
| 46 | + ) { |
| 47 | + ast.nodes[0].nodes = [ast.nodes[0].nodes[2]] |
| 48 | + prefixedVariant = designSystem.parseVariant('*') |
| 49 | + } |
| 50 | + |
| 51 | + // Handling a grand child combinator. E.g.: `[&_[data-visible]]` => `**:data-visible` |
| 52 | + if ( |
| 53 | + // Only top-level, so `has-[&_[data-visible]]` is not supported |
| 54 | + parent === null && |
| 55 | + // [&_[data-visible]]:flex |
| 56 | + // ^ ^^^^^^^^^^^^^^ |
| 57 | + ast.nodes[0].length === 3 && |
| 58 | + ast.nodes[0].nodes[0].type === 'nesting' && |
| 59 | + ast.nodes[0].nodes[0].value === '&' && |
| 60 | + ast.nodes[0].nodes[1].type === 'combinator' && |
| 61 | + ast.nodes[0].nodes[1].value === ' ' && |
| 62 | + ast.nodes[0].nodes[2].type === 'attribute' |
| 63 | + ) { |
| 64 | + ast.nodes[0].nodes = [ast.nodes[0].nodes[2]] |
| 65 | + prefixedVariant = designSystem.parseVariant('**') |
| 66 | + } |
| 67 | + |
| 68 | + // Filter out `&`. E.g.: `&[data-foo]` => `[data-foo]` |
| 69 | + let selectorNodes = ast.nodes[0].filter((node) => node.type !== 'nesting') |
| 70 | + |
| 71 | + // Expecting a single selector (normal selector or attribute selector) |
| 72 | + if (selectorNodes.length !== 1) continue |
| 73 | + |
| 74 | + let target = selectorNodes[0] |
| 75 | + if (target.type === 'pseudo' && target.value === ':is') { |
| 76 | + // Expecting a single selector node |
| 77 | + if (target.nodes.length !== 1) continue |
| 78 | + |
| 79 | + // Expecting a single attribute selector |
| 80 | + if (target.nodes[0].nodes.length !== 1) continue |
| 81 | + |
| 82 | + // Unwrap the selector from inside `&:is(…)` |
| 83 | + target = target.nodes[0].nodes[0] |
| 84 | + } |
| 85 | + |
| 86 | + // Expecting a pseudo selector |
| 87 | + if (target.type === 'pseudo') { |
| 88 | + let targetNode = target |
| 89 | + let compoundNot = false |
| 90 | + if (target.value === ':not') { |
| 91 | + compoundNot = true |
| 92 | + if (target.nodes.length !== 1) continue |
| 93 | + if (target.nodes[0].type !== 'selector') continue |
| 94 | + if (target.nodes[0].nodes.length !== 1) continue |
| 95 | + if (target.nodes[0].nodes[0].type !== 'pseudo') continue |
| 96 | + |
| 97 | + targetNode = target.nodes[0].nodes[0] |
| 98 | + } |
| 99 | + |
| 100 | + let newVariant = ((value) => { |
| 101 | + // |
| 102 | + if (value === ':first-letter') return 'first-letter' |
| 103 | + else if (value === ':first-line') return 'first-line' |
| 104 | + // |
| 105 | + else if (value === ':file-selector-button') return 'file' |
| 106 | + else if (value === ':placeholder') return 'placeholder' |
| 107 | + else if (value === ':backdrop') return 'backdrop' |
| 108 | + // Positional |
| 109 | + else if (value === ':first-child') return 'first' |
| 110 | + else if (value === ':last-child') return 'last' |
| 111 | + else if (value === ':only-child') return 'only' |
| 112 | + else if (value === ':first-of-type') return 'first-of-type' |
| 113 | + else if (value === ':last-of-type') return 'last-of-type' |
| 114 | + else if (value === ':only-of-type') return 'only-of-type' |
| 115 | + // State |
| 116 | + else if (value === ':visited') return 'visited' |
| 117 | + else if (value === ':target') return 'target' |
| 118 | + // Forms |
| 119 | + else if (value === ':default') return 'default' |
| 120 | + else if (value === ':checked') return 'checked' |
| 121 | + else if (value === ':indeterminate') return 'indeterminate' |
| 122 | + else if (value === ':placeholder-shown') return 'placeholder-shown' |
| 123 | + else if (value === ':autofill') return 'autofill' |
| 124 | + else if (value === ':optional') return 'optional' |
| 125 | + else if (value === ':required') return 'required' |
| 126 | + else if (value === ':valid') return 'valid' |
| 127 | + else if (value === ':invalid') return 'invalid' |
| 128 | + else if (value === ':in-range') return 'in-range' |
| 129 | + else if (value === ':out-of-range') return 'out-of-range' |
| 130 | + else if (value === ':read-only') return 'read-only' |
| 131 | + // Content |
| 132 | + else if (value === ':empty') return 'empty' |
| 133 | + // Interactive |
| 134 | + else if (value === ':focus-within') return 'focus-within' |
| 135 | + else if (value === ':focus') return 'focus' |
| 136 | + else if (value === ':focus-visible') return 'focus-visible' |
| 137 | + else if (value === ':active') return 'active' |
| 138 | + else if (value === ':enabled') return 'enabled' |
| 139 | + else if (value === ':disabled') return 'disabled' |
| 140 | + // |
| 141 | + if ( |
| 142 | + value === ':nth-child' && |
| 143 | + targetNode.nodes.length === 1 && |
| 144 | + targetNode.nodes[0].nodes.length === 1 && |
| 145 | + targetNode.nodes[0].nodes[0].type === 'tag' && |
| 146 | + targetNode.nodes[0].nodes[0].value === 'odd' |
| 147 | + ) { |
| 148 | + if (compoundNot) { |
| 149 | + compoundNot = false |
| 150 | + return 'even' |
| 151 | + } |
| 152 | + return 'odd' |
| 153 | + } |
| 154 | + if ( |
| 155 | + value === ':nth-child' && |
| 156 | + targetNode.nodes.length === 1 && |
| 157 | + targetNode.nodes[0].nodes.length === 1 && |
| 158 | + targetNode.nodes[0].nodes[0].type === 'tag' && |
| 159 | + targetNode.nodes[0].nodes[0].value === 'even' |
| 160 | + ) { |
| 161 | + if (compoundNot) { |
| 162 | + compoundNot = false |
| 163 | + return 'odd' |
| 164 | + } |
| 165 | + return 'even' |
| 166 | + } |
| 167 | + |
| 168 | + for (let [selector, variantName] of [ |
| 169 | + [':nth-child', 'nth'], |
| 170 | + [':nth-last-child', 'nth-last'], |
| 171 | + [':nth-of-type', 'nth-of-type'], |
| 172 | + [':nth-last-of-type', 'nth-of-last-type'], |
| 173 | + ]) { |
| 174 | + if (value === selector && targetNode.nodes.length === 1) { |
| 175 | + if ( |
| 176 | + targetNode.nodes[0].nodes.length === 1 && |
| 177 | + targetNode.nodes[0].nodes[0].type === 'tag' && |
| 178 | + isPositiveInteger(targetNode.nodes[0].nodes[0].value) |
| 179 | + ) { |
| 180 | + return `${variantName}-${targetNode.nodes[0].nodes[0].value}` |
| 181 | + } |
| 182 | + |
| 183 | + return `${variantName}-[${targetNode.nodes[0].toString()}]` |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return null |
| 188 | + })(targetNode.value) |
| 189 | + |
| 190 | + if (newVariant === null) continue |
| 191 | + |
| 192 | + // Add `not-` prefix |
| 193 | + if (compoundNot) newVariant = `not-${newVariant}` |
| 194 | + |
| 195 | + let parsed = designSystem.parseVariant(newVariant) |
| 196 | + if (parsed === null) continue |
| 197 | + |
| 198 | + // Update original variant |
| 199 | + changed = true |
| 200 | + Object.assign(variant, parsed) |
| 201 | + } |
| 202 | + |
| 203 | + // Expecting an attribute selector |
| 204 | + else if (target.type === 'attribute') { |
| 205 | + // Attribute selectors |
| 206 | + let attributeKey = target.attribute |
| 207 | + let attributeValue = target.value |
| 208 | + ? target.quoted |
| 209 | + ? `${target.quoteMark}${target.value}${target.quoteMark}` |
| 210 | + : target.value |
| 211 | + : null |
| 212 | + |
| 213 | + // Insensitive attribute selectors. E.g.: `[data-foo="value" i]` |
| 214 | + // ^ |
| 215 | + if (target.insensitive && attributeValue) { |
| 216 | + attributeValue += ' i' |
| 217 | + } |
| 218 | + |
| 219 | + let operator = target.operator ?? '=' |
| 220 | + |
| 221 | + // Migrate `data-*` |
| 222 | + if (attributeKey.startsWith('data-')) { |
| 223 | + changed = true |
| 224 | + attributeKey = attributeKey.slice(5) // Remove `data-` |
| 225 | + Object.assign(variant, { |
| 226 | + kind: 'functional', |
| 227 | + root: 'data', |
| 228 | + modifier: null, |
| 229 | + value: |
| 230 | + attributeValue === null |
| 231 | + ? { kind: 'named', value: attributeKey } |
| 232 | + : { kind: 'arbitrary', value: `${attributeKey}${operator}${attributeValue}` }, |
| 233 | + } satisfies Variant) |
| 234 | + } |
| 235 | + |
| 236 | + // Migrate `aria-*` |
| 237 | + else if (attributeKey.startsWith('aria-')) { |
| 238 | + changed = true |
| 239 | + attributeKey = attributeKey.slice(5) // Remove `aria-` |
| 240 | + Object.assign(variant, { |
| 241 | + kind: 'functional', |
| 242 | + root: 'aria', |
| 243 | + modifier: null, |
| 244 | + value: |
| 245 | + attributeValue === null |
| 246 | + ? { kind: 'arbitrary', value: attributeKey } // aria-[foo] |
| 247 | + : operator === '=' && target.value === 'true' && !target.insensitive |
| 248 | + ? { kind: 'named', value: attributeKey } // aria-[foo="true"] or aria-[foo='true'] or aria-[foo=true] |
| 249 | + : { kind: 'arbitrary', value: `${attributeKey}${operator}${attributeValue}` }, // aria-[foo~="true"], aria-[foo|="true"], … |
| 250 | + } satisfies Variant) |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + if (prefixedVariant) { |
| 255 | + let idx = clone.variants.indexOf(variant) |
| 256 | + if (idx === -1) continue |
| 257 | + |
| 258 | + // Ensure we inject the prefixed variant |
| 259 | + clone.variants.splice(idx, 1, variant, prefixedVariant) |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + return changed ? printCandidate(designSystem, clone) : rawCandidate |
| 264 | + } |
| 265 | + |
| 266 | + return rawCandidate |
| 267 | +} |
| 268 | + |
| 269 | +function* variants(candidate: Candidate) { |
| 270 | + function* inner( |
| 271 | + variant: Variant, |
| 272 | + parent: Extract<Variant, { kind: 'compound' }> | null = null, |
| 273 | + ): Iterable<[Variant, Extract<Variant, { kind: 'compound' }> | null]> { |
| 274 | + yield [variant, parent] |
| 275 | + |
| 276 | + if (variant.kind === 'compound') { |
| 277 | + yield* inner(variant.variant, variant) |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + for (let variant of candidate.variants) { |
| 282 | + yield* inner(variant, null) |
| 283 | + } |
| 284 | +} |
0 commit comments