reduce-non-adjacent.js 5.0 KB

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