Spaces:
Running
Running
File size: 3,955 Bytes
a5f860b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
var mergeAdjacent = require('./merge-adjacent');
var mergeMediaQueries = require('./merge-media-queries');
var mergeNonAdjacentByBody = require('./merge-non-adjacent-by-body');
var mergeNonAdjacentBySelector = require('./merge-non-adjacent-by-selector');
var reduceNonAdjacent = require('./reduce-non-adjacent');
var removeDuplicateFontAtRules = require('./remove-duplicate-font-at-rules');
var removeDuplicateMediaQueries = require('./remove-duplicate-media-queries');
var removeDuplicates = require('./remove-duplicates');
var removeUnusedAtRules = require('./remove-unused-at-rules');
var restructure = require('./restructure');
var optimizeProperties = require('./properties/optimize');
var OptimizationLevel = require('../../options/optimization-level').OptimizationLevel;
var Token = require('../../tokenizer/token');
function removeEmpty(tokens) {
for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
var isEmpty = false;
switch (token[0]) {
case Token.RULE:
isEmpty = token[1].length === 0 || token[2].length === 0;
break;
case Token.NESTED_BLOCK:
removeEmpty(token[2]);
isEmpty = token[2].length === 0;
break;
case Token.AT_RULE:
isEmpty = token[1].length === 0;
break;
case Token.AT_RULE_BLOCK:
isEmpty = token[2].length === 0;
}
if (isEmpty) {
tokens.splice(i, 1);
i--;
l--;
}
}
}
function recursivelyOptimizeBlocks(tokens, context) {
for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
if (token[0] == Token.NESTED_BLOCK) {
var isKeyframes = /@(-moz-|-o-|-webkit-)?keyframes/.test(token[1][0][1]);
level2Optimize(token[2], context, !isKeyframes);
}
}
}
function recursivelyOptimizeProperties(tokens, context) {
for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
switch (token[0]) {
case Token.RULE:
optimizeProperties(token[2], true, true, context);
break;
case Token.NESTED_BLOCK:
recursivelyOptimizeProperties(token[2], context);
}
}
}
function level2Optimize(tokens, context, withRestructuring) {
var levelOptions = context.options.level[OptimizationLevel.Two];
var level2Plugins = context.options.plugins.level2Block;
var reduced;
var i;
recursivelyOptimizeBlocks(tokens, context);
recursivelyOptimizeProperties(tokens, context);
if (levelOptions.removeDuplicateRules) {
removeDuplicates(tokens, context);
}
if (levelOptions.mergeAdjacentRules) {
mergeAdjacent(tokens, context);
}
if (levelOptions.reduceNonAdjacentRules) {
reduceNonAdjacent(tokens, context);
}
if (levelOptions.mergeNonAdjacentRules && levelOptions.mergeNonAdjacentRules != 'body') {
mergeNonAdjacentBySelector(tokens, context);
}
if (levelOptions.mergeNonAdjacentRules && levelOptions.mergeNonAdjacentRules != 'selector') {
mergeNonAdjacentByBody(tokens, context);
}
if (levelOptions.restructureRules && levelOptions.mergeAdjacentRules && withRestructuring) {
restructure(tokens, context);
mergeAdjacent(tokens, context);
}
if (levelOptions.restructureRules && !levelOptions.mergeAdjacentRules && withRestructuring) {
restructure(tokens, context);
}
if (levelOptions.removeDuplicateFontRules) {
removeDuplicateFontAtRules(tokens, context);
}
if (levelOptions.removeDuplicateMediaBlocks) {
removeDuplicateMediaQueries(tokens, context);
}
if (levelOptions.removeUnusedAtRules) {
removeUnusedAtRules(tokens, context);
}
if (levelOptions.mergeMedia) {
reduced = mergeMediaQueries(tokens, context);
for (i = reduced.length - 1; i >= 0; i--) {
level2Optimize(reduced[i][2], context, false);
}
}
for (i = 0; i < level2Plugins.length; i++) {
level2Plugins[i](tokens);
}
if (levelOptions.removeEmpty) {
removeEmpty(tokens);
}
return tokens;
}
module.exports = level2Optimize;
|