a zip code crypto-currency system good for red ONLY

merge-non-adjacent-by-body.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var isMergeable = require('./is-mergeable');
  2. var sortSelectors = require('../level-1/sort-selectors');
  3. var tidyRules = require('../level-1/tidy-rules');
  4. var OptimizationLevel = require('../../options/optimization-level').OptimizationLevel;
  5. var serializeBody = require('../../writer/one-time').body;
  6. var serializeRules = require('../../writer/one-time').rules;
  7. var Token = require('../../tokenizer/token');
  8. function unsafeSelector(value) {
  9. return /\.|\*| :/.test(value);
  10. }
  11. function isBemElement(token) {
  12. var asString = serializeRules(token[1]);
  13. return asString.indexOf('__') > -1 || asString.indexOf('--') > -1;
  14. }
  15. function withoutModifier(selector) {
  16. return selector.replace(/--[^ ,>\+~:]+/g, '');
  17. }
  18. function removeAnyUnsafeElements(left, candidates) {
  19. var leftSelector = withoutModifier(serializeRules(left[1]));
  20. for (var body in candidates) {
  21. var right = candidates[body];
  22. var rightSelector = withoutModifier(serializeRules(right[1]));
  23. if (rightSelector.indexOf(leftSelector) > -1 || leftSelector.indexOf(rightSelector) > -1)
  24. delete candidates[body];
  25. }
  26. }
  27. function mergeNonAdjacentByBody(tokens, context) {
  28. var options = context.options;
  29. var mergeSemantically = options.level[OptimizationLevel.Two].mergeSemantically;
  30. var adjacentSpace = options.compatibility.selectors.adjacentSpace;
  31. var selectorsSortingMethod = options.level[OptimizationLevel.One].selectorsSortingMethod;
  32. var mergeablePseudoClasses = options.compatibility.selectors.mergeablePseudoClasses;
  33. var mergeablePseudoElements = options.compatibility.selectors.mergeablePseudoElements;
  34. var multiplePseudoMerging = options.compatibility.selectors.multiplePseudoMerging;
  35. var candidates = {};
  36. for (var i = tokens.length - 1; i >= 0; i--) {
  37. var token = tokens[i];
  38. if (token[0] != Token.RULE)
  39. continue;
  40. if (token[2].length > 0 && (!mergeSemantically && unsafeSelector(serializeRules(token[1]))))
  41. candidates = {};
  42. if (token[2].length > 0 && mergeSemantically && isBemElement(token))
  43. removeAnyUnsafeElements(token, candidates);
  44. var candidateBody = serializeBody(token[2]);
  45. var oldToken = candidates[candidateBody];
  46. if (oldToken &&
  47. isMergeable(serializeRules(token[1]), mergeablePseudoClasses, mergeablePseudoElements, multiplePseudoMerging) &&
  48. isMergeable(serializeRules(oldToken[1]), mergeablePseudoClasses, mergeablePseudoElements, multiplePseudoMerging)) {
  49. if (token[2].length > 0) {
  50. token[1] = tidyRules(oldToken[1].concat(token[1]), false, adjacentSpace, false, context.warnings);
  51. token[1] = token[1].length > 1 ? sortSelectors(token[1], selectorsSortingMethod) : token[1];
  52. } else {
  53. token[1] = oldToken[1].concat(token[1]);
  54. }
  55. oldToken[2] = [];
  56. candidates[candidateBody] = null;
  57. }
  58. candidates[serializeBody(token[2])] = token;
  59. }
  60. }
  61. module.exports = mergeNonAdjacentByBody;