Skip to content

Commit

Permalink
Skip newlines when parsing selectors (#13280)
Browse files Browse the repository at this point in the history
* replace "\n" with " " when parsing CSS

* add test to ensure important whitespace is preserved
  • Loading branch information
RobinMalfait authored Mar 19, 2024
1 parent f3e7880 commit d2be632
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/tailwindcss/src/css-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,28 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
},
])
})

it('should parse a multi-line selector', () => {
expect(parse(['.foo,', '.bar,', '.baz', '{', 'color:red;', '}'].join('\n'))).toEqual([
{
kind: 'rule',
selector: '.foo, .bar, .baz',
nodes: [{ kind: 'declaration', property: 'color', value: 'red', important: false }],
},
])
})

it('should parse a multi-line selector and preserves important whitespace', () => {
expect(
parse(['.foo,', '.bar,', '.baz\t\n \n .qux', '{', 'color:red;', '}'].join('\n')),
).toEqual([
{
kind: 'rule',
selector: '.foo, .bar, .baz .qux',
nodes: [{ kind: 'declaration', property: 'color', value: 'red', important: false }],
},
])
})
})

describe('at-rules', () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/tailwindcss/src/css-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ export function parse(input: string) {
continue
}

// Replace new lines with spaces.
else if (char === '\n') {
if (current.length === 0) continue

let last = current[current.length - 1]
if (last !== ' ' && last !== '\n' && last !== '\t') {
current += ' '
}
}

// Start of a custom property.
//
// Custom properties are very permissive and can contain almost any
Expand Down

0 comments on commit d2be632

Please sign in to comment.