postcss / postcss-nested

PostCSS plugin to unwrap nested rules like how Sass does it.
MIT License
1.15k stars 66 forks source link

Copy rule for declarations after nested rule #93

Closed demikhovr closed 4 years ago

demikhovr commented 4 years ago

There's also another possible solution:

function pickDeclarations (child) {
  var result = []
  var next = child.next()

  while (next && next.type === 'decl') {
    result.push(next)
    next = next.next()
  }

  return result
}

function processRule (rule, bubble, unwrap, preserveEmpty) {
...
if (child.type === 'rule') {
  unwrapped = true
  child.selectors = selectors(rule, child)
  after = pickComment(child.prev(), after)
  var decls = pickDeclarations(child)
  after.after(child)
  after = child

  if (decls.length) {
    var parent = postcss.rule({
      selector: rule.selector,
      nodes: []
    })

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

    after.after(parent)
    after = parent
  }
}
...
ai commented 4 years ago

Released in 4.2.2