Skip to content

Commit 259bbb9

Browse files
committed
Improve canonicalization for & > :pseudo and & :pseudo arbitrary variants
1 parent 4055cfa commit 259bbb9

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/tailwindcss/src/canonicalize-candidates.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,11 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
857857
['[&:first-child]:flex', 'first:flex'],
858858
['[&:not(:first-child)]:flex', 'not-first:flex'],
859859

860+
['[&_:first-child]:flex', '**:first:flex'],
861+
['[&_>_:first-child]:flex', '*:first:flex'],
862+
['[&_:--custom]:flex', '**:[:--custom]:flex'],
863+
['[&_>_:--custom]:flex', '*:[:--custom]:flex'],
864+
860865
// in-* variants
861866
['[p_&]:flex', 'in-[p]:flex'],
862867
['[.foo_&]:flex', 'in-[.foo]:flex'],

packages/tailwindcss/src/canonicalize-candidates.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ function modernizeArbitraryValuesVariant(
16101610
ast[1].kind === 'combinator' &&
16111611
ast[1].value.trim() === '>' &&
16121612
ast[2].kind === 'selector' &&
1613-
isAttributeSelector(ast[2])
1613+
(isAttributeSelector(ast[2]) || ast[2].value[0] === ':')
16141614
) {
16151615
ast = [ast[2]]
16161616
prefixedVariant = designSystem.parseVariant('*')
@@ -1628,7 +1628,7 @@ function modernizeArbitraryValuesVariant(
16281628
ast[1].kind === 'combinator' &&
16291629
ast[1].value.trim() === '' && // space, but trimmed because there could be multiple spaces
16301630
ast[2].kind === 'selector' &&
1631-
isAttributeSelector(ast[2])
1631+
(isAttributeSelector(ast[2]) || ast[2].value[0] === ':')
16321632
) {
16331633
ast = [ast[2]]
16341634
prefixedVariant = designSystem.parseVariant('**')
@@ -1742,7 +1742,19 @@ function modernizeArbitraryValuesVariant(
17421742
return null
17431743
})(targetNode.value)
17441744

1745-
if (newVariant === null) continue
1745+
if (newVariant === null) {
1746+
if (prefixedVariant) {
1747+
replaceObject(variant, {
1748+
kind: 'arbitrary',
1749+
selector: target.value,
1750+
relative: false,
1751+
} satisfies Variant)
1752+
1753+
return [prefixedVariant, variant]
1754+
}
1755+
1756+
continue
1757+
}
17461758

17471759
// Add `not-` prefix
17481760
if (compoundNot) newVariant = `not-${newVariant}`

0 commit comments

Comments
 (0)