diff --git a/src/compiler/compile/css/Stylesheet.ts b/src/compiler/compile/css/Stylesheet.ts index 6267d0afb8..54427b183f 100644 --- a/src/compiler/compile/css/Stylesheet.ts +++ b/src/compiler/compile/css/Stylesheet.ts @@ -269,24 +269,19 @@ export default class Stylesheet { this.has_styles = true; - const stack: Array = []; + const stack: Array = []; + let depth = 0; let current_atrule: Atrule = null; walk(ast.css, { enter: (node: Node) => { if (node.type === 'Atrule') { - const last = stack[stack.length - 1]; - const atrule = new Atrule(node); stack.push(atrule); - // this is an awkward special case — @apply (and - // possibly other future constructs) - if (last && !(last instanceof Atrule)) return; - if (current_atrule) { current_atrule.children.push(atrule); - } else { + } else if (depth <= 1) { this.children.push(atrule); } @@ -303,26 +298,24 @@ export default class Stylesheet { if (node.type === 'Rule') { const rule = new Rule(node, this, current_atrule); - stack.push(rule); if (current_atrule) { current_atrule.children.push(rule); - } else { + } else if (depth <= 1) { this.children.push(rule); } } + + depth += 1; }, leave: (node: Node) => { - if (node.type === 'Rule' || node.type === 'Atrule') stack.pop(); if (node.type === 'Atrule') { - current_atrule = null; - for (let i = stack.length - 1; i >= 0; i--) { - if (stack[i] instanceof Atrule) { - current_atrule = stack[i] as Atrule; - } - } + stack.pop(); + current_atrule = stack[stack.length - 1]; } + + depth -= 1; } }); } else {