[79a0317] | 1 | var roundingPrecisionFrom = require('./rounding-precision').roundingPrecisionFrom;
|
---|
| 2 |
|
---|
| 3 | var override = require('../utils/override');
|
---|
| 4 |
|
---|
| 5 | var OptimizationLevel = {
|
---|
| 6 | Zero: '0',
|
---|
| 7 | One: '1',
|
---|
| 8 | Two: '2'
|
---|
| 9 | };
|
---|
| 10 |
|
---|
| 11 | var DEFAULTS = {};
|
---|
| 12 |
|
---|
| 13 | DEFAULTS[OptimizationLevel.Zero] = {};
|
---|
| 14 | DEFAULTS[OptimizationLevel.One] = {
|
---|
| 15 | cleanupCharsets: true,
|
---|
| 16 | normalizeUrls: true,
|
---|
| 17 | optimizeBackground: true,
|
---|
| 18 | optimizeBorderRadius: true,
|
---|
| 19 | optimizeFilter: true,
|
---|
| 20 | optimizeFontWeight: true,
|
---|
| 21 | optimizeOutline: true,
|
---|
| 22 | removeEmpty: true,
|
---|
| 23 | removeNegativePaddings: true,
|
---|
| 24 | removeQuotes: true,
|
---|
| 25 | removeWhitespace: true,
|
---|
| 26 | replaceMultipleZeros: true,
|
---|
| 27 | replaceTimeUnits: true,
|
---|
| 28 | replaceZeroUnits: true,
|
---|
| 29 | roundingPrecision: roundingPrecisionFrom(undefined),
|
---|
| 30 | selectorsSortingMethod: 'standard',
|
---|
| 31 | specialComments: 'all',
|
---|
| 32 | tidyAtRules: true,
|
---|
| 33 | tidyBlockScopes: true,
|
---|
| 34 | tidySelectors: true,
|
---|
| 35 | variableValueOptimizers: []
|
---|
| 36 | };
|
---|
| 37 | DEFAULTS[OptimizationLevel.Two] = {
|
---|
| 38 | mergeAdjacentRules: true,
|
---|
| 39 | mergeIntoShorthands: true,
|
---|
| 40 | mergeMedia: true,
|
---|
| 41 | mergeNonAdjacentRules: true,
|
---|
| 42 | mergeSemantically: false,
|
---|
| 43 | overrideProperties: true,
|
---|
| 44 | removeEmpty: true,
|
---|
| 45 | reduceNonAdjacentRules: true,
|
---|
| 46 | removeDuplicateFontRules: true,
|
---|
| 47 | removeDuplicateMediaBlocks: true,
|
---|
| 48 | removeDuplicateRules: true,
|
---|
| 49 | removeUnusedAtRules: false,
|
---|
| 50 | restructureRules: false,
|
---|
| 51 | skipProperties: []
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | var ALL_KEYWORD_1 = '*';
|
---|
| 55 | var ALL_KEYWORD_2 = 'all';
|
---|
| 56 | var FALSE_KEYWORD_1 = 'false';
|
---|
| 57 | var FALSE_KEYWORD_2 = 'off';
|
---|
| 58 | var TRUE_KEYWORD_1 = 'true';
|
---|
| 59 | var TRUE_KEYWORD_2 = 'on';
|
---|
| 60 |
|
---|
| 61 | var LIST_VALUE_SEPARATOR = ',';
|
---|
| 62 | var OPTION_SEPARATOR = ';';
|
---|
| 63 | var OPTION_VALUE_SEPARATOR = ':';
|
---|
| 64 |
|
---|
| 65 | function optimizationLevelFrom(source) {
|
---|
| 66 | var level = override(DEFAULTS, {});
|
---|
| 67 | var Zero = OptimizationLevel.Zero;
|
---|
| 68 | var One = OptimizationLevel.One;
|
---|
| 69 | var Two = OptimizationLevel.Two;
|
---|
| 70 |
|
---|
| 71 | if (undefined === source) {
|
---|
| 72 | delete level[Two];
|
---|
| 73 | return level;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if (typeof source == 'string') {
|
---|
| 77 | source = parseInt(source);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | if (typeof source == 'number' && source === parseInt(Two)) {
|
---|
| 81 | return level;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (typeof source == 'number' && source === parseInt(One)) {
|
---|
| 85 | delete level[Two];
|
---|
| 86 | return level;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if (typeof source == 'number' && source === parseInt(Zero)) {
|
---|
| 90 | delete level[Two];
|
---|
| 91 | delete level[One];
|
---|
| 92 | return level;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | if (typeof source == 'object') {
|
---|
| 96 | source = covertValuesToHashes(source);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | if (One in source && 'roundingPrecision' in source[One]) {
|
---|
| 100 | source[One].roundingPrecision = roundingPrecisionFrom(source[One].roundingPrecision);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | if (Two in source && 'skipProperties' in source[Two] && typeof (source[Two].skipProperties) == 'string') {
|
---|
| 104 | source[Two].skipProperties = source[Two].skipProperties.split(LIST_VALUE_SEPARATOR);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | if (Zero in source || One in source || Two in source) {
|
---|
| 108 | level[Zero] = override(level[Zero], source[Zero]);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (One in source && ALL_KEYWORD_1 in source[One]) {
|
---|
| 112 | level[One] = override(level[One], defaults(One, normalizeValue(source[One][ALL_KEYWORD_1])));
|
---|
| 113 | delete source[One][ALL_KEYWORD_1];
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | if (One in source && ALL_KEYWORD_2 in source[One]) {
|
---|
| 117 | level[One] = override(level[One], defaults(One, normalizeValue(source[One][ALL_KEYWORD_2])));
|
---|
| 118 | delete source[One][ALL_KEYWORD_2];
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | if (One in source || Two in source) {
|
---|
| 122 | level[One] = override(level[One], source[One]);
|
---|
| 123 | } else {
|
---|
| 124 | delete level[One];
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | if (Two in source && ALL_KEYWORD_1 in source[Two]) {
|
---|
| 128 | level[Two] = override(level[Two], defaults(Two, normalizeValue(source[Two][ALL_KEYWORD_1])));
|
---|
| 129 | delete source[Two][ALL_KEYWORD_1];
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | if (Two in source && ALL_KEYWORD_2 in source[Two]) {
|
---|
| 133 | level[Two] = override(level[Two], defaults(Two, normalizeValue(source[Two][ALL_KEYWORD_2])));
|
---|
| 134 | delete source[Two][ALL_KEYWORD_2];
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | if (Two in source) {
|
---|
| 138 | level[Two] = override(level[Two], source[Two]);
|
---|
| 139 | } else {
|
---|
| 140 | delete level[Two];
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | return level;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | function defaults(level, value) {
|
---|
| 147 | var options = override(DEFAULTS[level], {});
|
---|
| 148 | var key;
|
---|
| 149 |
|
---|
| 150 | for (key in options) {
|
---|
| 151 | if (typeof options[key] == 'boolean') {
|
---|
| 152 | options[key] = value;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | return options;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | function normalizeValue(value) {
|
---|
| 160 | switch (value) {
|
---|
| 161 | case FALSE_KEYWORD_1:
|
---|
| 162 | case FALSE_KEYWORD_2:
|
---|
| 163 | return false;
|
---|
| 164 | case TRUE_KEYWORD_1:
|
---|
| 165 | case TRUE_KEYWORD_2:
|
---|
| 166 | return true;
|
---|
| 167 | default:
|
---|
| 168 | return value;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | function covertValuesToHashes(source) {
|
---|
| 173 | var clonedSource = override(source, {});
|
---|
| 174 | var level;
|
---|
| 175 | var i;
|
---|
| 176 |
|
---|
| 177 | for (i = 0; i <= 2; i++) {
|
---|
| 178 | level = '' + i;
|
---|
| 179 |
|
---|
| 180 | if (level in clonedSource && (clonedSource[level] === undefined || clonedSource[level] === false)) {
|
---|
| 181 | delete clonedSource[level];
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | if (level in clonedSource && clonedSource[level] === true) {
|
---|
| 185 | clonedSource[level] = {};
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if (level in clonedSource && typeof clonedSource[level] == 'string') {
|
---|
| 189 | clonedSource[level] = covertToHash(clonedSource[level], level);
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | return clonedSource;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | function covertToHash(asString, level) {
|
---|
| 197 | return asString
|
---|
| 198 | .split(OPTION_SEPARATOR)
|
---|
| 199 | .reduce(function(accumulator, directive) {
|
---|
| 200 | var parts = directive.split(OPTION_VALUE_SEPARATOR);
|
---|
| 201 | var name = parts[0];
|
---|
| 202 | var value = parts[1];
|
---|
| 203 | var normalizedValue = normalizeValue(value);
|
---|
| 204 |
|
---|
| 205 | if (ALL_KEYWORD_1 == name || ALL_KEYWORD_2 == name) {
|
---|
| 206 | accumulator = override(accumulator, defaults(level, normalizedValue));
|
---|
| 207 | } else {
|
---|
| 208 | accumulator[name] = normalizedValue;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | return accumulator;
|
---|
| 212 | }, {});
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | module.exports = {
|
---|
| 216 | OptimizationLevel: OptimizationLevel,
|
---|
| 217 | optimizationLevelFrom: optimizationLevelFrom
|
---|
| 218 | };
|
---|