You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6 KiB

4 years ago
  1. var walk = require('css-tree').walk;
  2. var utils = require('./utils');
  3. function processRule(node, item, list) {
  4. var selectors = node.prelude.children;
  5. var declarations = node.block.children;
  6. list.prevUntil(item.prev, function(prev) {
  7. // skip non-ruleset node if safe
  8. if (prev.type !== 'Rule') {
  9. return utils.unsafeToSkipNode.call(selectors, prev);
  10. }
  11. var prevSelectors = prev.prelude.children;
  12. var prevDeclarations = prev.block.children;
  13. // try to join rulesets with equal pseudo signature
  14. if (node.pseudoSignature === prev.pseudoSignature) {
  15. // try to join by selectors
  16. if (utils.isEqualSelectors(prevSelectors, selectors)) {
  17. prevDeclarations.appendList(declarations);
  18. list.remove(item);
  19. return true;
  20. }
  21. // try to join by declarations
  22. if (utils.isEqualDeclarations(declarations, prevDeclarations)) {
  23. utils.addSelectors(prevSelectors, selectors);
  24. list.remove(item);
  25. return true;
  26. }
  27. }
  28. // go to prev ruleset if has no selector similarities
  29. return utils.hasSimilarSelectors(selectors, prevSelectors);
  30. });
  31. }
  32. // NOTE: direction should be left to right, since rulesets merge to left
  33. // ruleset. When direction right to left unmerged rulesets may prevent lookup
  34. // TODO: remove initial merge
  35. module.exports = function initialMergeRule(ast) {
  36. walk(ast, {
  37. visit: 'Rule',
  38. enter: processRule
  39. });
  40. };