Repository navigation
Commit 2570e00
Parse a custom property at the end of the input without a trailing
## 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]>; (#20653)1 parent e4543af commit 2570e00
3 files changed
Lines changed: 44 additions & 4 deletions
File tree
- .changelog
- packages/tailwindcss/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
457 | 457 | | |
458 | 458 | | |
459 | 459 | | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
460 | 485 | | |
461 | 486 | | |
462 | 487 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
| 280 | + | |
284 | 281 | | |
285 | 282 | | |
286 | 283 | | |
| |||
310 | 307 | | |
311 | 308 | | |
312 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
313 | 325 | | |
314 | 326 | | |
315 | 327 | | |
| |||
0 commit comments