Skip to content
Open
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
3 changes: 3 additions & 0 deletions .changelog/unbalanced-curlies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Ignore arbitrary values with unbalanced curly braces (e.g. `w-[{]`) instead of generating invalid CSS
10 changes: 8 additions & 2 deletions packages/tailwindcss/src/candidate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,18 @@ describe('arbitrary values with unbalanced parens or brackets', () => {
'w-[var(--a,[1px)]',
'w-[var(--a,[1px]',

// A `}` at the top-level is invalid. `{` is not tracked on the stack,
// otherwise `[&{color:red}]:flex` would become valid.
// A `{` or `}` at the top-level is invalid, e.g. `[&{color:red}]:flex`
'w-[{}]',
'w-[{]',

// Nested curlies must be balanced
'w-[calc({)]',
'w-[calc(})]',
'bg-(--a,{)',

// Arbitrary properties
'[width:calc(1px]',
'[--a:{]',

// Arbitrary modifiers
'bg-red-500/[calc(1]',
Expand Down
11 changes: 7 additions & 4 deletions packages/tailwindcss/src/utils/is-valid-arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,26 @@ export function isValidArbitrary(input: string, isDeclarationValue = false) {
stackPos++
break
case OPEN_CURLY:
// NOTE: We intentionally do not consider `{` to move the stack pointer
// because a candidate like `[&{color:red}]:flex` should not be valid.
// A top-level `{` would start a block in the generated CSS, e.g.:
// `[&{color:red}]:flex`. Nested ones must be closed, e.g. `var(--a,{})`.
if (stackPos === 0) return false
closingBracketStack[stackPos] = CLOSE_CURLY
stackPos++
break
case CLOSE_BRACKET:
case CLOSE_CURLY:
case CLOSE_PAREN:
if (stackPos === 0) return false

if (stackPos > 0 && char === closingBracketStack[stackPos - 1]) {
if (char === closingBracketStack[stackPos - 1]) {
// SAFETY: The buffer does not need to be mutated because the stack is
// only ever read from or written to its current position. Its current
// position is only ever incremented after writing to it. Meaning that
// the buffer can be dirty for the next use and still be correct since
// reading/writing always starts at position `0`.
stackPos--
if (stackPos === keepUnderscoresUntil) keepUnderscoresUntil = -1
} else if (char !== CLOSE_CURLY) {
} else {
return false
}

Expand Down