Skip to content

Commit d2be632

Browse files
authored
Skip newlines when parsing selectors (#13280)
* replace "\n" with " " when parsing CSS * add test to ensure important whitespace is preserved
1 parent f3e7880 commit d2be632

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packages/tailwindcss/src/css-parser.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,28 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
600600
},
601601
])
602602
})
603+
604+
it('should parse a multi-line selector', () => {
605+
expect(parse(['.foo,', '.bar,', '.baz', '{', 'color:red;', '}'].join('\n'))).toEqual([
606+
{
607+
kind: 'rule',
608+
selector: '.foo, .bar, .baz',
609+
nodes: [{ kind: 'declaration', property: 'color', value: 'red', important: false }],
610+
},
611+
])
612+
})
613+
614+
it('should parse a multi-line selector and preserves important whitespace', () => {
615+
expect(
616+
parse(['.foo,', '.bar,', '.baz\t\n \n .qux', '{', 'color:red;', '}'].join('\n')),
617+
).toEqual([
618+
{
619+
kind: 'rule',
620+
selector: '.foo, .bar, .baz .qux',
621+
nodes: [{ kind: 'declaration', property: 'color', value: 'red', important: false }],
622+
},
623+
])
624+
})
603625
})
604626

605627
describe('at-rules', () => {

packages/tailwindcss/src/css-parser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ export function parse(input: string) {
142142
continue
143143
}
144144

145+
// Replace new lines with spaces.
146+
else if (char === '\n') {
147+
if (current.length === 0) continue
148+
149+
let last = current[current.length - 1]
150+
if (last !== ' ' && last !== '\n' && last !== '\t') {
151+
current += ' '
152+
}
153+
}
154+
145155
// Start of a custom property.
146156
//
147157
// Custom properties are very permissive and can contain almost any

0 commit comments

Comments
 (0)