Skip to content
Merged
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/declaration-without-semicolon-at-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Keep a declaration without a `;` at the end of the CSS input (e.g. `color: red`)
46 changes: 46 additions & 0 deletions packages/tailwindcss/src/css-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,42 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
).toEqual([{ kind: 'declaration', property: 'color', value: 'red', important: false }])
})

it('should parse a declaration with a missing ending `;`', () => {
expect(parse('color: red')).toEqual([
{ kind: 'declaration', property: 'color', value: 'red', important: false },
])
})

it('should parse a declaration with a missing ending `;` and a trailing newline', () => {
expect(parse('color: red\n')).toEqual([
{ kind: 'declaration', property: 'color', value: 'red', important: false },
])
})

it('should parse a declaration with a missing ending `;` and `!important`', () => {
expect(parse('color: red !important')).toEqual([
{ kind: 'declaration', property: 'color', value: 'red', important: true },
])
})

it('should parse multiple declarations where the last one has a missing ending `;`', () => {
expect(parse('a: 1; b: 2')).toEqual([
{ kind: 'declaration', property: 'a', value: '1', important: false },
{ kind: 'declaration', property: 'b', value: '2', important: false },
])
})

it('should parse a declaration with a missing ending `;` after a custom property', () => {
expect(parse('--a: 1;\ncolor: red')).toEqual([
{ kind: 'declaration', property: '--a', value: '1', important: false },
{ kind: 'declaration', property: 'color', value: 'red', important: false },
])
})

it('should not parse a declaration with unbalanced brackets and a missing ending `;`', () => {
expect(parse('color: rgb(1, 2')).toEqual([])
})

