|
| 1 | +import type { Config } from 'tailwindcss' |
| 2 | +import { parseCandidate, type Candidate, type Variant } from '../../../../tailwindcss/src/candidate' |
| 3 | +import type { DesignSystem } from '../../../../tailwindcss/src/design-system' |
| 4 | +import { segment } from '../../../../tailwindcss/src/utils/segment' |
| 5 | +import { printCandidate } from '../candidates' |
| 6 | + |
| 7 | +export function arbitraryValueToBareValue( |
| 8 | + designSystem: DesignSystem, |
| 9 | + _userConfig: Config, |
| 10 | + rawCandidate: string, |
| 11 | +): string { |
| 12 | + for (let candidate of parseCandidate(rawCandidate, designSystem)) { |
| 13 | + let clone = structuredClone(candidate) |
| 14 | + let changed = false |
| 15 | + for (let variant of variants(clone)) { |
| 16 | + // Convert `data-[selected]` to `data-selected` |
| 17 | + if ( |
| 18 | + variant.kind === 'functional' && |
| 19 | + variant.root === 'data' && |
| 20 | + variant.value?.kind === 'arbitrary' && |
| 21 | + !variant.value.value.includes('=') |
| 22 | + ) { |
| 23 | + changed = true |
| 24 | + variant.value = { |
| 25 | + kind: 'named', |
| 26 | + value: variant.value.value, |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Convert `aria-[selected="true"]` to `aria-selected` |
| 31 | + else if ( |
| 32 | + variant.kind === 'functional' && |
| 33 | + variant.root === 'aria' && |
| 34 | + variant.value?.kind === 'arbitrary' && |
| 35 | + (variant.value.value.endsWith('=true') || |
| 36 | + variant.value.value.endsWith('="true"') || |
| 37 | + variant.value.value.endsWith("='true'")) |
| 38 | + ) { |
| 39 | + let [key, _value] = segment(variant.value.value, '=') |
| 40 | + if ( |
| 41 | + // aria-[foo~="true"] |
| 42 | + key[key.length - 1] === '~' || |
| 43 | + // aria-[foo|="true"] |
| 44 | + key[key.length - 1] === '|' || |
| 45 | + // aria-[foo^="true"] |
| 46 | + key[key.length - 1] === '^' || |
| 47 | + // aria-[foo$="true"] |
| 48 | + key[key.length - 1] === '$' || |
| 49 | + // aria-[foo*="true"] |
| 50 | + key[key.length - 1] === '*' |
| 51 | + ) { |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + changed = true |
| 56 | + variant.value = { |
| 57 | + kind: 'named', |
| 58 | + value: variant.value.value.slice(0, variant.value.value.indexOf('=')), |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Convert `supports-[gap]` to `supports-gap` |
| 63 | + else if ( |
| 64 | + variant.kind === 'functional' && |
| 65 | + variant.root === 'supports' && |
| 66 | + variant.value?.kind === 'arbitrary' && |
| 67 | + /^[a-z-][a-z0-9-]*$/i.test(variant.value.value) |
| 68 | + ) { |
| 69 | + changed = true |
| 70 | + variant.value = { |
| 71 | + kind: 'named', |
| 72 | + value: variant.value.value, |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return changed ? printCandidate(designSystem, clone) : rawCandidate |
| 78 | + } |
| 79 | + |
| 80 | + return rawCandidate |
| 81 | +} |
| 82 | + |
| 83 | +function* variants(candidate: Candidate) { |
| 84 | + function* inner(variant: Variant): Iterable<Variant> { |
| 85 | + yield variant |
| 86 | + if (variant.kind === 'compound') { |
| 87 | + yield* inner(variant.variant) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + for (let variant of candidate.variants) { |
| 92 | + yield* inner(variant) |
| 93 | + } |
| 94 | +} |
0 commit comments