diff --git a/index.js b/index.js index c228f54..11a1364 100644 --- a/index.js +++ b/index.js @@ -348,7 +348,17 @@ function unwrapRootRule(rule) { } } + let parent = rule.parent rule.remove() + + if (escapes) { + // Remove at-rules which were left empty after escaping from them + while (parent instanceof AtRule && parent.nodes.length === 0) { + let next = parent.parent + parent.remove() + parent = next + } + } } const ROOT_RULE_MERGE_SEL = Symbol('rootRuleMergeSel') diff --git a/index.test.js b/index.test.js index 916bb66..db2dd17 100644 --- a/index.test.js +++ b/index.test.js @@ -439,6 +439,59 @@ test('at-root supports (without: foo) 5', () => { ) }) +test('at-root removes at-rules left empty after escaping', () => { + run( + `@media x { + a { + @at-root (without: media) { + b {} + } + } + }`, + `a b {}` + ) + run( + `@media x { + a { + @at-root (without: media) { + b {} + } + c: d; + } + }`, + `a b {} + @media x { + a { c: d } + }` + ) + run( + `@media x { + @supports (y:z) { + a { + @at-root (without: media) { + b {} + } + } + } + }`, + `@supports (y:z) { + a b {} + }` + ) + run( + `@supports (y:z) { + @media x { + a { + @at-root (without: all) { + b {} + } + } + } + }`, + `b {}` + ) +}) + test('replaces ampersand', () => { run('a { body &:hover b {} }', 'body a:hover b {}') })