3-disjoinRuleset.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var List = require('css-tree').List;
  2. var walk = require('css-tree').walk;
  3. function processRule(node, item, list) {
  4. var selectors = node.prelude.children;
  5. // generate new rule sets:
  6. // .a, .b { color: red; }
  7. // ->
  8. // .a { color: red; }
  9. // .b { color: red; }
  10. // while there are more than 1 simple selector split for rulesets
  11. while (selectors.head !== selectors.tail) {
  12. var newSelectors = new List();
  13. newSelectors.insert(selectors.remove(selectors.head));
  14. list.insert(list.createItem({
  15. type: 'Rule',
  16. loc: node.loc,
  17. prelude: {
  18. type: 'SelectorList',
  19. loc: node.prelude.loc,
  20. children: newSelectors
  21. },
  22. block: {
  23. type: 'Block',
  24. loc: node.block.loc,
  25. children: node.block.children.copy()
  26. },
  27. pseudoSignature: node.pseudoSignature
  28. }), item);
  29. }
  30. }
  31. module.exports = function disjoinRule(ast) {
  32. walk(ast, {
  33. visit: 'Rule',
  34. reverse: true,
  35. enter: processRule
  36. });
  37. };