source: imaps-frontend/node_modules/clean-css/lib/options/optimization-level.js@ 79a0317

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 5.4 KB
Line 
1var roundingPrecisionFrom = require('./rounding-precision').roundingPrecisionFrom;
2
3var override = require('../utils/override');
4
5var OptimizationLevel = {
6 Zero: '0',
7 One: '1',
8 Two: '2'
9};
10
11var DEFAULTS = {};
12
13DEFAULTS[OptimizationLevel.Zero] = {};
14DEFAULTS[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};
37DEFAULTS[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
54var ALL_KEYWORD_1 = '*';
55var ALL_KEYWORD_2 = 'all';
56var FALSE_KEYWORD_1 = 'false';
57var FALSE_KEYWORD_2 = 'off';
58var TRUE_KEYWORD_1 = 'true';
59var TRUE_KEYWORD_2 = 'on';
60
61var LIST_VALUE_SEPARATOR = ',';
62var OPTION_SEPARATOR = ';';
63var OPTION_VALUE_SEPARATOR = ':';
64
65function 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
146function 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
159function 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
172function 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
196function 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
215module.exports = {
216 OptimizationLevel: OptimizationLevel,
217 optimizationLevelFrom: optimizationLevelFrom
218};
Note: See TracBrowser for help on using the repository browser.