source: imaps-frontend/node_modules/clean-css/lib/optimizer/level-2/merge-non-adjacent-by-body.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 3.0 KB
Line 
1var isMergeable = require('./is-mergeable');
2
3var sortSelectors = require('../level-1/sort-selectors');
4var tidyRules = require('../level-1/tidy-rules');
5
6var OptimizationLevel = require('../../options/optimization-level').OptimizationLevel;
7
8var serializeBody = require('../../writer/one-time').body;
9var serializeRules = require('../../writer/one-time').rules;
10
11var Token = require('../../tokenizer/token');
12
13function unsafeSelector(value) {
14 return /\.|\*| :/.test(value);
15}
16
17function isBemElement(token) {
18 var asString = serializeRules(token[1]);
19 return asString.indexOf('__') > -1 || asString.indexOf('--') > -1;
20}
21
22function withoutModifier(selector) {
23 return selector.replace(/--[^ ,>+~:]+/g, '');
24}
25
26function 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
39function 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
88module.exports = mergeNonAdjacentByBody;
Note: See TracBrowser for help on using the repository browser.