Skip to content

Commit b5aeb6d

Browse files
committed
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 tailwindlabs#20715.
1 parent b0b3a7c commit b5aeb6d

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,11 @@ describe('regressions', () => {
21482148
@slot;
21492149
}
21502150
}
2151+
@custom-variant in-foo-hex-plus-child-bar {
2152+
@scope (.foo\2b>.bar) {
2153+
@slot;
2154+
}
2155+
}
21512156
`,
21522157
{ base: __dirname },
21532158
)
@@ -2184,6 +2189,16 @@ describe('regressions', () => {
21842189
expect(designSystem.canonicalizeCandidates(['[@scope_(.foo\\2b_.bar)]:flex'])).toEqual([
21852190
'in-foo-hex-plus-bar:flex',
21862191
])
2192+
2193+
// The whitespace that ends a hex escape is optional when the next character
2194+
// can't be part of the escape
2195+
expect(
2196+
designSystem.canonicalizeCandidates([
2197+
'[@scope_(.foo\\2b>.bar)]:flex',
2198+
'[@scope_(.foo\\2b_>.bar)]:flex',
2199+
'[@scope_(.foo\\2b__>_.bar)]:flex',
2200+
]),
2201+
).toEqual(['in-foo-hex-plus-child-bar:flex'])
21872202
})
21882203
})
21892204

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3522,6 +3522,10 @@ function normalizeAtRuleParamsWhitespace(params: string, canContainSelectors: bo
35223522
// `.foo\+ .bar` is part of the class name, not a delimiter.
35233523
let escaped = false
35243524

3525+
// Whether `result` ends with a hex escape that has no whitespace after it
3526+
// yet. E.g.: `\2b` in `.foo\2b .bar`.
3527+
let openHexEscape = false
3528+
35253529
for (let i = 0; i < params.length; i++) {
35263530
let char = params.charCodeAt(i)
35273531

@@ -3545,33 +3549,34 @@ function normalizeAtRuleParamsWhitespace(params: string, canContainSelectors: bo
35453549

35463550
// Only keep whitespace between two characters that need it. Trailing
35473551
// whitespace is dropped, because no character follows it.
3548-
if (pendingSpace) {
3549-
pendingSpace = false
3550-
if (
3551-
(escaped || !dropsWhitespaceAfter(result.charCodeAt(result.length - 1))) &&
3552-
!dropsWhitespaceBefore(char) &&
3553-
!(char === COLON && (!canContainSelectors || isWhitespace(params.charCodeAt(i + 1))))
3554-
) {
3555-
result += ' '
3556-
}
3557-
}
3552+
let space =
3553+
pendingSpace &&
3554+
(escaped || !dropsWhitespaceAfter(result.charCodeAt(result.length - 1))) &&
3555+
!dropsWhitespaceBefore(char) &&
3556+
!(char === COLON && (!canContainSelectors || isWhitespace(params.charCodeAt(i + 1))))
3557+
pendingSpace = false
3558+
3559+
// A hex escape needs whitespace to end it when a hex digit or another
3560+
// space follows. E.g.: `.foo\2b .bar` is `.foo\+ .bar`, but `.foo\2b>.bar`
3561+
// and `.foo\2b >.bar` are the same.
3562+
if (openHexEscape && (space || isHexDigit(char))) result += ' '
3563+
if (space) result += ' '
35583564

35593565
result += params[i]
35603566
escaped = false
3567+
openHexEscape = false
35613568

35623569
if (char === BACKSLASH && i + 1 < params.length) {
35633570
if (isHexDigit(params.charCodeAt(i + 1))) {
35643571
// A hex escape has up to 6 hex digits, and consumes one whitespace
3565-
// character after it. E.g.: `.foo\2b .bar` is `.foo\+.bar`, so a
3566-
// second space would be a descendant combinator.
3572+
// character after it. E.g.: `.foo\2b .bar` is `.foo\+.bar`, so only
3573+
// a second space would be a descendant combinator.
35673574
let end = i + 1
35683575
while (end < i + 7 && end < params.length && isHexDigit(params.charCodeAt(end))) end++
35693576
result += params.slice(i + 1, end)
35703577
i = end - 1
3571-
if (isWhitespace(params.charCodeAt(i + 1))) {
3572-
result += ' '
3573-
i++
3574-
}
3578+
if (isWhitespace(params.charCodeAt(i + 1))) i++
3579+
openHexEscape = true
35753580
} else {
35763581
result += params[++i]
35773582
}

0 commit comments

Comments
 (0)