source: imaps-frontend/node_modules/clean-css/lib/optimizer/wrap-for-optimizing.js

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.9 KB
Line 
1var Hack = require('./hack');
2
3var Marker = require('../tokenizer/marker');
4var Token = require('../tokenizer/token');
5
6var Match = {
7 ASTERISK: '*',
8 BACKSLASH: '\\',
9 BANG: '!',
10 BANG_SUFFIX_PATTERN: /!\w+$/,
11 IMPORTANT_TOKEN: '!important',
12 IMPORTANT_TOKEN_PATTERN: new RegExp('!important$', 'i'),
13 IMPORTANT_WORD: 'important',
14 IMPORTANT_WORD_PATTERN: new RegExp('important$', 'i'),
15 SUFFIX_BANG_PATTERN: /!$/,
16 UNDERSCORE: '_',
17 VARIABLE_REFERENCE_PATTERN: /var\(--.+\)$/
18};
19
20function wrapAll(properties, skipProperties) {
21 var wrapped = [];
22 var single;
23 var property;
24 var i;
25
26 for (i = properties.length - 1; i >= 0; i--) {
27 property = properties[i];
28
29 if (property[0] != Token.PROPERTY) {
30 continue;
31 }
32
33 if (skipProperties && skipProperties.indexOf(property[1][1]) > -1) {
34 continue;
35 }
36
37 single = wrapSingle(property);
38 single.all = properties;
39 single.position = i;
40 wrapped.unshift(single);
41 }
42
43 return wrapped;
44}
45
46function someVariableReferences(property) {
47 var i, l;
48 var value;
49
50 // skipping `property` and property name tokens
51 for (i = 2, l = property.length; i < l; i++) {
52 value = property[i];
53
54 if (value[0] != Token.PROPERTY_VALUE) {
55 continue;
56 }
57
58 if (isVariableReference(value[1])) {
59 return true;
60 }
61 }
62
63 return false;
64}
65
66function isVariableReference(value) {
67 return Match.VARIABLE_REFERENCE_PATTERN.test(value);
68}
69
70function isMultiplex(property) {
71 var value;
72 var i, l;
73
74 for (i = 3, l = property.length; i < l; i++) {
75 value = property[i];
76
77 if (value[0] == Token.PROPERTY_VALUE && (value[1] == Marker.COMMA || value[1] == Marker.FORWARD_SLASH)) {
78 return true;
79 }
80 }
81
82 return false;
83}
84
85function hackFrom(property) {
86 var match = false;
87 var name = property[1][1];
88 var lastValue = property[property.length - 1];
89
90 if (name[0] == Match.UNDERSCORE) {
91 match = [Hack.UNDERSCORE];
92 } else if (name[0] == Match.ASTERISK) {
93 match = [Hack.ASTERISK];
94 } else if (lastValue[1][0] == Match.BANG && !lastValue[1].match(Match.IMPORTANT_WORD_PATTERN)) {
95 match = [Hack.BANG];
96 } else if (lastValue[1].indexOf(Match.BANG) > 0
97 && !lastValue[1].match(Match.IMPORTANT_WORD_PATTERN)
98 && Match.BANG_SUFFIX_PATTERN.test(lastValue[1])) {
99 match = [Hack.BANG];
100 } else if (lastValue[1].indexOf(Match.BACKSLASH) > 0
101 && lastValue[1].indexOf(Match.BACKSLASH) == lastValue[1].length - Match.BACKSLASH.length - 1) {
102 match = [Hack.BACKSLASH, lastValue[1].substring(lastValue[1].indexOf(Match.BACKSLASH) + 1)];
103 } else if (lastValue[1].indexOf(Match.BACKSLASH) === 0 && lastValue[1].length == 2) {
104 match = [Hack.BACKSLASH, lastValue[1].substring(1)];
105 }
106
107 return match;
108}
109
110function isImportant(property) {
111 if (property.length < 3) { return false; }
112
113 var lastValue = property[property.length - 1];
114 if (Match.IMPORTANT_TOKEN_PATTERN.test(lastValue[1])) {
115 return true;
116 } if (Match.IMPORTANT_WORD_PATTERN.test(lastValue[1])
117 && Match.SUFFIX_BANG_PATTERN.test(property[property.length - 2][1])) {
118 return true;
119 }
120
121 return false;
122}
123
124function stripImportant(property) {
125 var lastValue = property[property.length - 1];
126 var oneButLastValue = property[property.length - 2];
127
128 if (Match.IMPORTANT_TOKEN_PATTERN.test(lastValue[1])) {
129 lastValue[1] = lastValue[1].replace(Match.IMPORTANT_TOKEN_PATTERN, '');
130 } else {
131 lastValue[1] = lastValue[1].replace(Match.IMPORTANT_WORD_PATTERN, '');
132 oneButLastValue[1] = oneButLastValue[1].replace(Match.SUFFIX_BANG_PATTERN, '');
133 }
134
135 if (lastValue[1].length === 0) {
136 property.pop();
137 }
138
139 if (oneButLastValue[1].length === 0) {
140 property.pop();
141 }
142}
143
144function stripPrefixHack(property) {
145 property[1][1] = property[1][1].substring(1);
146}
147
148function stripSuffixHack(property, hackFrom) {
149 var lastValue = property[property.length - 1];
150 lastValue[1] = lastValue[1]
151 .substring(0, lastValue[1].indexOf(hackFrom[0] == Hack.BACKSLASH ? Match.BACKSLASH : Match.BANG))
152 .trim();
153
154 if (lastValue[1].length === 0) {
155 property.pop();
156 }
157}
158
159function wrapSingle(property) {
160 var importantProperty = isImportant(property);
161 if (importantProperty) {
162 stripImportant(property);
163 }
164
165 var whichHack = hackFrom(property);
166 if (whichHack[0] == Hack.ASTERISK || whichHack[0] == Hack.UNDERSCORE) {
167 stripPrefixHack(property);
168 } else if (whichHack[0] == Hack.BACKSLASH || whichHack[0] == Hack.BANG) {
169 stripSuffixHack(property, whichHack);
170 }
171
172 return {
173 block: property[2] && property[2][0] == Token.PROPERTY_BLOCK,
174 components: [],
175 dirty: false,
176 dynamic: someVariableReferences(property),
177 hack: whichHack,
178 important: importantProperty,
179 name: property[1][1],
180 multiplex: property.length > 3 ? isMultiplex(property) : false,
181 optimizable: true,
182 position: 0,
183 shorthand: false,
184 unused: false,
185 value: property.slice(2)
186 };
187}
188
189module.exports = {
190 all: wrapAll,
191 single: wrapSingle
192};
Note: See TracBrowser for help on using the repository browser.