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