From 16cda09b13e6a9136489e9fc1ad05ce2e3a6f81b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoi=CC=82t=20Rouleau?= Date: Fri, 13 Nov 2026 00:21:53 -0500 Subject: [PATCH 1/7] Don't treat different `@scope` variants as the same when canonicalizing Variants that generate `@scope` rules wrap them in `context` nodes since #20369, and `toCss` prints `context` nodes as an empty string. So every `@scope` variant had the same signature, and canonicalization rewrote them to whichever `@scope` variant came first. Unwrap the `context` nodes before computing the signature. Fixes #20714 Co-Authored-By: Claude Opus 5.5 --- .changelog/canonicalize-scope-variants.md | 3 +++ .../src/canonicalize-candidates.test.ts | 23 +++++++++++++++++++ .../src/canonicalize-candidates.ts | 9 +++++++- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 .changelog/canonicalize-scope-variants.md diff --git a/.changelog/canonicalize-scope-variants.md b/.changelog/canonicalize-scope-variants.md new file mode 100644 index 000000000000..412ad5a7b691 --- /dev/null +++ b/.changelog/canonicalize-scope-variants.md @@ -0,0 +1,3 @@ +### Fixed + +- Canonicalization: don't replace `@scope` variants with unrelated `@scope` variants (e.g. `[@scope_(.card)]:flex` no longer becomes `in-blue:flex`) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 6f300c2f5abf..627402c70749 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -2100,6 +2100,29 @@ describe('regressions', () => { ]) expect(compare).not.toHaveBeenCalled() }) + + test('does not treat different `@scope` variants as the same', async () => { + let designSystem = await __unstable__loadDesignSystem( + css` + @tailwind utilities; + @custom-variant in-blue { + @scope ([data-theme='blue']) to ([data-theme]) { + @slot; + } + } + `, + { base: __dirname }, + ) + + expect(designSystem.canonicalizeCandidates(['[@scope_(.card)]:flex'])).toEqual([ + '[@scope_(.card)]:flex', + ]) + expect( + designSystem.canonicalizeCandidates([ + "[@scope_([data-theme='blue'])_to_([data-theme])]:flex", + ]), + ).toEqual(['in-blue:flex']) + }) }) describe('normalizeEscapes', () => { diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index d79c498393ad..1e486f335b90 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -3402,8 +3402,15 @@ function createVariantSignatureCache( // Canonicalize selectors to their minimal form walk(ast, (node) => { + // Variants that generate `@scope` rules wrap them in `context` nodes, + // which `toCss` doesn't print. Unwrap them so the signature includes + // the `@scope` rule. + if (node.kind === 'context') { + return WalkAction.Replace(node.nodes) + } + // At-rules - if (node.kind === 'at-rule') { + else if (node.kind === 'at-rule') { // Normalize dimensions such that equivalent values in different // units are considered the same. E.g.: with a root font size of // `16px`, `@media (width >= 64rem)` and `@media (width >= 1024px)` From 21d4689f6364227f9c0d5e0ecf6e04caa24d8b91 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 16 Nov 2026 19:00:32 +0100 Subject: [PATCH 2/7] Keep descendant combinators in at-rule params when canonicalizing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The variant signature removed every space from at-rule params, so `@scope (.foo .bar)` and `@scope (.foo.bar)` got the same signature, and `[@scope_(.foo_.bar)]:flex` was replaced with a variant for `.foo.bar`. `@supports selector(…)` had the same problem. Now only whitespace next to delimiters is removed (e.g. `(width >= 64rem)` → `(width>=64rem)`), and other whitespace is collapsed to a single space. This happens in a single pass that keeps strings and escapes as-is. --- .changelog/canonicalize-scope-variants.md | 1 + .../src/canonicalize-candidates.test.ts | 34 +++++++ .../src/canonicalize-candidates.ts | 90 ++++++++++++++++++- 3 files changed, 124 insertions(+), 1 deletion(-) diff --git a/.changelog/canonicalize-scope-variants.md b/.changelog/canonicalize-scope-variants.md index 412ad5a7b691..89091112002a 100644 --- a/.changelog/canonicalize-scope-variants.md +++ b/.changelog/canonicalize-scope-variants.md @@ -1,3 +1,4 @@ ### Fixed - Canonicalization: don't replace `@scope` variants with unrelated `@scope` variants (e.g. `[@scope_(.card)]:flex` no longer becomes `in-blue:flex`) +- Canonicalization: don't treat `.foo .bar` and `.foo.bar` as the same in at-rule variants (e.g. `@scope (…)` or `@supports selector(…)`) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 627402c70749..e442fcd3b8d8 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -2123,6 +2123,40 @@ describe('regressions', () => { ]), ).toEqual(['in-blue:flex']) }) + + test('does not treat a descendant combinator in an at-rule as a compound selector', async () => { + let designSystem = await __unstable__loadDesignSystem( + css` + @tailwind utilities; + @custom-variant in-foo-bar { + @scope (.foo.bar) { + @slot; + } + } + @custom-variant supports-foo-bar { + @supports selector(.foo.bar) { + @slot; + } + } + `, + { base: __dirname }, + ) + + // `.foo .bar` and `.foo.bar` match different elements + expect( + designSystem.canonicalizeCandidates([ + '[@scope_(.foo_.bar)]:flex', + '[@supports_selector(.foo_.bar)]:flex', + ]), + ).toEqual(['[@scope_(.foo_.bar)]:flex', '[@supports_selector(.foo_.bar)]:flex']) + + expect( + designSystem.canonicalizeCandidates([ + '[@scope_(.foo.bar)]:flex', + '[@supports_selector(.foo.bar)]:flex', + ]), + ).toEqual(['in-foo-bar:flex', 'supports-foo-bar:flex']) + }) }) describe('normalizeEscapes', () => { diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 1e486f335b90..08f046d72999 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -3418,7 +3418,7 @@ function createVariantSignatureCache( node.params = constantFoldDeclaration(node.params, rem) if (node.params.includes(' ')) { - node.params = node.params.replaceAll(' ', '') + node.params = normalizeAtRuleParamsWhitespace(node.params) } } @@ -3501,6 +3501,94 @@ function createVariantSignatureCache( }) } +// Remove whitespace in at-rule params that doesn't change their meaning, in a +// single pass. E.g.: `(width >= 64rem)` → `(width>=64rem)`. +// +// Whitespace between other characters is collapsed to a single space, because +// it can be a descendant combinator. E.g.: `@scope (.foo .bar)` and +// `@scope (.foo.bar)` match different elements. The same goes for whitespace +// before a `:` (`.foo :hover`), unless whitespace follows the `:` as well +// (`(pointer : fine)`). Strings and escapes are kept as-is. +function normalizeAtRuleParamsWhitespace(params: string) { + let result = '' + let pendingSpace = false + let quote = 0 + + for (let i = 0; i < params.length; i++) { + let char = params.charCodeAt(i) + + // Copy strings verbatim, including escapes + if (quote !== 0) { + result += params[i] + if (char === BACKSLASH && i + 1 < params.length) { + result += params[++i] + } else if (char === quote) { + quote = 0 + } + continue + } + + if (isWhitespace(char)) { + // Leading whitespace is dropped + pendingSpace = result.length > 0 + continue + } + + // Only keep whitespace between two characters that need it. Trailing + // whitespace is dropped, because no character follows it. + if (pendingSpace) { + pendingSpace = false + if ( + !dropsWhitespaceAfter(result.charCodeAt(result.length - 1)) && + !dropsWhitespaceBefore(char) && + !(char === COLON && isWhitespace(params.charCodeAt(i + 1))) + ) { + result += ' ' + } + } + + result += params[i] + + if (char === BACKSLASH && i + 1 < params.length) { + result += params[++i] + } else if (char === SINGLE_QUOTE || char === DOUBLE_QUOTE) { + quote = char + } + } + + return result +} + +// `(`, `,`, `:`, `<`, `>`, `=`, `+`, `~` and `/` +function dropsWhitespaceAfter(char: number) { + return ( + char === OPEN_PAREN || + char === 0x2c || + char === COLON || + char === 0x3c || + char === GREATER_THAN || + char === 0x3d || + char === 0x2b || + char === 0x7e || + char === 0x2f + ) +} + +// `(`, `)`, `,`, `<`, `>`, `=`, `+`, `~` and `/` +function dropsWhitespaceBefore(char: number) { + return ( + char === OPEN_PAREN || + char === CLOSE_PAREN || + char === 0x2c || + char === 0x3c || + char === GREATER_THAN || + char === 0x3d || + char === 0x2b || + char === 0x7e || + char === 0x2f + ) +} + export const PRE_COMPUTED_VARIANTS_KEY = Symbol() function createPreComputedVariantsCache( designSystem: DesignSystem, From aa22315eb366862e6d94a1309bbe1dd51d8507a9 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 16 Nov 2026 19:38:14 +0100 Subject: [PATCH 3/7] Keep the space after an escaped delimiter in at-rule params In `@scope (.foo\+ .bar)`, the `+` is part of the class name and the space is a descendant combinator. The whitespace normalization treated the escaped `+` as a delimiter and dropped the space, so the signature matched `@scope (.foo\+.bar)`. --- .../src/canonicalize-candidates.test.ts | 14 ++++++++++++++ .../tailwindcss/src/canonicalize-candidates.ts | 11 +++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index e442fcd3b8d8..93ef15494cca 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -2138,6 +2138,11 @@ describe('regressions', () => { @slot; } } + @custom-variant in-foo-plus-bar { + @scope (.foo\+.bar) { + @slot; + } + } `, { base: __dirname }, ) @@ -2156,6 +2161,15 @@ describe('regressions', () => { '[@supports_selector(.foo.bar)]:flex', ]), ).toEqual(['in-foo-bar:flex', 'supports-foo-bar:flex']) + + // An escaped `+` is part of the class name, so the space after it is + // still a descendant combinator + expect(designSystem.canonicalizeCandidates(['[@scope_(.foo\\+_.bar)]:flex'])).toEqual([ + '[@scope_(.foo\\+_.bar)]:flex', + ]) + expect(designSystem.canonicalizeCandidates(['[@scope_(.foo\\+.bar)]:flex'])).toEqual([ + 'in-foo-plus-bar:flex', + ]) }) }) diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 08f046d72999..d0371b7b2513 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -3508,18 +3508,23 @@ function createVariantSignatureCache( // it can be a descendant combinator. E.g.: `@scope (.foo .bar)` and // `@scope (.foo.bar)` match different elements. The same goes for whitespace // before a `:` (`.foo :hover`), unless whitespace follows the `:` as well -// (`(pointer : fine)`). Strings and escapes are kept as-is. +// (`(pointer : fine)`). Strings and escaped characters are kept as-is. function normalizeAtRuleParamsWhitespace(params: string) { let result = '' let pendingSpace = false let quote = 0 + // Whether the last character in `result` is escaped. E.g.: the `+` in + // `.foo\+ .bar` is part of the class name, not a delimiter. + let escaped = false + for (let i = 0; i < params.length; i++) { let char = params.charCodeAt(i) // Copy strings verbatim, including escapes if (quote !== 0) { result += params[i] + escaped = false if (char === BACKSLASH && i + 1 < params.length) { result += params[++i] } else if (char === quote) { @@ -3539,7 +3544,7 @@ function normalizeAtRuleParamsWhitespace(params: string) { if (pendingSpace) { pendingSpace = false if ( - !dropsWhitespaceAfter(result.charCodeAt(result.length - 1)) && + (escaped || !dropsWhitespaceAfter(result.charCodeAt(result.length - 1))) && !dropsWhitespaceBefore(char) && !(char === COLON && isWhitespace(params.charCodeAt(i + 1))) ) { @@ -3548,9 +3553,11 @@ function normalizeAtRuleParamsWhitespace(params: string) { } result += params[i] + escaped = false if (char === BACKSLASH && i + 1 < params.length) { result += params[++i] + escaped = true } else if (char === SINGLE_QUOTE || char === DOUBLE_QUOTE) { quote = char } From be960695ed0ddcc8b3634a9cf4c3b1bc21047be3 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 16 Nov 2026 19:38:15 +0100 Subject: [PATCH 4/7] Ignore whitespace before `:` in `@media` and `@container` params Whitespace before a `:` is kept in at-rule params, because it can be a descendant combinator in a selector (`.foo :hover`). `@media` and `@container` params can't contain selectors, so `(pointer :fine)` and `(pointer:fine)` are the same there. This lets `[@media_(pointer_:fine)]:flex` canonicalize to `pointer-fine:flex` again. --- .../tailwindcss/src/canonicalize-candidates.test.ts | 1 + packages/tailwindcss/src/canonicalize-candidates.ts | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 93ef15494cca..45a4b32235ae 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -1149,6 +1149,7 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s', // Media queries ['[@media(pointer:fine)]:flex', 'pointer-fine:flex'], ['[@media_(pointer_:_fine)]:flex', 'pointer-fine:flex'], + ['[@media_(pointer_:fine)]:flex', 'pointer-fine:flex'], ['[@media_not_(pointer_:_fine)]:flex', 'not-pointer-fine:flex'], ['[@media_print]:flex', 'print:flex'], ['[@media_not_print]:flex', 'not-print:flex'], diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index d0371b7b2513..96e2e8b22ea1 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -3418,7 +3418,10 @@ function createVariantSignatureCache( node.params = constantFoldDeclaration(node.params, rem) if (node.params.includes(' ')) { - node.params = normalizeAtRuleParamsWhitespace(node.params) + node.params = normalizeAtRuleParamsWhitespace( + node.params, + node.name !== '@media' && node.name !== '@container', + ) } } @@ -3508,8 +3511,9 @@ function createVariantSignatureCache( // it can be a descendant combinator. E.g.: `@scope (.foo .bar)` and // `@scope (.foo.bar)` match different elements. The same goes for whitespace // before a `:` (`.foo :hover`), unless whitespace follows the `:` as well -// (`(pointer : fine)`). Strings and escaped characters are kept as-is. -function normalizeAtRuleParamsWhitespace(params: string) { +// (`(pointer : fine)`), or the params can't contain selectors (`@media` and +// `@container`). Strings and escaped characters are kept as-is. +function normalizeAtRuleParamsWhitespace(params: string, canContainSelectors: boolean) { let result = '' let pendingSpace = false let quote = 0 @@ -3546,7 +3550,7 @@ function normalizeAtRuleParamsWhitespace(params: string) { if ( (escaped || !dropsWhitespaceAfter(result.charCodeAt(result.length - 1))) && !dropsWhitespaceBefore(char) && - !(char === COLON && isWhitespace(params.charCodeAt(i + 1))) + !(char === COLON && (!canContainSelectors || isWhitespace(params.charCodeAt(i + 1)))) ) { result += ' ' } From d00a173bf63aefc33e0011d06d685441d31c830a Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 16 Nov 2026 22:30:52 +0100 Subject: [PATCH 5/7] Keep the whitespace terminator of a hex escape in at-rule params A hex escape like `\2b` consumes one whitespace character after it. So `.foo\2b .bar` is a descendant selector, but `.foo\2b .bar` is the compound `.foo\+.bar`. The whitespace normalization only consumed the first character after the backslash, then collapsed both spaces into one, so the two selectors got the same variant signature. Reported by CodeRabbit on https://github.com/tailwindlabs/tailwindcss/pull/20715. --- .../src/canonicalize-candidates.test.ts | 14 ++++++++++++++ .../tailwindcss/src/canonicalize-candidates.ts | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 45a4b32235ae..da623566f896 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -2144,6 +2144,11 @@ describe('regressions', () => { @slot; } } + @custom-variant in-foo-hex-plus-bar { + @scope (.foo\2b .bar) { + @slot; + } + } `, { base: __dirname }, ) @@ -2171,6 +2176,15 @@ describe('regressions', () => { expect(designSystem.canonicalizeCandidates(['[@scope_(.foo\\+.bar)]:flex'])).toEqual([ 'in-foo-plus-bar:flex', ]) + + // A hex escape consumes one whitespace character after it, so only the + // second space is a descendant combinator + expect(designSystem.canonicalizeCandidates(['[@scope_(.foo\\2b__.bar)]:flex'])).toEqual([ + '[@scope_(.foo\\2b__.bar)]:flex', + ]) + expect(designSystem.canonicalizeCandidates(['[@scope_(.foo\\2b_.bar)]:flex'])).toEqual([ + 'in-foo-hex-plus-bar:flex', + ]) }) }) diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 96e2e8b22ea1..6b61205945bf 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -3560,7 +3560,21 @@ function normalizeAtRuleParamsWhitespace(params: string, canContainSelectors: bo escaped = false if (char === BACKSLASH && i + 1 < params.length) { - result += params[++i] + if (isHexDigit(params.charCodeAt(i + 1))) { + // A hex escape has up to 6 hex digits, and consumes one whitespace + // character after it. E.g.: `.foo\2b .bar` is `.foo\+.bar`, so a + // second space would be a descendant combinator. + let end = i + 1 + while (end < i + 7 && end < params.length && isHexDigit(params.charCodeAt(end))) end++ + result += params.slice(i + 1, end) + i = end - 1 + if (isWhitespace(params.charCodeAt(i + 1))) { + result += ' ' + i++ + } + } else { + result += params[++i] + } escaped = true } else if (char === SINGLE_QUOTE || char === DOUBLE_QUOTE) { quote = char From 1002130d661a167f636650c435b4b2a64590f90c Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 16 Nov 2026 22:50:36 +0100 Subject: [PATCH 6/7] Drop the optional whitespace after a hex escape in at-rule params The whitespace that ends a hex escape is only needed when a hex digit or another space follows it. Keeping it everywhere meant that `.foo\2b>.bar` and `.foo\2b >.bar` got different variant signatures, so `[@scope_(.foo\2b_>.bar)]:flex` was no longer replaced by an equivalent custom variant. Reported by Greptile on https://github.com/tailwindlabs/tailwindcss/pull/20715. --- .../src/canonicalize-candidates.test.ts | 15 ++++++++ .../src/canonicalize-candidates.ts | 37 +++++++++++-------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index da623566f896..332f326813d2 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -2149,6 +2149,11 @@ describe('regressions', () => { @slot; } } + @custom-variant in-foo-hex-plus-child-bar { + @scope (.foo\2b>.bar) { + @slot; + } + } `, { base: __dirname }, ) @@ -2185,6 +2190,16 @@ describe('regressions', () => { expect(designSystem.canonicalizeCandidates(['[@scope_(.foo\\2b_.bar)]:flex'])).toEqual([ 'in-foo-hex-plus-bar:flex', ]) + + // The whitespace that ends a hex escape is optional when the next character + // can't be part of the escape + expect( + designSystem.canonicalizeCandidates([ + '[@scope_(.foo\\2b>.bar)]:flex', + '[@scope_(.foo\\2b_>.bar)]:flex', + '[@scope_(.foo\\2b__>_.bar)]:flex', + ]), + ).toEqual(['in-foo-hex-plus-child-bar:flex']) }) }) diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 6b61205945bf..47dec616e106 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -3522,6 +3522,10 @@ function normalizeAtRuleParamsWhitespace(params: string, canContainSelectors: bo // `.foo\+ .bar` is part of the class name, not a delimiter. let escaped = false + // Whether `result` ends with a hex escape that has no whitespace after it + // yet. E.g.: `\2b` in `.foo\2b .bar`. + let openHexEscape = false + for (let i = 0; i < params.length; i++) { let char = params.charCodeAt(i) @@ -3545,33 +3549,34 @@ function normalizeAtRuleParamsWhitespace(params: string, canContainSelectors: bo // Only keep whitespace between two characters that need it. Trailing // whitespace is dropped, because no character follows it. - if (pendingSpace) { - pendingSpace = false - if ( - (escaped || !dropsWhitespaceAfter(result.charCodeAt(result.length - 1))) && - !dropsWhitespaceBefore(char) && - !(char === COLON && (!canContainSelectors || isWhitespace(params.charCodeAt(i + 1)))) - ) { - result += ' ' - } - } + let space = + pendingSpace && + (escaped || !dropsWhitespaceAfter(result.charCodeAt(result.length - 1))) && + !dropsWhitespaceBefore(char) && + !(char === COLON && (!canContainSelectors || isWhitespace(params.charCodeAt(i + 1)))) + pendingSpace = false + + // A hex escape needs whitespace to end it when a hex digit or another + // space follows. E.g.: `.foo\2b .bar` is `.foo\+ .bar`, but `.foo\2b>.bar` + // and `.foo\2b >.bar` are the same. + if (openHexEscape && (space || isHexDigit(char))) result += ' ' + if (space) result += ' ' result += params[i] escaped = false + openHexEscape = false if (char === BACKSLASH && i + 1 < params.length) { if (isHexDigit(params.charCodeAt(i + 1))) { // A hex escape has up to 6 hex digits, and consumes one whitespace - // character after it. E.g.: `.foo\2b .bar` is `.foo\+.bar`, so a - // second space would be a descendant combinator. + // character after it. E.g.: `.foo\2b .bar` is `.foo\+.bar`, so only + // a second space would be a descendant combinator. let end = i + 1 while (end < i + 7 && end < params.length && isHexDigit(params.charCodeAt(end))) end++ result += params.slice(i + 1, end) i = end - 1 - if (isWhitespace(params.charCodeAt(i + 1))) { - result += ' ' - i++ - } + if (isWhitespace(params.charCodeAt(i + 1))) i++ + openHexEscape = true } else { result += params[++i] } From 80332f46361449d9d8f38afa058da8ed43a36a04 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 16 Nov 2026 22:58:09 +0100 Subject: [PATCH 7/7] Treat CRLF after a hex escape in at-rule params as one whitespace CSS treats CRLF as one newline, so a hex escape consumes both characters. The whitespace normalization consumed only the CR, and the LF became a descendant combinator. So a plugin variant like `@scope (.foo\2b\r\n.bar) to (.limit)` (a compound selector) got the same signature as `[@scope_(.foo\2b__.bar)_to_(.limit)]:flex` (a descendant selector). The stylesheet parser already normalizes CRLF, so only plugin variants keep it in their params. Reported by CodeRabbit on https://github.com/tailwindlabs/tailwindcss/pull/20715. --- .../src/canonicalize-candidates.test.ts | 29 +++++++++++++++++++ .../src/canonicalize-candidates.ts | 7 ++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 332f326813d2..85dc07c40a94 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -2201,6 +2201,35 @@ describe('regressions', () => { ]), ).toEqual(['in-foo-hex-plus-child-bar:flex']) }) + + // CSS treats CRLF as one newline. The stylesheet parser already normalizes + // it, but a plugin variant keeps it in the at-rule params. + test('treats CRLF after a hex escape in an at-rule as one whitespace character', async () => { + let designSystem = await __unstable__loadDesignSystem( + css` + @tailwind utilities; + @plugin "my-plugin"; + `, + { + async loadModule() { + return { + path: '', + base: '', + module: plugin(({ addVariant }) => { + addVariant('in-foo-hex-plus-bar', '@scope (.foo\\2b\r\n.bar) to (.limit)') + }), + } + }, + }, + ) + + expect( + designSystem.canonicalizeCandidates(['[@scope_(.foo\\2b__.bar)_to_(.limit)]:flex']), + ).toEqual(['[@scope_(.foo\\2b__.bar)_to_(.limit)]:flex']) + expect( + designSystem.canonicalizeCandidates(['[@scope_(.foo\\2b_.bar)_to_(.limit)]:flex']), + ).toEqual(['in-foo-hex-plus-bar:flex']) + }) }) describe('normalizeEscapes', () => { diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 47dec616e106..ba760a878d4b 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -3575,7 +3575,12 @@ function normalizeAtRuleParamsWhitespace(params: string, canContainSelectors: bo while (end < i + 7 && end < params.length && isHexDigit(params.charCodeAt(end))) end++ result += params.slice(i + 1, end) i = end - 1 - if (isWhitespace(params.charCodeAt(i + 1))) i++ + if (isWhitespace(params.charCodeAt(i + 1))) { + i++ + + // CSS treats CRLF as one newline + if (params.charCodeAt(i) === 0x0d && params.charCodeAt(i + 1) === 0x0a) i++ + } openHexEscape = true } else { result += params[++i]