1 | var canReorder = require('./reorderable').canReorder;
|
---|
2 | var extractProperties = require('./extract-properties');
|
---|
3 |
|
---|
4 | var optimizeProperties = require('./properties/optimize');
|
---|
5 |
|
---|
6 | var serializeRules = require('../../writer/one-time').rules;
|
---|
7 |
|
---|
8 | var Token = require('../../tokenizer/token');
|
---|
9 |
|
---|
10 | function mergeNonAdjacentBySelector(tokens, context) {
|
---|
11 | var specificityCache = context.cache.specificity;
|
---|
12 | var allSelectors = {};
|
---|
13 | var repeatedSelectors = [];
|
---|
14 | var i;
|
---|
15 |
|
---|
16 | for (i = tokens.length - 1; i >= 0; i--) {
|
---|
17 | if (tokens[i][0] != Token.RULE) { continue; }
|
---|
18 | if (tokens[i][2].length === 0) { continue; }
|
---|
19 |
|
---|
20 | var selector = serializeRules(tokens[i][1]);
|
---|
21 | allSelectors[selector] = [i].concat(allSelectors[selector] || []);
|
---|
22 |
|
---|
23 | if (allSelectors[selector].length == 2) { repeatedSelectors.push(selector); }
|
---|
24 | }
|
---|
25 |
|
---|
26 | for (i = repeatedSelectors.length - 1; i >= 0; i--) {
|
---|
27 | var positions = allSelectors[repeatedSelectors[i]];
|
---|
28 |
|
---|
29 | selectorIterator:
|
---|
30 | for (var j = positions.length - 1; j > 0; j--) {
|
---|
31 | var positionOne = positions[j - 1];
|
---|
32 | var tokenOne = tokens[positionOne];
|
---|
33 | var positionTwo = positions[j];
|
---|
34 | var tokenTwo = tokens[positionTwo];
|
---|
35 |
|
---|
36 | directionIterator:
|
---|
37 | for (var direction = 1; direction >= -1; direction -= 2) {
|
---|
38 | var topToBottom = direction == 1;
|
---|
39 | var from = topToBottom ? positionOne + 1 : positionTwo - 1;
|
---|
40 | var to = topToBottom ? positionTwo : positionOne;
|
---|
41 | var delta = topToBottom ? 1 : -1;
|
---|
42 | var moved = topToBottom ? tokenOne : tokenTwo;
|
---|
43 | var target = topToBottom ? tokenTwo : tokenOne;
|
---|
44 | var movedProperties = extractProperties(moved);
|
---|
45 |
|
---|
46 | while (from != to) {
|
---|
47 | var traversedProperties = extractProperties(tokens[from]);
|
---|
48 | from += delta;
|
---|
49 |
|
---|
50 | // traversed then moved as we move selectors towards the start
|
---|
51 | var reorderable = topToBottom
|
---|
52 | ? canReorder(movedProperties, traversedProperties, specificityCache)
|
---|
53 | : canReorder(traversedProperties, movedProperties, specificityCache);
|
---|
54 |
|
---|
55 | if (!reorderable && !topToBottom) { continue selectorIterator; }
|
---|
56 | if (!reorderable && topToBottom) { continue directionIterator; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (topToBottom) {
|
---|
60 | Array.prototype.push.apply(moved[2], target[2]);
|
---|
61 | target[2] = moved[2];
|
---|
62 | } else {
|
---|
63 | Array.prototype.push.apply(target[2], moved[2]);
|
---|
64 | }
|
---|
65 |
|
---|
66 | optimizeProperties(target[2], true, true, context);
|
---|
67 | moved[2] = [];
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | module.exports = mergeNonAdjacentBySelector;
|
---|