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.

157 lines
5.7 KiB

4 years ago
  1. var List = require('css-tree').List;
  2. var walk = require('css-tree').walk;
  3. var utils = require('./utils');
  4. function calcSelectorLength(list) {
  5. var length = 0;
  6. list.each(function(data) {
  7. length += data.id.length + 1;
  8. });
  9. return length - 1;
  10. }
  11. function calcDeclarationsLength(tokens) {
  12. var length = 0;
  13. for (var i = 0; i < tokens.length; i++) {
  14. length += tokens[i].length;
  15. }
  16. return (
  17. length + // declarations
  18. tokens.length - 1 // delimeters
  19. );
  20. }
  21. function processRule(node, item, list) {
  22. var avoidRulesMerge = this.block !== null ? this.block.avoidRulesMerge : false;
  23. var selectors = node.prelude.children;
  24. var block = node.block;
  25. var disallowDownMarkers = Object.create(null);
  26. var allowMergeUp = true;
  27. var allowMergeDown = true;
  28. list.prevUntil(item.prev, function(prev, prevItem) {
  29. // skip non-ruleset node if safe
  30. if (prev.type !== 'Rule') {
  31. return utils.unsafeToSkipNode.call(selectors, prev);
  32. }
  33. var prevSelectors = prev.prelude.children;
  34. var prevBlock = prev.block;
  35. if (node.pseudoSignature !== prev.pseudoSignature) {
  36. return true;
  37. }
  38. allowMergeDown = !prevSelectors.some(function(selector) {
  39. return selector.compareMarker in disallowDownMarkers;
  40. });
  41. // try prev ruleset if simpleselectors has no equal specifity and element selector
  42. if (!allowMergeDown && !allowMergeUp) {
  43. return true;
  44. }
  45. // try to join by selectors
  46. if (allowMergeUp && utils.isEqualSelectors(prevSelectors, selectors)) {
  47. prevBlock.children.appendList(block.children);
  48. list.remove(item);
  49. return true;
  50. }
  51. // try to join by properties
  52. var diff = utils.compareDeclarations(block.children, prevBlock.children);
  53. // console.log(diff.eq, diff.ne1, diff.ne2);
  54. if (diff.eq.length) {
  55. if (!diff.ne1.length && !diff.ne2.length) {
  56. // equal blocks
  57. if (allowMergeDown) {
  58. utils.addSelectors(selectors, prevSelectors);
  59. list.remove(prevItem);
  60. }
  61. return true;
  62. } else if (!avoidRulesMerge) { /* probably we don't need to prevent those merges for @keyframes
  63. TODO: need to be checked */
  64. if (diff.ne1.length && !diff.ne2.length) {
  65. // prevBlock is subset block
  66. var selectorLength = calcSelectorLength(selectors);
  67. var blockLength = calcDeclarationsLength(diff.eq); // declarations length
  68. if (allowMergeUp && selectorLength < blockLength) {
  69. utils.addSelectors(prevSelectors, selectors);
  70. block.children = new List().fromArray(diff.ne1);
  71. }
  72. } else if (!diff.ne1.length && diff.ne2.length) {
  73. // node is subset of prevBlock
  74. var selectorLength = calcSelectorLength(prevSelectors);
  75. var blockLength = calcDeclarationsLength(diff.eq); // declarations length
  76. if (allowMergeDown && selectorLength < blockLength) {
  77. utils.addSelectors(selectors, prevSelectors);
  78. prevBlock.children = new List().fromArray(diff.ne2);
  79. }
  80. } else {
  81. // diff.ne1.length && diff.ne2.length
  82. // extract equal block
  83. var newSelector = {
  84. type: 'SelectorList',
  85. loc: null,
  86. children: utils.addSelectors(prevSelectors.copy(), selectors)
  87. };
  88. var newBlockLength = calcSelectorLength(newSelector.children) + 2; // selectors length + curly braces length
  89. var blockLength = calcDeclarationsLength(diff.eq); // declarations length
  90. // create new ruleset if declarations length greater than
  91. // ruleset description overhead
  92. if (allowMergeDown && blockLength >= newBlockLength) {
  93. var newRule = {
  94. type: 'Rule',
  95. loc: null,
  96. prelude: newSelector,
  97. block: {
  98. type: 'Block',
  99. loc: null,
  100. children: new List().fromArray(diff.eq)
  101. },
  102. pseudoSignature: node.pseudoSignature
  103. };
  104. block.children = new List().fromArray(diff.ne1);
  105. prevBlock.children = new List().fromArray(diff.ne2overrided);
  106. list.insert(list.createItem(newRule), prevItem);
  107. return true;
  108. }
  109. }
  110. }
  111. }
  112. if (allowMergeUp) {
  113. // TODO: disallow up merge only if any property interception only (i.e. diff.ne2overrided.length > 0);
  114. // await property families to find property interception correctly
  115. allowMergeUp = !prevSelectors.some(function(prevSelector) {
  116. return selectors.some(function(selector) {
  117. return selector.compareMarker === prevSelector.compareMarker;
  118. });
  119. });
  120. }
  121. prevSelectors.each(function(data) {
  122. disallowDownMarkers[data.compareMarker] = true;
  123. });
  124. });
  125. }
  126. module.exports = function restructRule(ast) {
  127. walk(ast, {
  128. visit: 'Rule',
  129. reverse: true,
  130. enter: processRule
  131. });
  132. };