Skip newlines when parsing selectors (#13280)

* replace "\n" with " " when parsing CSS

* add test to ensure important whitespace is preserved
This commit is contained in:
Robin Malfait 2024-03-19 16:23:14 +01:00 • committed by GitHub
parent f3e78807af
commit d2be632635
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

View file

@ -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', () => {

View file

@ -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