Skip to content

Custom at rules nesting issues #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,32 @@ function pickComment (comment, after) {
}
}

function atruleChilds (rule, atrule, bubbling) {
let children = []
atrule.each(child => {
if (child.type === 'comment') {
children.push(child)
} else if (child.type === 'decl') {
children.push(child)
} else if (child.type === 'rule' && bubbling) {
child.selectors = selectors(rule, child)
} else if (child.type === 'atrule') {
atruleChilds(rule, child, bubbling)
}
})
if (bubbling) {
if (children.length) {
let clone = rule.clone({ nodes: [] })
for (let child of children) {
clone.append(child)
function createFnAtruleChilds (bubble) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a wrapper - currying to have bubble available in the scope.

return function atruleChilds (rule, atrule, bubbling) {
let children = []
atrule.each(child => {
if (child.type === 'comment') {
children.push(child)
} else if (child.type === 'decl') {
children.push(child)
} else if (child.type === 'rule' && bubbling) {
child.selectors = selectors(rule, child)
} else if (child.type === 'atrule') {
if (child.nodes && bubble[child.name]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only bubble if has children and actually should bubble, otherwise push as a "casual" child node.

atruleChilds(rule, child, true)
} else {
children.push(child)
}
}
})
if (bubbling) {
if (children.length) {
let clone = rule.clone({ nodes: [] })
for (let child of children) {
clone.append(child)
}
atrule.prepend(clone)
}
atrule.prepend(clone)
}
}
}
Expand Down Expand Up @@ -120,6 +126,7 @@ function atruleNames (defaults, custom) {

module.exports = (opts = {}) => {
let bubble = atruleNames(['media', 'supports'], opts.bubble)
let atruleChilds = createFnAtruleChilds(bubble)
let unwrap = atruleNames(
[
'document',
Expand Down
13 changes: 10 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,21 @@ it('unwraps at-rules with interleaved properties', () => {
)
})

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

it('does not move custom at-rules placed under nested bubbling ones', () => {
run(
'.one { @supports (color: black) { @media screen { @mixin test; } } } .two { @supports (color: black) { @media screen { @mixin test { color: black } } } }',
'@supports (color: black) { @media screen {.one { @mixin test } } } @supports (color: black) { @media screen { .two { @mixin test { color: black } } } }'
)
})

it('supports bubble option with at-name', () => {
run('a { @phone { color: black } }', '@phone {a { color: black } }', {
bubble: ['@phone']
Expand Down