1 | var OptimizationLevel = require('../../../options/optimization-level').OptimizationLevel;
|
---|
2 |
|
---|
3 | var Marker = require('../../../tokenizer/marker');
|
---|
4 |
|
---|
5 | var CALC_DIVISION_WHITESPACE_PATTERN = /\) ?\/ ?/g;
|
---|
6 | var COMMA_AND_SPACE_PATTERN = /, /g;
|
---|
7 | var LINE_BREAK_PATTERN = /\r?\n/g;
|
---|
8 | var MULTI_WHITESPACE_PATTERN = /\s+/g;
|
---|
9 | var FUNCTION_CLOSING_BRACE_WHITESPACE_PATTERN = /\s+(;?\))/g;
|
---|
10 | var FUNCTION_OPENING_BRACE_WHITESPACE_PATTERN = /(\(;?)\s+/g;
|
---|
11 | var VARIABLE_NAME_PATTERN = /^--\S+$/;
|
---|
12 | var VARIABLE_VALUE_PATTERN = /^var\(\s*--\S+\s*\)$/;
|
---|
13 |
|
---|
14 | var plugin = {
|
---|
15 | level1: {
|
---|
16 | value: function whitespace(name, value, options) {
|
---|
17 | if (!options.level[OptimizationLevel.One].removeWhitespace) {
|
---|
18 | return value;
|
---|
19 | }
|
---|
20 |
|
---|
21 | if (VARIABLE_NAME_PATTERN.test(name) && !VARIABLE_VALUE_PATTERN.test(value)) {
|
---|
22 | return value;
|
---|
23 | }
|
---|
24 |
|
---|
25 | if ((value.indexOf(' ') == -1 && value.indexOf('\n') == -1) || value.indexOf('expression') === 0) {
|
---|
26 | return value;
|
---|
27 | }
|
---|
28 |
|
---|
29 | if (value.indexOf(Marker.SINGLE_QUOTE) > -1 || value.indexOf(Marker.DOUBLE_QUOTE) > -1) {
|
---|
30 | return value;
|
---|
31 | }
|
---|
32 |
|
---|
33 | value = value.replace(LINE_BREAK_PATTERN, '');
|
---|
34 | value = value.replace(MULTI_WHITESPACE_PATTERN, ' ');
|
---|
35 |
|
---|
36 | if (value.indexOf('calc') > -1) {
|
---|
37 | value = value.replace(CALC_DIVISION_WHITESPACE_PATTERN, ')/ ');
|
---|
38 | }
|
---|
39 |
|
---|
40 | return value
|
---|
41 | .replace(FUNCTION_OPENING_BRACE_WHITESPACE_PATTERN, '$1')
|
---|
42 | .replace(FUNCTION_CLOSING_BRACE_WHITESPACE_PATTERN, '$1')
|
---|
43 | .replace(COMMA_AND_SPACE_PATTERN, ',');
|
---|
44 | }
|
---|
45 | }
|
---|
46 | };
|
---|
47 |
|
---|
48 | module.exports = plugin;
|
---|