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(