Skip to content

Commit 395d4f5

Browse files
committed
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 #20715.
1 parent 1772890 commit 395d4f5

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,35 @@ describe('regressions', () => {
22012201
]),
22022202
).toEqual(['in-foo-hex-plus-child-bar:flex'])
22032203
})
2204+
2205+
// CSS treats CRLF as one newline. The stylesheet parser already normalizes
2206+
// it, but a plugin variant keeps it in the at-rule params.
2207+
test('treats CRLF after a hex escape in an at-rule as one whitespace character', async () => {
2208+
let designSystem = await __unstable__loadDesignSystem(
2209+
css`
2210+
@tailwind utilities;
2211+
@plugin "my-plugin";
2212+
`,
2213+
{
2214+
async loadModule() {
2215+
return {
2216+
path: '',
2217+
base: '',
2218+
module: plugin(({ addVariant }) => {
2219+
addVariant('in-foo-hex-plus-bar', '@scope (.foo\\2b\r\n.bar) to (.limit)')
2220+
}),
2221+
}
2222+
},
2223+
},
2224+
)
2225+
2226+
expect(
2227+
designSystem.canonicalizeCandidates(['[@scope_(.foo\\2b__.bar)_to_(.limit)]:flex']),
2228+
).toEqual(['[@scope_(.foo\\2b__.bar)_to_(.limit)]:flex'])
2229+
expect(
2230+
designSystem.canonicalizeCandidates(['[@scope_(.foo\\2b_.bar)_to_(.limit)]:flex']),
2231+
).toEqual(['in-foo-hex-plus-bar:flex'])
2232+
})
22042233
})
22052234

22062235
describe('normalizeEscapes', () => {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3575,7 +3575,12 @@ function normalizeAtRuleParamsWhitespace(params: string, canContainSelectors: bo
35753575
while (end < i + 7 && end < params.length && isHexDigit(params.charCodeAt(end))) end++
35763576
result += params.slice(i + 1, end)
35773577
i = end - 1
3578-
if (isWhitespace(params.charCodeAt(i + 1))) i++
3578+
if (isWhitespace(params.charCodeAt(i + 1))) {
3579+
i++
3580+
3581+
// CSS treats CRLF as one newline
3582+
if (params.charCodeAt(i) === 0x0d && params.charCodeAt(i + 1) === 0x0a) i++
3583+
}
35793584
openHexEscape = true
35803585
} else {
35813586
result += params[++i]

0 commit comments

Comments
 (0)