Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions packages/tailwindcss/src/ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,167 @@ describe('optimization', () => {
return pretty(toCss(handleNesting(ast)))
}

describe('combinators after a pseudo-element', () => {
it('drops a nested rule that puts a combinator after a pseudo-element', async () => {
expect(
optimize(css`
.a::selection {
--x: 1;
}
.a::selection {
& > * {
--x: 2;
}
& .b {
--x: 3;
}
& + .c {
--x: 4;
}
& ~ .d {
--x: 5;
}
}
`),
).toMatchInlineSnapshot(`
"
.a::selection {
--x: 1;
}
"
`)
})

it('drops it for legacy single-colon and functional pseudo-elements', async () => {
expect(
optimize(css`
.a:before {
& > * {
--x: 1;
}
}
.a:first-line {
& > * {
--x: 2;
}
}
.a::part(foo) {
& > * {
--x: 3;
}
}
.a ::selection {
& > * {
--x: 4;
}
}
`),
).toMatchInlineSnapshot(`""`)
})

it('keeps pseudo-classes after a pseudo-element', async () => {
expect(
optimize(css`
.a::before {
& {
--x: 0;
}
}
.a::part(foo) {
&:hover {
--x: 1;
}
}
.a::selection {
&:where(.b) {
--x: 2;
}
}
`),
).toMatchInlineSnapshot(`
"
.a::before {
--x: 0;
}
.a::part(foo):hover {
--x: 1;
}
.a::selection:where(.b) {
--x: 2;
}
"
`)
})

it('keeps combinators after pseudo-classes and before pseudo-elements', async () => {
expect(
optimize(css`
.a:hover {
& > * {
--x: 1;
}
}
.a:first-child {
& > * {
--x: 2;
}
}
.a {
& > *::before {
--x: 3;
}
::selection {
--x: 4;
}
}
`),
).toMatchInlineSnapshot(`
"
.a:hover > * {
--x: 1;
}
.a:first-child > * {
--x: 2;
}
.a > ::before {
--x: 3;
}
.a ::selection {
--x: 4;
}
"
`)
})

it('only drops the selectors in a list that put a combinator after a pseudo-element', async () => {
expect(
optimize(css`
.a::before {
& > *,
&:hover {
--x: 1;
}
}
.a {
& ::selection > *,
& > .b {
--x: 2;
}
}
`),
).toMatchInlineSnapshot(`
"
.a::before:hover {
--x: 1;
}
.a > .b {
--x: 2;
}
"
`)
})
})

// See: https://drafts.csswg.org/css-nesting-1/
describe('CSS Nesting Module Level 1', () => {
it('uses the descendant combinator by default', async () => {
Expand Down
43 changes: 40 additions & 3 deletions packages/tailwindcss/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,19 @@ export function handleNesting(ast: AstNode[]): AstNode[] {
}

let lastSelector = selectorStack[selectorStack.length - 1][0]
let selector = segment(node.selector, ',')
let selectors = segment(node.selector, ',')
.map((selector) => substituteNestingSelector(selector, lastSelector))
.join(', ')
selectorStack.push([selector, node.src, node.dst])
.filter((selector) => !hasCombinatorAfterPseudoElement(selector))

// Every selector puts a combinator after a pseudo-element, e.g.
// `selection:*` on a utility with a nested `& > *` rule. The
// result matches nothing and is invalid CSS, so drop the rule.
if (selectors.length === 0) {
skipExit.add(node)
return WalkAction.Skip
}

selectorStack.push([selectors.join(', '), node.src, node.dst])
}

// Once we hit a rule that has at least one declaration, then we can
Expand Down Expand Up @@ -1344,6 +1353,27 @@ export function handleNesting(ast: AstNode[]): AstNode[] {
}
}

// A pseudo-element can only be followed by pseudo-classes, so a selector
// like `.a::before > *` or `.a ::selection .b` can never match anything.
function hasCombinatorAfterPseudoElement(selector: string): boolean {
let ast = parseSelectorCache.get(selector)
let complexes = ast.length === 1 && ast[0].kind === 'list' ? ast[0].nodes : ast

for (let complex of complexes) {
if (complex.kind !== 'complex') continue

for (let i = 0; i < complex.nodes.length - 1; i++) {
if (complex.nodes[i + 1].kind !== 'combinator') continue

let current = complex.nodes[i]
let parts = current.kind === 'compound' ? current.nodes : [current]
if (parts.some(isPseudoElement)) return true
}
}

return false
}

// Substitutes the `&` selector with the parent selector with `:is(…)` semantics.
//
// If the `&` is not found, we will prepend it using a descendant combinator.
Expand Down Expand Up @@ -1892,3 +1922,10 @@ function isVariableUsed(
function extractKeyframeNames(value: string): string[] {
return value.split(/[\s,]+/)
}

const LEGACY_PSEUDO_ELEMENTS = new Set([':before', ':after', ':first-line', ':first-letter'])

function isPseudoElement(node: SelectorParser.SelectorAstNode): boolean {
if (node.kind !== 'selector' && node.kind !== 'function') return false
return node.value.startsWith('::') || LEGACY_PSEUDO_ELEMENTS.has(node.value.toLowerCase())
}
55 changes: 55 additions & 0 deletions packages/tailwindcss/src/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30274,6 +30274,61 @@ describe('custom utilities', () => {
expect(await run(['example', 'example-foo'], input)).toEqual('')
})

test('nested child rules are dropped under pseudo-element variants', async () => {
let input = css`
@theme reference {
--color-red-500: #ef4444;
}

@utility bg-* {
& > * {
--nearest-bg: --value(--color-*);
}
}

@tailwind utilities;
`

let { build } = await compile(input)

expect(build(['selection:bg-red-500', 'marker:bg-red-500', 'hover:bg-red-500', 'bg-red-500']))
.toMatchInlineSnapshot(`
".bg-red-500 {
background-color: var(--color-red-500, #ef4444);
}
.bg-red-500 > * {
--nearest-bg: var(--color-red-500, #ef4444);
}
.marker\\:bg-red-500 ::marker {
background-color: var(--color-red-500, #ef4444);
}
.marker\\:bg-red-500::marker {
background-color: var(--color-red-500, #ef4444);
}
.marker\\:bg-red-500 ::-webkit-details-marker {
background-color: var(--color-red-500, #ef4444);
}
.marker\\:bg-red-500::-webkit-details-marker {
background-color: var(--color-red-500, #ef4444);
}
.selection\\:bg-red-500 ::selection {
background-color: var(--color-red-500, #ef4444);
}
.selection\\:bg-red-500::selection {
background-color: var(--color-red-500, #ef4444);
}
@media (hover: hover) {
.hover\\:bg-red-500:hover {
background-color: var(--color-red-500, #ef4444);
}
.hover\\:bg-red-500:hover > * {
--nearest-bg: var(--color-red-500, #ef4444);
}
}
"
`)
})

test('functional utilities must resolve at least one `--value(…)`', async () => {
let input = css`
@utility example-* {
Expand Down