Skip to content

Commit 564b03f

Browse files
authored
Custom at rules nesting issues (#137)
* Fix childless custom at-rules nested in bubbling at-rules * Fix non-bubbling custom at-rules with children, nested in bubbling at-rules
1 parent dc7ea9e commit 564b03f

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

index.js

+26-19
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,32 @@ function pickComment (comment, after) {
6666
}
6767
}
6868

69-
function atruleChilds (rule, atrule, bubbling) {
70-
let children = []
71-
atrule.each(child => {
72-
if (child.type === 'comment') {
73-
children.push(child)
74-
} else if (child.type === 'decl') {
75-
children.push(child)
76-
} else if (child.type === 'rule' && bubbling) {
77-
child.selectors = selectors(rule, child)
78-
} else if (child.type === 'atrule') {
79-
atruleChilds(rule, child, bubbling)
80-
}
81-
})
82-
if (bubbling) {
83-
if (children.length) {
84-
let clone = rule.clone({ nodes: [] })
85-
for (let child of children) {
86-
clone.append(child)
69+
function createFnAtruleChilds (bubble) {
70+
return function atruleChilds (rule, atrule, bubbling) {
71+
let children = []
72+
atrule.each(child => {
73+
if (child.type === 'comment') {
74+
children.push(child)
75+
} else if (child.type === 'decl') {
76+
children.push(child)
77+
} else if (child.type === 'rule' && bubbling) {
78+
child.selectors = selectors(rule, child)
79+
} else if (child.type === 'atrule') {
80+
if (child.nodes && bubble[child.name]) {
81+
atruleChilds(rule, child, true)
82+
} else {
83+
children.push(child)
84+
}
85+
}
86+
})
87+
if (bubbling) {
88+
if (children.length) {
89+
let clone = rule.clone({ nodes: [] })
90+
for (let child of children) {
91+
clone.append(child)
92+
}
93+
atrule.prepend(clone)
8794
}
88-
atrule.prepend(clone)
8995
}
9096
}
9197
}
@@ -120,6 +126,7 @@ function atruleNames (defaults, custom) {
120126

121127
module.exports = (opts = {}) => {
122128
let bubble = atruleNames(['media', 'supports'], opts.bubble)
129+
let atruleChilds = createFnAtruleChilds(bubble)
123130
let unwrap = atruleNames(
124131
[
125132
'document',

index.test.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,21 @@ it('unwraps at-rules with interleaved properties', () => {
9494
)
9595
})
9696

97-
it('do not move custom at-rules', () => {
97+
it('does not move custom at-rules', () => {
9898
run(
99-
'.one { @mixin test; } .two { @phone { color: black } }',
100-
'.one { @mixin test; } @phone { .two { color: black } }',
99+
'.one { @mixin test; } .two { @media screen { @mixin test; } } .three { @media screen { @mixin test { color: black } } } .four { @phone { color: black } }',
100+
'.one { @mixin test; } @media screen { .two { @mixin test } } @media screen { .three { @mixin test { color: black } } } @phone { .four { color: black } }',
101101
{ bubble: ['phone'] }
102102
)
103103
})
104104

105+
it('does not move custom at-rules placed under nested bubbling ones', () => {
106+
run(
107+
'.one { @supports (color: black) { @media screen { @mixin test; } } } .two { @supports (color: black) { @media screen { @mixin test { color: black } } } }',
108+
'@supports (color: black) { @media screen {.one { @mixin test } } } @supports (color: black) { @media screen { .two { @mixin test { color: black } } } }'
109+
)
110+
})
111+
105112
it('supports bubble option with at-name', () => {
106113
run('a { @phone { color: black } }', '@phone {a { color: black } }', {
107114
bubble: ['@phone']

0 commit comments

Comments
 (0)