From 8bdf211ba48179d0237b241a61c74eaf23a88a51 Mon Sep 17 00:00:00 2001 From: b84330808 Date: Fri, 30 Oct 2026 08:10:25 +0000 Subject: [PATCH] Parse a custom property at the end of the input without a trailing `;` When a custom property was the last thing in the input and had no `;` or `}` after it, the parser took `input.slice(start, j)` where `j` was the index of the last character, so that character was dropped: `--foo: bar` became `--foo: ba` and `--foo: bar !important` became the non-important `--foo: bar !importan`. If the last character was consumed by another branch instead (a closing `)`, a string, a comment or an escape), the loop ended without ever filling the buffer and the parser emitted an empty `: ;` declaration, e.g. for `--foo: var(--bar)`. This is reachable through `@import`: a file of custom properties that is imported inside a rule (e.g. `:root { @import './vars.css'; }`) and has no trailing newline ends up as `--accent: blu;` or `: ;` in the output. Now, when the scan reaches the end of the input, the rest of the input is used as the custom property, the same way PostCSS parses it. Co-Authored-By: Claude Opus 5.5 --- .changelog/custom-property-at-end-of-input.md | 3 +++ packages/tailwindcss/src/css-parser.test.ts | 25 +++++++++++++++++++ packages/tailwindcss/src/css-parser.ts | 20 ++++++++++++--- 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 .changelog/custom-property-at-end-of-input.md diff --git a/.changelog/custom-property-at-end-of-input.md b/.changelog/custom-property-at-end-of-input.md new file mode 100644 index 000000000000..3b6445b30ecf --- /dev/null +++ b/.changelog/custom-property-at-end-of-input.md @@ -0,0 +1,3 @@ +### Fixed + +- Parse a custom property at the end of the input when it has no trailing `;` (e.g. `--foo: bar` as the last declaration) diff --git a/packages/tailwindcss/src/css-parser.test.ts b/packages/tailwindcss/src/css-parser.test.ts index 47034b8bde97..9956dac3cb29 100644 --- a/packages/tailwindcss/src/css-parser.test.ts +++ b/packages/tailwindcss/src/css-parser.test.ts @@ -457,6 +457,31 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => { ).toEqual([{ kind: 'declaration', property: '--foo', value: 'bar', important: true }]) }) + it('should parse a custom property with a missing ending `;` at the end of the input', () => { + expect(parse('--foo: bar')).toEqual([ + { kind: 'declaration', property: '--foo', value: 'bar', important: false }, + ]) + expect(parse('--foo: bar !important')).toEqual([ + { kind: 'declaration', property: '--foo', value: 'bar', important: true }, + ]) + expect(parse('--foo: var(--bar)')).toEqual([ + { kind: 'declaration', property: '--foo', value: 'var(--bar)', important: false }, + ]) + expect(parse("--foo: 'bar'")).toEqual([ + { kind: 'declaration', property: '--foo', value: "'bar'", important: false }, + ]) + expect(parse('--foo: {}')).toEqual([ + { kind: 'declaration', property: '--foo', value: '{}', important: false }, + ]) + expect(parse('--foo: bar /* baz */')).toEqual([ + { kind: 'declaration', property: '--foo', value: 'bar /* baz */', important: false }, + ]) + expect(parse('--foo: bar;\n--baz: rgb(0 0 0)')).toEqual([ + { kind: 'declaration', property: '--foo', value: 'bar', important: false }, + { kind: 'declaration', property: '--baz', value: 'rgb(0 0 0)', important: false }, + ]) + }) + it('should parse a custom property with an embedded programming language', () => { expect( parse(css` diff --git a/packages/tailwindcss/src/css-parser.ts b/packages/tailwindcss/src/css-parser.ts index 704948aaea69..d5a24cdf5683 100644 --- a/packages/tailwindcss/src/css-parser.ts +++ b/packages/tailwindcss/src/css-parser.ts @@ -277,10 +277,7 @@ export function parse(input: string, opts?: ParseOptions) { // ^ // } // ``` - else if ( - (peekChar === CLOSE_CURLY || input.length - 1 === j) && - closingBracketStack.length === 0 - ) { + else if (peekChar === CLOSE_CURLY && closingBracketStack.length === 0) { i = j - 1 buffer += input.slice(start, j) break @@ -310,6 +307,21 @@ export function parse(input: string, opts?: ParseOptions) { } } + // We reached the end of the input without finding a `;` or `}` that ends + // the custom property, which means that the rest of the input is part of + // the custom property. + // + // E.g.: + // + // ```css + // --custom: value + // ^ + // ``` + if (buffer.length === 0) { + buffer = input.slice(start) + i = input.length + } + let declaration = parseDeclaration(buffer, colonIdx, valueEnd) if (!declaration) { throw new CssSyntaxError(