3-disjoinRuleset.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var List = require('../../utils/list.js');
  2. var walkRulesRight = require('../../utils/walk.js').rulesRight;
  3. function processRuleset(node, item, list) {
  4. var selectors = node.selector.selectors;
  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: 'Ruleset',
  16. info: node.info,
  17. pseudoSignature: node.pseudoSignature,
  18. selector: {
  19. type: 'Selector',
  20. info: node.selector.info,
  21. selectors: newSelectors
  22. },
  23. block: {
  24. type: 'Block',
  25. info: node.block.info,
  26. declarations: node.block.declarations.copy()
  27. }
  28. }), item);
  29. }
  30. };
  31. module.exports = function disjoinRuleset(ast) {
  32. walkRulesRight(ast, function(node, item, list) {
  33. if (node.type === 'Ruleset') {
  34. processRuleset(node, item, list);
  35. }
  36. });
  37. };