Skip to content

Commit

Permalink
Handle duplicate atrules without children
Browse files Browse the repository at this point in the history
We assumed that all At Rule nodes had children. Including ones that do not and should not — for example `@import url(…);`.

Since this is not the case we’ll skip adding children for nodes that don’t have any
  • Loading branch information
thecrypticace committed Apr 15, 2022
1 parent 5c76de7 commit ac8db14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/lib/collapseAdjacentRules.js
Expand Up @@ -29,7 +29,11 @@ export default function collapseAdjacentRules() {
(currentRule[property] ?? '').replace(/\s+/g, ' ')
)
) {
currentRule.append(node.nodes)
// An AtRule may not have children (for example if we encounter duplicate @import url(…) rules)
if (node.nodes) {
currentRule.append(node.nodes)
}

node.remove()
} else {
currentRule = node
Expand Down
20 changes: 19 additions & 1 deletion tests/collapse-adjacent-rules.test.js
@@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'

import { run, css } from './util/run'
import { run, html, css } from './util/run'

test('collapse adjacent rules', () => {
let config = {
Expand Down Expand Up @@ -61,3 +61,21 @@ test('collapse adjacent rules', () => {
expect(result.css).toMatchFormattedCss(expected)
})
})

test('duplicate url imports does not break rule collapsing', () => {
let config = {
content: [{ raw: html`` }],
corePlugins: { preflight: false },
}

let input = css`
@import url('https://example.com');
@import url('https://example.com');
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
@import url('https://example.com');
`)
})
})

0 comments on commit ac8db14

Please sign in to comment.