it('should parse declarations with strings', () => {
expect(
parse(css`
Expand Down Expand Up @@ -482,6 +518,16 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
])
})

it('should parse a custom property with an unclosed bracket at the end of the input', () => {
expect(parse('--foo: [bar')).toEqual([
{ kind: 'declaration', property: '--foo', value: '[bar', important: false },
])
expect(parse('color: red; --foo: (bar')).toEqual([
{ kind: 'declaration', property: 'color', value: 'red', important: false },
{ kind: 'declaration', property: '--foo', value: '(bar', important: false },
])
})

it('should parse a custom property with an embedded programming language', () => {
expect(
parse(css`
Expand Down
25 changes: 25 additions & 0 deletions packages/tailwindcss/src/css-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,31 @@ export function parse(input: string, opts?: ParseOptions) {
ast.push(node)
}

// If we have any other leftover `buffer` then it can be a declaration that
// is not terminated with a semicolon at the end of the input. Unbalanced
// brackets (e.g. `color: rgb(1, 2`) mean that the declaration is incomplete.
//
// E.g.:
//
// ```css
// color: red
// ```
else if (buffer.length > 0 && closingBracketStack.length === 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

Supported by static analysis

Script executed:

printf '%s\n' '--- declaration AST and printers ---'
rg -n 'function decl|type.*Declaration|nodes.*decl|toCss|print|serialize|Declaration' packages/tailwindcss/src/ast.ts packages/tailwindcss/src 2>/dev/null | head -260
sed -n '1,240p' packages/tailwindcss/src/ast.ts
printf '%s\n' '--- import substitution path ---'
sed -n '1,230p' packages/tailwindcss/src/at-import.ts
sed -n '120,180p' packages/tailwindcss/src/index.ts
printf '%s\n' '--- relevant tests ---'
rg -n 'substituteAtImports|@import|grid-template-columns|serialize|toCss' packages/tailwindcss/src/*.test.ts packages/tailwindcss/src 2>/dev/null | head -260

Repository: tailwindlabs/tailwindcss

Length of output: 41913


Script executed:

set -e
printf '%s\n' '--- ast declaration printer ---'
nl -ba packages/tailwindcss/src/ast.ts | sed -n '1700,1785p'
printf '%s\n' '--- import substitution ---'
nl -ba packages/tailwindcss/src/at-import.ts | sed -n '1,115p'
printf '%s\n' '--- parser EOF and declaration paths ---'
nl -ba packages/tailwindcss/src/css-parser.ts | sed -n '220,285p'
nl -ba packages/tailwindcss/src/css-parser.ts | sed -n '390,620p'
nl -ba packages/tailwindcss/src/css-parser.ts | sed -n '660,725p'
printf '%s\n' '--- parse entrypoint and relevant callers ---'
rg -n -F -- 'substituteAtImports(' packages/tailwindcss/src
rg -n -F -- 'toCss(' packages/tailwindcss/src/index.ts packages/tailwindcss/src/ast.ts packages/tailwindcss/src | head -80

Repository: tailwindlabs/tailwindcss

Length of output: 28716


Reject EOF declarations with an unclosed square bracket.

parseDeclaration accepts grid-template-columns: [sidebar at EOF, and toCss emits the value unchanged. substituteAtImports replaces imports with AST nodes; it does not consume sibling nodes itself. However, the serialized stylesheet contains an unclosed [, so a downstream CSS parser can consume the following semicolon, closing brace, and later rules as part of that unfinished block.

Add a string-aware square-bracket check. Brackets inside quoted strings must not count, so content: "[" remains valid.

Suggested fix
--- "a/packages/tailwindcss/src/css-parser.ts"
+++ "b/packages/tailwindcss/src/css-parser.ts"
@@ -594,7 +594,11 @@
   // ```css
   // color: red
   // ```
-  else if (buffer.length > 0 && closingBracketStack.length === 0) {
+  else if (
+    buffer.length > 0 &&
+    closingBracketStack.length === 0 &&
+    !hasUnclosedSquareBracket(buffer)
+  ) {
     let declaration = parseDeclaration(buffer)
     if (declaration) {
       if (source) {

Implement hasUnclosedSquareBracket with escape- and quote-aware scanning, and add tests for both grid-template-columns: [sidebar and content: "[".

let declaration = parseDeclaration(buffer)
Comment on lines +609 to +610

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Data Integrity & Integration · Quick win

Handle a trailing backslash before emitting the declaration.

For color: red\ at EOF, the scanner retains the final backslash. toCss then emits color: red\;, so the backslash escapes the generated semicolon. A following declaration can be consumed as part of the color value. Reject or normalize a trailing escape before emitting the declaration. (raw.githubusercontent.com)


Minor · Data Integrity & Integration · Quick win

Supported by static analysis

Script executed:

sed -n '1,220p' packages/tailwindcss/src/css-parser.ts
sed -n '560,650p' packages/tailwindcss/src/css-parser.ts
rg -n 'parseString|parseDeclaration|toCss|substituteAtImports|@import' packages/tailwindcss/src/css-parser.ts packages/tailwindcss/src/ast.ts packages/tailwindcss/src/at-import.ts packages/tailwindcss/src/index.ts packages/tailwindcss/src/*.test.ts
sed -n '1,260p' packages/tailwindcss/src/ast.ts
sed -n '1,240p' packages/tailwindcss/src/at-import.ts

Repository: tailwindlabs/tailwindcss

Length of output: 41373


Script executed:

set -o pipefail
printf '%s\n' '--- css-parser declarations ---'
nl -ba packages/tailwindcss/src/css-parser.ts | sed -n '280,420p'
nl -ba packages/tailwindcss/src/css-parser.ts | sed -n '470,550p'
nl -ba packages/tailwindcss/src/css-parser.ts | sed -n '670,790p'
printf '%s\n' '--- ast serializer ---'
nl -ba packages/tailwindcss/src/ast.ts | sed -n '1680,1775p'
printf '%s\n' '--- import and parser tests ---'
rg -n -F --glob 'css-parser.test.ts' -e 'unfinished' -e 'unterminated' -e 'trailing' -e 'content:' -e '@import' packages/tailwindcss/src
rg -n -F --glob '*.test.ts' -e 'substituteAtImports' -e 'imported' -e 'content:' packages/tailwindcss/src/at-import.test.ts packages/tailwindcss/src/index.test.ts packages/tailwindcss/src/css-parser.test.ts

Repository: tailwindlabs/tailwindcss

Length of output: 21497


Script executed:

set -o pipefail
printf '%s\n' '--- parseString completion ---'
nl -ba packages/tailwindcss/src/css-parser.ts | sed -n '770,835p'
printf '%s\n' '--- serializer traversal ---'
nl -ba packages/tailwindcss/src/ast.ts | sed -n '1760,1875p'
printf '%s\n' '--- unterminated-string tests ---'
nl -ba packages/tailwindcss/src/css-parser.test.ts | sed -n '1425,1525p'
printf '%s\n' '--- EOF declaration tests ---'
nl -ba packages/tailwindcss/src/css-parser.test.ts | sed -n '160,220p'

Repository: tailwindlabs/tailwindcss

Length of output: 11551


Script executed:

set -o pipefail
printf '%s\n' '--- remaining serializer cases ---'
nl -ba packages/tailwindcss/src/ast.ts | sed -n '1870,1985p'
printf '%s\n' '--- import substitution test helpers and adjacency cases ---'
nl -ba packages/tailwindcss/src/at-import.test.ts | sed -n '1,115p'
nl -ba packages/tailwindcss/src/at-import.test.ts | sed -n '550,640p'

Repository: tailwindlabs/tailwindcss

Length of output: 9887


Script executed:

set -o pipefail
printf '%s\n' '--- optimizeAst context handling ---'
rg -n -F -- 'node.kind ===' packages/tailwindcss/src/ast.ts | sed -n '1,80p'
nl -ba packages/tailwindcss/src/ast.ts | sed -n '230,430p'
printf '%s\n' '--- compile pipeline around import substitution and serialization ---'
nl -ba packages/tailwindcss/src/index.ts | sed -n '100,175p'
nl -ba packages/tailwindcss/src/index.ts | sed -n '820,865p'

Repository: tailwindlabs/tailwindcss

Length of output: 12741


Reject EOF declarations with unterminated strings.

When parseString reaches EOF, it returns startIdx instead of reporting an unmatched quote. The scanner still includes the remaining characters in buffer, and the EOF branch emits the declaration. toCss then serializes the raw value followed by ;\n.

An imported partial can therefore retain content: "unfinished and produce malformed CSS. The current parser throws at the generated newline; it does not consume the following declaration as part of the value. Reject the incomplete declaration at EOF instead of adding a closing quote.

Suggested fix
-  return startIdx
+  throw new CssSyntaxError(
+    `Unterminated string: ${input.slice(startIdx) + String.fromCharCode(quoteChar)}`,
+    source ? [source, startIdx, input.length] : null,
+  )

if (declaration) {
if (source) {
declaration.src = [source, bufferStart, input.length]
declaration.dst = [source, bufferStart, input.length]
}

if (parent) {
parent.nodes.push(declaration)
} else {
ast.push(declaration)
}
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// When we are done parsing then everything should be balanced. If we still
// have a leftover `parent`, then it means that we have an unterminated block.
if (closingBracketStack.length > 0 && parent) {
Expand Down
Loading