Skip to content

Commit cd5831f

Browse files
authored
Keep a declaration without a ; at the end of the CSS input (#20655)
## Summary This PR keeps a regular declaration that has no `;` at the end of the CSS input, e.g. `color: red` or `--a: 1;\ncolor: red`. Before, the parser only handled a leftover at-rule after the main loop, so it silently dropped the declaration. PostCSS keeps it (`postcss.parse('color: red')` gives a `decl`), and a top-level declaration that ends with `;` is already kept. The fix parses the leftover `buffer` as a declaration too, and adds it to the current parent, or to the root. A leftover that is not a declaration (e.g. `foo`) is still ignored, as before, so input that parses today does not start to throw. An unclosed block (e.g. `.foo { color: red`) still throws `Missing closing }`. A leftover with unbalanced parentheses (e.g. `color: rgb(1, 2`) is not kept, because it is incomplete. A test also covers a custom property with an unclosed bracket at the end of the input, e.g. `--foo: [bar`. #20653 makes the rest of the input part of the custom property. Without it, the leftover `-foo: [bar` would become a declaration with this PR. A user can only reach the bare declaration case through an `@import` inside a rule, e.g. `:root { @import './partial.css'; }`, where the partial ends with a bare declaration. That is not valid CSS, but `substituteAtImports` inlines it, because it walks the whole AST. Thus the value of this fix is small, and the change stays small. Related: #20653 (merged) fixed the same problem for custom properties. This PR is rebased on top of it. ## Test plan - New tests in `packages/tailwindcss/src/css-parser.test.ts` (Unix and Windows line endings) for `color: red`, `color: red\n`, `color: red !important`, `a: 1; b: 2` and `--a: 1;\ncolor: red`. They fail without the fix. - New tests for `color: rgb(1, 2` (gives `[]`) and for `--foo: [bar` and `color: red; --foo: (bar` (keep the custom property, together with #20653). - `pnpm exec vitest run` in `packages/tailwindcss`: all tests pass. - `pnpm changelog check` passes.
1 parent 4364cad commit cd5831f

3 files changed

Lines changed: 74 additions & 0 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+
- Keep a declaration without a `;` at the end of the CSS input (e.g. `color: red`)

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,42 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
180180
).toEqual([{ kind: 'declaration', property: 'color', value: 'red', important: false }])
181181
})
182182

183+
it('should parse a declaration with a missing ending `;`', () => {
184+
expect(parse('color: red')).toEqual([
185+
{ kind: 'declaration', property: 'color', value: 'red', important: false },
186+
])
187+
})
188+
189+
it('should parse a declaration with a missing ending `;` and a trailing newline', () => {
190+
expect(parse('color: red\n')).toEqual([
191+
{ kind: 'declaration', property: 'color', value: 'red', important: false },
192+
])
193+
})
194+
195+
it('should parse a declaration with a missing ending `;` and `!important`', () => {
196+
expect(parse('color: red !important')).toEqual([
197+
{ kind: 'declaration', property: 'color', value: 'red', important: true },
198+
])
199+
})
200+
201+
it('should parse multiple declarations where the last one has a missing ending `;`', () => {
202+
expect(parse('a: 1; b: 2')).toEqual([
203+
{ kind: 'declaration', property: 'a', value: '1', important: false },
204+
{ kind: 'declaration', property: 'b', value: '2', important: false },
205+
])
206+
})
207+
208+
it('should parse a declaration with a missing ending `;` after a custom property', () => {
209+
expect(parse('--a: 1;\ncolor: red')).toEqual([
210+
{ kind: 'declaration', property: '--a', value: '1', important: false },
211+
{ kind: 'declaration', property: 'color', value: 'red', important: false },
212+
])
213+
})
214+
215+
it('should not parse a declaration with unbalanced brackets and a missing ending `;`', () => {
216+
expect(parse('color: rgb(1, 2')).toEqual([])
217+
})
218+
183219
it('should parse declarations with strings', () => {
184220
expect(
185221
parse(css`
@@ -482,6 +518,16 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
482518
])
483519
})
484520

521+
it('should parse a custom property with an unclosed bracket at the end of the input', () => {
522+
expect(parse('--foo: [bar')).toEqual([
523+
{ kind: 'declaration', property: '--foo', value: '[bar', important: false },
524+
])
525+
expect(parse('color: red; --foo: (bar')).toEqual([
526+
{ kind: 'declaration', property: 'color', value: 'red', important: false },
527+
{ kind: 'declaration', property: '--foo', value: '(bar', important: false },
528+
])
529+
})
530+
485531
it('should parse a custom property with an embedded programming language', () => {
486532
expect(
487533
parse(css`

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,31 @@ export function parse(input: string, opts?: ParseOptions) {
597597
ast.push(node)
598598
}
599599

600+
// If we have any other leftover `buffer` then it can be a declaration that
601+
// is not terminated with a semicolon at the end of the input. Unbalanced
602+
// brackets (e.g. `color: rgb(1, 2`) mean that the declaration is incomplete.
603+
//
604+
// E.g.:
605+
//
606+
// ```css
607+
// color: red
608+
// ```
609+
else if (buffer.length > 0 && closingBracketStack.length === 0) {
610+
let declaration = parseDeclaration(buffer)
611+
if (declaration) {
612+
if (source) {
613+
declaration.src = [source, bufferStart, input.length]
614+
declaration.dst = [source, bufferStart, input.length]
615+
}
616+
617+
if (parent) {
618+
parent.nodes.push(declaration)
619+
} else {
620+
ast.push(declaration)
621+
}
622+
}
623+
}
624+
600625
// When we are done parsing then everything should be balanced. If we still
601626
// have a leftover `parent`, then it means that we have an unterminated block.
602627
if (closingBracketStack.length > 0 && parent) {

0 commit comments

Comments
 (0)