Skip to content

Skip newlines when parsing selectors #13280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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