source: imaps-frontend/node_modules/clean-css/lib/optimizer/level-2/merge-media-queries.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.1 KB
Line 
1var canReorder = require('./reorderable').canReorder;
2var canReorderSingle = require('./reorderable').canReorderSingle;
3var extractProperties = require('./extract-properties');
4var rulesOverlap = require('./rules-overlap');
5
6var serializeRules = require('../../writer/one-time').rules;
7var OptimizationLevel = require('../../options/optimization-level').OptimizationLevel;
8var Token = require('../../tokenizer/token');
9
10function mergeMediaQueries(tokens, context) {
11 var mergeSemantically = context.options.level[OptimizationLevel.Two].mergeSemantically;
12 var specificityCache = context.cache.specificity;
13 var candidates = {};
14 var reduced = [];
15
16 for (var i = tokens.length - 1; i >= 0; i--) {
17 var token = tokens[i];
18 if (token[0] != Token.NESTED_BLOCK) {
19 continue;
20 }
21
22 var key = serializeRules(token[1]);
23 var candidate = candidates[key];
24 if (!candidate) {
25 candidate = [];
26 candidates[key] = candidate;
27 }
28
29 candidate.push(i);
30 }
31
32 for (var name in candidates) {
33 var positions = candidates[name];
34
35 positionLoop:
36 for (var j = positions.length - 1; j > 0; j--) {
37 var positionOne = positions[j];
38 var tokenOne = tokens[positionOne];
39 var positionTwo = positions[j - 1];
40 var tokenTwo = tokens[positionTwo];
41
42 directionLoop:
43 for (var direction = 1; direction >= -1; direction -= 2) {
44 var topToBottom = direction == 1;
45 var from = topToBottom ? positionOne + 1 : positionTwo - 1;
46 var to = topToBottom ? positionTwo : positionOne;
47 var delta = topToBottom ? 1 : -1;
48 var source = topToBottom ? tokenOne : tokenTwo;
49 var target = topToBottom ? tokenTwo : tokenOne;
50 var movedProperties = extractProperties(source);
51
52 while (from != to) {
53 var traversedProperties = extractProperties(tokens[from]);
54 from += delta;
55
56 if (mergeSemantically
57 && allSameRulePropertiesCanBeReordered(movedProperties, traversedProperties, specificityCache)
58 ) {
59 continue;
60 }
61
62 if (!canReorder(movedProperties, traversedProperties, specificityCache)) { continue directionLoop; }
63 }
64
65 target[2] = topToBottom
66 ? source[2].concat(target[2])
67 : target[2].concat(source[2]);
68 source[2] = [];
69
70 reduced.push(target);
71 continue positionLoop;
72 }
73 }
74 }
75
76 return reduced;
77}
78
79function allSameRulePropertiesCanBeReordered(movedProperties, traversedProperties, specificityCache) {
80 var movedProperty;
81 var movedRule;
82 var traversedProperty;
83 var traversedRule;
84 var i, l;
85 var j, m;
86
87 for (i = 0, l = movedProperties.length; i < l; i++) {
88 movedProperty = movedProperties[i];
89 movedRule = movedProperty[5];
90
91 for (j = 0, m = traversedProperties.length; j < m; j++) {
92 traversedProperty = traversedProperties[j];
93 traversedRule = traversedProperty[5];
94
95 if (rulesOverlap(movedRule, traversedRule, true)
96 && !canReorderSingle(movedProperty, traversedProperty, specificityCache)) {
97 return false;
98 }
99 }
100 }
101
102 return true;
103}
104
105module.exports = mergeMediaQueries;
Note: See TracBrowser for help on using the repository browser.