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.

180 lines
5.2 KiB

4 years ago
  1. var isMergeable = require('./is-mergeable');
  2. var optimizeProperties = require('./properties/optimize');
  3. var cloneArray = require('../../utils/clone-array');
  4. var Token = require('../../tokenizer/token');
  5. var serializeBody = require('../../writer/one-time').body;
  6. var serializeRules = require('../../writer/one-time').rules;
  7. function reduceNonAdjacent(tokens, context) {
  8. var options = context.options;
  9. var mergeablePseudoClasses = options.compatibility.selectors.mergeablePseudoClasses;
  10. var mergeablePseudoElements = options.compatibility.selectors.mergeablePseudoElements;
  11. var multiplePseudoMerging = options.compatibility.selectors.multiplePseudoMerging;
  12. var candidates = {};
  13. var repeated = [];
  14. for (var i = tokens.length - 1; i >= 0; i--) {
  15. var token = tokens[i];
  16. if (token[0] != Token.RULE) {
  17. continue;
  18. } else if (token[2].length === 0) {
  19. continue;
  20. }
  21. var selectorAsString = serializeRules(token[1]);
  22. var isComplexAndNotSpecial = token[1].length > 1 &&
  23. isMergeable(selectorAsString, mergeablePseudoClasses, mergeablePseudoElements, multiplePseudoMerging);
  24. var wrappedSelectors = wrappedSelectorsFrom(token[1]);
  25. var selectors = isComplexAndNotSpecial ?
  26. [selectorAsString].concat(wrappedSelectors) :
  27. [selectorAsString];
  28. for (var j = 0, m = selectors.length; j < m; j++) {
  29. var selector = selectors[j];
  30. if (!candidates[selector])
  31. candidates[selector] = [];
  32. else
  33. repeated.push(selector);
  34. candidates[selector].push({
  35. where: i,
  36. list: wrappedSelectors,
  37. isPartial: isComplexAndNotSpecial && j > 0,
  38. isComplex: isComplexAndNotSpecial && j === 0
  39. });
  40. }
  41. }
  42. reduceSimpleNonAdjacentCases(tokens, repeated, candidates, options, context);
  43. reduceComplexNonAdjacentCases(tokens, candidates, options, context);
  44. }
  45. function wrappedSelectorsFrom(list) {
  46. var wrapped = [];
  47. for (var i = 0; i < list.length; i++) {
  48. wrapped.push([list[i][1]]);
  49. }
  50. return wrapped;
  51. }
  52. function reduceSimpleNonAdjacentCases(tokens, repeated, candidates, options, context) {
  53. function filterOut(idx, bodies) {
  54. return data[idx].isPartial && bodies.length === 0;
  55. }
  56. function reduceBody(token, newBody, processedCount, tokenIdx) {
  57. if (!data[processedCount - tokenIdx - 1].isPartial)
  58. token[2] = newBody;
  59. }
  60. for (var i = 0, l = repeated.length; i < l; i++) {
  61. var selector = repeated[i];
  62. var data = candidates[selector];
  63. reduceSelector(tokens, data, {
  64. filterOut: filterOut,
  65. callback: reduceBody
  66. }, options, context);
  67. }
  68. }
  69. function reduceComplexNonAdjacentCases(tokens, candidates, options, context) {
  70. var mergeablePseudoClasses = options.compatibility.selectors.mergeablePseudoClasses;
  71. var mergeablePseudoElements = options.compatibility.selectors.mergeablePseudoElements;
  72. var multiplePseudoMerging = options.compatibility.selectors.multiplePseudoMerging;
  73. var localContext = {};
  74. function filterOut(idx) {
  75. return localContext.data[idx].where < localContext.intoPosition;
  76. }
  77. function collectReducedBodies(token, newBody, processedCount, tokenIdx) {
  78. if (tokenIdx === 0)
  79. localContext.reducedBodies.push(newBody);
  80. }
  81. allSelectors:
  82. for (var complexSelector in candidates) {
  83. var into = candidates[complexSelector];
  84. if (!into[0].isComplex)
  85. continue;
  86. var intoPosition = into[into.length - 1].where;
  87. var intoToken = tokens[intoPosition];
  88. var reducedBodies = [];
  89. var selectors = isMergeable(complexSelector, mergeablePseudoClasses, mergeablePseudoElements, multiplePseudoMerging) ?
  90. into[0].list :
  91. [complexSelector];
  92. localContext.intoPosition = intoPosition;
  93. localContext.reducedBodies = reducedBodies;
  94. for (var j = 0, m = selectors.length; j < m; j++) {
  95. var selector = selectors[j];
  96. var data = candidates[selector];
  97. if (data.length < 2)
  98. continue allSelectors;
  99. localContext.data = data;
  100. reduceSelector(tokens, data, {
  101. filterOut: filterOut,
  102. callback: collectReducedBodies
  103. }, options, context);
  104. if (serializeBody(reducedBodies[reducedBodies.length - 1]) != serializeBody(reducedBodies[0]))
  105. continue allSelectors;
  106. }
  107. intoToken[2] = reducedBodies[0];
  108. }
  109. }
  110. function reduceSelector(tokens, data, context, options, outerContext) {
  111. var bodies = [];
  112. var bodiesAsList = [];
  113. var processedTokens = [];
  114. for (var j = data.length - 1; j >= 0; j--) {
  115. if (context.filterOut(j, bodies))
  116. continue;
  117. var where = data[j].where;
  118. var token = tokens[where];
  119. var clonedBody = cloneArray(token[2]);
  120. bodies = bodies.concat(clonedBody);
  121. bodiesAsList.push(clonedBody);
  122. processedTokens.push(where);
  123. }
  124. optimizeProperties(bodies, true, false, outerContext);
  125. var processedCount = processedTokens.length;
  126. var propertyIdx = bodies.length - 1;
  127. var tokenIdx = processedCount - 1;
  128. while (tokenIdx >= 0) {
  129. if ((tokenIdx === 0 || (bodies[propertyIdx] && bodiesAsList[tokenIdx].indexOf(bodies[propertyIdx]) > -1)) && propertyIdx > -1) {
  130. propertyIdx--;
  131. continue;
  132. }
  133. var newBody = bodies.splice(propertyIdx + 1);
  134. context.callback(tokens[processedTokens[tokenIdx]], newBody, processedCount, tokenIdx);
  135. tokenIdx--;
  136. }
  137. }
  138. module.exports = reduceNonAdjacent;