Skip to content

Commit 2570e00

Browse files
b84330808claude
andauthored
Parse a custom property at the end of the input without a trailing ; (#20653)
## Summary When a custom property is the last thing in the CSS input and has no `;` or `}` after it, the CSS parser loses part of it: - `--foo: bar` is parsed as `--foo: ba`. When the scan reaches the last character, it takes `input.slice(start, j)`, which leaves out the character at `j`. - `--foo: bar !important` is parsed as the non-important `--foo: bar !importan`. - If the last character is consumed by another branch instead (a closing `)`, a string, a comment or an escape), the loop ends without filling the buffer and the parser emits an empty `: ;` declaration. This happens for `--foo: var(--bar)` or `--foo: 'bar'`, for example. The existing `should parse a custom property with a missing ending ;` test only passes because its input ends with a newline and indentation. This can reach the build output through `@import`, when a file of custom properties without a trailing newline is imported inside a rule: ```css /* input.css */ :root { @import './vars.css'; } ``` ```css /* vars.css, no trailing newline */ --brand: red; --accent: blue ``` This builds to `--accent: blu;`. With `--accent: var(--brand)` it builds to `: ;`. This PR drops the `input.length - 1 === j` check from the loop. When the loop reaches the end of the input without a `;` or `}` ending the custom property, the rest of the input is used as its value. This matches how PostCSS parses the same input: | Input | Before | After (same as PostCSS) | | --- | --- | --- | | `--foo: bar` | `--foo: ba` | `--foo: bar` | | `--foo: bar !important` | `--foo: bar !importan` | `--foo: bar !important` | | `--foo: var(--x)` | `: ;` | `--foo: var(--x)` | | `--foo: 'bar'` | `: ;` | `--foo: 'bar'` | | `--a: 1; --b: 2` | `--a: 1; --b: ;` | `--a: 1; --b: 2` | ## Test plan - Added a test to `css-parser.test.ts` for a custom property at the end of the input with a plain value, `!important`, `var(…)`, a string, a block, a comment, and after another declaration. Like the other tests in that file, it runs with both Unix and Windows line endings. - With the fix reverted, the new test fails (`"value": "ba"` instead of `"bar"`). With the fix, it passes. - `pnpm exec vitest run` in `packages/tailwindcss`: 44 files, 5164 tests passed, 1 skipped. - `pnpm exec vitest run --project @tailwindcss/node` (it uses this parser to rewrite `url(…)`s in imported files): 8 passed. - Compared the parsed declarations with `postcss.parse` for the inputs in the table above. They now match. - `pnpm exec prettier --check` on the changed files passes. I used Claude Code to write this change. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e4543af commit 2570e00

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Fixed
2+
3+
- Parse a custom property at the end of the input when it has no trailing `;` (e.g. `--foo: bar` as the last declaration)

‎packages/tailwindcss/src/css-parser.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,31 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
457457
).toEqual([{ kind: 'declaration', property: '--foo', value: 'bar', important: true }])
458458
})
459459

460+
it('should parse a custom property with a missing ending `;` at the end of the input', () => {
461+
expect(parse('--foo: bar')).toEqual([
462+
{ kind: 'declaration', property: '--foo', value: 'bar', important: false },
463+
])
464+
expect(parse('--foo: bar !important')).toEqual([
465+
{ kind: 'declaration', property: '--foo', value: 'bar', important: true },
466+
])
467+
expect(parse('--foo: var(--bar)')).toEqual([
468+
{ kind: 'declaration', property: '--foo', value: 'var(--bar)', important: false },
469+
])
470+
expect(parse("--foo: 'bar'")).toEqual([
471+
{ kind: 'declaration', property: '--foo', value: "'bar'", important: false },
472+
])
473+
expect(parse('--foo: {}')).toEqual([
474+
{ kind: 'declaration', property: '--foo', value: '{}', important: false },
475+
])
476+
expect(parse('--foo: bar /* baz */')).toEqual([
477+
{ kind: 'declaration', property: '--foo', value: 'bar /* baz */', important: false },
478+
])
479+
expect(parse('--foo: bar;\n--baz: rgb(0 0 0)')).toEqual([
480+
{ kind: 'declaration', property: '--foo', value: 'bar', important: false },
481+
{ kind: 'declaration', property: '--baz', value: 'rgb(0 0 0)', important: false },
482+
])
483+
})
484+
460485
it('should parse a custom property with an embedded programming language', () => {
461486
expect(
462487
parse(css`

‎packages/tailwindcss/src/css-parser.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,7 @@ export function parse(input: string, opts?: ParseOptions) {
277277
// ^
278278
// }
279279
// ```
280-
else if (
281-
(peekChar === CLOSE_CURLY || input.length - 1 === j) &&
282-
closingBracketStack.length === 0
283-
) {
280+
else if (peekChar === CLOSE_CURLY && closingBracketStack.length === 0) {
284281
i = j - 1
285282
buffer += input.slice(start, j)
286283
break
@@ -310,6 +307,21 @@ export function parse(input: string, opts?: ParseOptions) {
310307
}
311308
}
312309

310+
// We reached the end of the input without finding a `;` or `}` that ends
311+
// the custom property, which means that the rest of the input is part of
312+
// the custom property.
313+
//
314+
// E.g.:
315+
//
316+
// ```css
317+
// --custom: value
318+
// ^
319+
// ```
320+
if (buffer.length === 0) {
321+
buffer = input.slice(start)
322+
i = input.length
323+
}
324+
313325
let declaration = parseDeclaration(buffer, colonIdx, valueEnd)
314326
if (!declaration) {
315327
throw new CssSyntaxError(

0 commit comments

Comments
 (0)