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.

50 lines
2.1 KiB

4 years ago
  1. var isMergeable = require('./is-mergeable');
  2. var optimizeProperties = require('./properties/optimize');
  3. var sortSelectors = require('../level-1/sort-selectors');
  4. var tidyRules = require('../level-1/tidy-rules');
  5. var OptimizationLevel = require('../../options/optimization-level').OptimizationLevel;
  6. var serializeBody = require('../../writer/one-time').body;
  7. var serializeRules = require('../../writer/one-time').rules;
  8. var Token = require('../../tokenizer/token');
  9. function mergeAdjacent(tokens, context) {
  10. var lastToken = [null, [], []];
  11. var options = context.options;
  12. var adjacentSpace = options.compatibility.selectors.adjacentSpace;
  13. var selectorsSortingMethod = options.level[OptimizationLevel.One].selectorsSortingMethod;
  14. var mergeablePseudoClasses = options.compatibility.selectors.mergeablePseudoClasses;
  15. var mergeablePseudoElements = options.compatibility.selectors.mergeablePseudoElements;
  16. var mergeLimit = options.compatibility.selectors.mergeLimit;
  17. var multiplePseudoMerging = options.compatibility.selectors.multiplePseudoMerging;
  18. for (var i = 0, l = tokens.length; i < l; i++) {
  19. var token = tokens[i];
  20. if (token[0] != Token.RULE) {
  21. lastToken = [null, [], []];
  22. continue;
  23. }
  24. if (lastToken[0] == Token.RULE && serializeRules(token[1]) == serializeRules(lastToken[1])) {
  25. Array.prototype.push.apply(lastToken[2], token[2]);
  26. optimizeProperties(lastToken[2], true, true, context);
  27. token[2] = [];
  28. } else if (lastToken[0] == Token.RULE && serializeBody(token[2]) == serializeBody(lastToken[2]) &&
  29. isMergeable(serializeRules(token[1]), mergeablePseudoClasses, mergeablePseudoElements, multiplePseudoMerging) &&
  30. isMergeable(serializeRules(lastToken[1]), mergeablePseudoClasses, mergeablePseudoElements, multiplePseudoMerging) &&
  31. lastToken[1].length < mergeLimit) {
  32. lastToken[1] = tidyRules(lastToken[1].concat(token[1]), false, adjacentSpace, false, context.warnings);
  33. lastToken[1] = lastToken.length > 1 ? sortSelectors(lastToken[1], selectorsSortingMethod) : lastToken[1];
  34. token[2] = [];
  35. } else {
  36. lastToken = token;
  37. }
  38. }
  39. }
  40. module.exports = mergeAdjacent;