diff --git a/index.js b/index.js index 81a6f6d..e9125de 100644 --- a/index.js +++ b/index.js @@ -178,11 +178,17 @@ function breakOut(child, currentNode) { function createFnAtruleChilds(bubble) { return function atruleChilds(rule, atrule, bubbling, mergeSels = bubbling) { let children = [] + let groups = [] atrule.each(child => { if (child.type === 'rule' && bubbling) { if (mergeSels) { child.selectors = mergeSelectors(rule, child) } + // Declarations after a nested rule stay after it, like outside at-rules + if (children.length) { + groups.push(children) + children = [] + } } else if (child.type === 'atrule' && child.nodes) { if (bubble[child.name]) { atruleChilds(rule, child, mergeSels) @@ -193,12 +199,15 @@ function createFnAtruleChilds(bubble) { children.push(child) } }) - if (bubbling && children.length) { - let clone = rule.clone({ nodes: [] }) - for (let child of children) { - clone.append(child) + if (children.length) groups.push(children) + if (bubbling) { + for (let group of groups) { + let clone = rule.clone({ nodes: [] }) + group[0].before(clone) + for (let child of group) { + clone.append(child) + } } - atrule.prepend(clone) } } } diff --git a/index.test.js b/index.test.js index 0d219f3..a5ab0fe 100644 --- a/index.test.js +++ b/index.test.js @@ -709,6 +709,17 @@ test('copies rule for declarations after nested rule and before at-rule', () => ) }) +test('copies rule for declarations after nested rule inside at-rule', () => { + run( + 'a { @media screen { a: 1; &b { b: 2 } c: 1; &c { d: 5 } e: 6 } }', + '@media screen { a { a: 1 } ab { b: 2 } a { c: 1 } ac { d: 5 } a { e: 6 } }' + ) + run( + 'a { @supports (a: 1) { &b { b: 2 } c: 1 } }', + '@supports (a: 1) { ab { b: 2 } a { c: 1 } }' + ) +}) + test('does not replace ampersand inside string', () => { run( 'div { &[data-category="sound & vision"] {} }',