Skip to content
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
Copy rule for declarations after nested rule
  • Loading branch information
demikhovr committed Jul 4, 2020
commit f7f74e79c7f292191f6ff12da91a81e8e27e0200
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,42 @@ function atruleChilds (rule, atrule, bubbling) {
}
}

function pickDeclarations (selector, declarations, after) {
var parent = postcss.rule({
selector: selector,
nodes: []
})

for (var i = 0; i < declarations.length; i++) {
parent.append(declarations[i])
}

after.after(parent)
return parent
}

function processRule (rule, bubble, unwrap, preserveEmpty) {
var unwrapped = false
var after = rule
var copyDeclarations = false
var declarations = []

rule.each(function (child) {
if (child.type === 'rule') {
if (declarations.length) {
after = pickDeclarations(rule.selector, declarations, after)
declarations = []
}

copyDeclarations = true
unwrapped = true
child.selectors = selectors(rule, child)
after = pickComment(child.prev(), after)
after.after(child)
after = child
} else if (child.type === 'atrule') {
copyDeclarations = false

if (child.name === 'at-root') {
unwrapped = true
atruleChilds(rule, child, false)
Expand Down Expand Up @@ -129,8 +153,15 @@ function processRule (rule, bubble, unwrap, preserveEmpty) {
after.after(child)
after = child
}
} else if (child.type === 'decl' && copyDeclarations) {
declarations.push(child)
}
})

if (declarations.length) {
after = pickDeclarations(rule.selector, declarations, after)
}

if (unwrapped && preserveEmpty !== true) {
rule.raws.semicolon = true
if (rule.nodes.length === 0) rule.remove()
Expand Down
7 changes: 7 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ it('saves order of rules', () => {
)
})

it('copy rule for declarations after nested rule', () => {
return run(
'a { a: 1; &b { b: 2 } c: 1; &c { d: 5 } e: 6 } c { f: 1 }',
'a { a: 1; } ab { b: 2 } a { c: 1; } ac { d: 5 } a { e: 6; } c { f: 1 }'
)
})

it('does not replace ampersand inside string', () => {
return run(
'div { &[data-category="sound & vision"] {} }',
Expand Down