This repository has been archived by the owner on Jun 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathuglifycss-lib.js
462 lines (383 loc) · 14.4 KB
/
uglifycss-lib.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**
* UglifyCSS
* Port of YUI CSS Compressor to NodeJS
* Author: Franck Marcia - https://github.com/fmarcia
* MIT licenced
*/
/**
* cssmin.js
* Author: Stoyan Stefanov - http://phpied.com/
* This is a JavaScript port of the CSS minification tool
* distributed with YUICompressor, itself a port
* of the cssmin utility by Isaac Schlueter - http://foohack.com/
* Permission is hereby granted to use the JavaScript version under the same
* conditions as the YUICompressor (original YUICompressor note below).
*/
/**
* YUI Compressor
* http://developer.yahoo.com/yui/compressor/
* Author: Julien Lecomte - http://www.julienlecomte.net/
* Copyright (c) 2011 Yahoo! Inc. All rights reserved.
* The copyrights embodied in the content of this file are licensed
* by Yahoo! Inc. under the BSD (revised) open source license.
*/
var util = require('util'),
fs = require('fs'),
uglifycss = module.exports = {
defaultOptions: {
maxLineLen: 0,
expandVars: false,
uglyComments: false,
cuteComments: false,
},
/**
* Utility method to replace all data urls with tokens before we start
* compressing, to avoid performance issues running some of the subsequent
* regexes against large strings chunks.
*
* @private
* @method _extractDataUrls
* @param {String} css The input css
* @param {Array} The global array of tokens to preserve
* @returns String The processed css
*/
extractDataUrls: function (css, preservedTokens) {
// Leave data urls alone to increase parse performance.
var maxIndex = css.length - 1,
appendIndex = 0,
startIndex,
endIndex,
terminator,
foundTerminator,
sb = [],
m,
preserver,
token,
pattern = /url\(\s*(["']?)data\:/g;
// Since we need to account for non-base64 data urls, we need to handle
// ' and ) being part of the data string. Hence switching to indexOf,
// to determine whether or not we have matching string terminators and
// handling sb appends directly, instead of using matcher.append* methods.
while ((m = pattern.exec(css)) !== null) {
startIndex = m.index + 4; // "url(".length()
terminator = m[1]; // ', " or empty (not quoted)
if (terminator.length === 0) {
terminator = ")";
}
foundTerminator = false;
endIndex = pattern.lastIndex - 1;
while(foundTerminator === false && endIndex+1 <= maxIndex) {
endIndex = css.indexOf(terminator, endIndex + 1);
// endIndex == 0 doesn't really apply here
if ((endIndex > 0) && (css.charAt(endIndex - 1) !== '\\')) {
foundTerminator = true;
if (")" != terminator) {
endIndex = css.indexOf(")", endIndex);
}
}
}
// Enough searching, start moving stuff over to the buffer
sb.push(css.substring(appendIndex, m.index));
if (foundTerminator) {
token = css.substring(startIndex, endIndex);
token = token.replace(/\s+/g, "");
preservedTokens.push(token);
preserver = "url(___PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___)";
sb.push(preserver);
appendIndex = endIndex + 1;
} else {
// No end terminator found, re-add the whole match. Should we throw/warn here?
sb.push(css.substring(m.index, pattern.lastIndex));
appendIndex = pattern.lastIndex;
}
}
sb.push(css.substring(appendIndex));
return sb.join("");
},
/**
* Utility method to compress hex color values of the form #AABBCC to #ABC.
*
* DOES NOT compress CSS ID selectors which match the above pattern (which would break things).
* e.g. #AddressForm { ... }
*
* DOES NOT compress IE filters, which have hex color values (which would break things).
* e.g. filter: chroma(color="#FFFFFF");
*
* DOES NOT compress invalid hex values.
* e.g. background-color: #aabbccdd
*
* @private
* @method _compressHexColors
* @param {String} css The input css
* @returns String The processed css
*/
compressHexColors: function(css) {
// Look for hex colors inside { ... } (to avoid IDs) and which don't have a =, or a " in front of them (to avoid filters)
var pattern = /(\=\s*?["']?)?#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])(\}|[^0-9a-f{][^{]*?\})/gi,
m,
index = 0,
isFilter,
sb = [];
while ((m = pattern.exec(css)) !== null) {
sb.push(css.substring(index, m.index));
isFilter = m[1];
if (isFilter) {
// Restore, maintain case, otherwise filter will break
sb.push(m[1] + "#" + (m[2] + m[3] + m[4] + m[5] + m[6] + m[7]));
} else {
if (m[2].toLowerCase() == m[3].toLowerCase() &&
m[4].toLowerCase() == m[5].toLowerCase() &&
m[6].toLowerCase() == m[7].toLowerCase()) {
// Compress.
sb.push("#" + (m[3] + m[5] + m[7]).toLowerCase());
} else {
// Non compressible color, restore but lower case.
sb.push("#" + (m[2] + m[3] + m[4] + m[5] + m[6] + m[7]).toLowerCase());
}
}
index = pattern.lastIndex = pattern.lastIndex - m[8].length;
}
sb.push(css.substring(index));
return sb.join("");
},
// Uglify a CSS string
processString: function(content, options) {
var startIndex,
endIndex,
comments = [],
preservedTokens = [],
token,
len = content.length,
pattern,
quote,
rgbcolors,
hexcolor,
placeholder,
val,
i,
c,
line = [],
lines = [],
vars = {};
options = options || uglifycss.defaultOptions;
content = uglifycss.extractDataUrls(content, preservedTokens);
// collect all comment blocks...
while ((startIndex = content.indexOf("/*", startIndex)) >= 0) {
endIndex = content.indexOf("*/", startIndex + 2);
if (endIndex < 0) {
endIndex = len;
}
token = content.slice(startIndex + 2, endIndex);
comments.push(token);
content = content.slice(0, startIndex + 2)
+ "___PRESERVE_CANDIDATE_COMMENT_" + (comments.length - 1) + "___"
+ content.slice(endIndex);
startIndex += 2;
}
// preserve strings so their content doesn't get accidentally minified
pattern = /("([^\\"]|\\.|\\)*")|('([^\\']|\\.|\\)*')/g;
content = content.replace(pattern, function (token) {
quote = token.substring(0, 1);
token = token.slice(1, -1);
// maybe the string contains a comment-like substring or more? put'em back then
if (token.indexOf("___PRESERVE_CANDIDATE_COMMENT_") >= 0) {
for (i = 0, len = comments.length; i < len; i += 1) {
token = token.replace("___PRESERVE_CANDIDATE_COMMENT_" + i + "___", comments[i]);
}
}
// minify alpha opacity in filter strings
token = token.replace(/progid:DXImageTransform.Microsoft.Alpha\(Opacity=/gi, "alpha(opacity=");
preservedTokens.push(token);
return quote + "___PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___" + quote;
});
// strings are safe, now wrestle the comments
for (i = 0, len = comments.length; i < len; i += 1) {
token = comments[i];
placeholder = "___PRESERVE_CANDIDATE_COMMENT_" + i + "___";
// ! in the first position of the comment means preserve
// so push to the preserved tokens keeping the !
if (token.charAt(0) === "!") {
if (options.cuteComments) {
preservedTokens.push(token.substring(1));
} else if (options.uglyComments) {
preservedTokens.push(token.substring(1).replace(/[\r\n]/g, ''));
} else {
preservedTokens.push(token);
}
content = content.replace(placeholder, "___PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___");
continue;
}
// \ in the last position looks like hack for Mac/IE5
// shorten that to /*\*/ and the next one to /**/
if (token.charAt(token.length - 1) === "\\") {
preservedTokens.push("\\");
content = content.replace(placeholder, "___PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___");
i = i + 1; // attn: advancing the loop
preservedTokens.push("");
content = content.replace(
"___PRESERVE_CANDIDATE_COMMENT_" + i + "___",
"___PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___"
);
continue;
}
// keep empty comments after child selectors (IE7 hack)
// e.g. html >/**/ body
if (token.length === 0) {
startIndex = content.indexOf(placeholder);
if (startIndex > 2) {
if (content.charAt(startIndex - 3) === '>') {
preservedTokens.push("");
content = content.replace(placeholder, "___PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___");
}
}
}
// in all other cases kill the comment
content = content.replace("/*" + placeholder + "*/", "");
}
if (options.expandVars) {
// parse simple @variables blocks and remove them
pattern = /@variables\s*\{\s*([^\}]+)\s*\}/g;
content = content.replace(pattern, function (token, f1) {
pattern = /\s*([a-z0-9\-]+)\s*:\s*([^;\}]+)\s*/gi;
f1.replace(pattern, function(token, f1, f2) {
if (f1 && f2) {
vars[f1] = f2;
}
return '';
});
return '';
});
// replace var(x) with the value of x
pattern = /var\s*\(\s*([^\)]+)\s*\)/g;
content = content.replace(pattern, function (token, f1) {
return vars[f1] || 'none';
});
}
// normalize all whitespace strings to single spaces. Easier to work with that way.
content = content.replace(/\s+/g, " ");
// remove the spaces before the things that should not have spaces before them.
// but, be careful not to turn "p :link {...}" into "p:link{...}"
// swap out any pseudo-class colons with the token, and then swap back.
pattern = /(^|\})(([^\{:])+:)+([^\{]*\{)/g;
content = content.replace(pattern, function (token) {
return token.replace(/:/g, "___PSEUDOCLASSCOLON___");
});
// remove spaces before the things that should not have spaces before them.
content = content.replace(/\s+([!{};:>+\(\)\],])/g, "$1");
// bring back the colon
content = content.replace(/___PSEUDOCLASSCOLON___/g, ":");
// retain space for special IE6 cases
content = content.replace(/:first-(line|letter)(\{|,)/g, ":first-$1 $2");
// newlines before and after the end of a preserved comment
if (options.cuteComments) {
content = content.replace(/\s*\/\*/g, "___PRESERVED_NEWLINE___/*");
content = content.replace(/\*\/\s*/g, "*/___PRESERVED_NEWLINE___");
// no space after the end of a preserved comment
} else {
content = content.replace(/\*\/\s*/g, '*/');
}
// if there is a @charset, then only allow one, and push to the top of the file.
content = content.replace(/^(.*)(@charset \"[^\"]*\";)/g, "$2$1");
content = content.replace(/^(\s*@charset [^;]+;\s*)+/g, "$1");
// put the space back in some cases, to support stuff like
// @media screen and (-webkit-min-device-pixel-ratio:0){
content = content.replace(/\band\(/g, "and (");
// remove the spaces after the things that should not have spaces after them.
content = content.replace(/([!{}:;>+\(\[,])\s+/g, "$1");
// remove unnecessary semicolons
content = content.replace(/;+\}/g, "}");
// replace 0(px,em,%) with 0.
content = content.replace(/([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)/g, "$1$2");
// replace 0 0 0 0; with 0.
content = content.replace(/:0 0 0 0(;|\})/g, ":0$1");
content = content.replace(/:0 0 0(;|\})/g, ":0$1");
content = content.replace(/:0 0(;|\})/g, ":0$1");
// replace background-position:0; with background-position:0 0;
// same for transform-origin
pattern = /(background-position|transform-origin|webkit-transform-origin|moz-transform-origin|o-transform-origin|ms-transform-origin):0(;|\})/gi;
content = content.replace(pattern, function (token, f1, f2) {
return f1.toLowerCase() + ":0 0" + f2;
});
// replace 0.6 to .6, but only when preceded by : or a white-space
content = content.replace(/(:|\s)0+\.(\d+)/g, "$1.$2");
// shorten colors from rgb(51,102,153) to #336699
// this makes it more likely that it'll get further compressed in the next step.
pattern = /rgb\s*\(\s*([0-9,\s]+)\s*\)/gi;
content = content.replace(pattern, function (token, f1) {
rgbcolors = f1.split(",");
hexcolor = "#";
for (i = 0; i < rgbcolors.length; i += 1) {
val = parseInt(rgbcolors[i], 10);
if (val < 16) {
hexcolor += "0";
}
hexcolor += val.toString(16);
}
return hexcolor;
});
// Shorten colors from #AABBCC to #ABC.
content = uglifycss.compressHexColors(content);
// border: none -> border:0
pattern = /(border|border-top|border-right|border-bottom|border-right|outline|background):none(;|\})/gi;
content = content.replace(pattern, function (token, f1, f2) {
return f1.toLowerCase() + ":0" + f2;
});
// shorter opacity IE filter
content = content.replace(/progid:DXImageTransform\.Microsoft\.Alpha\(Opacity=/gi, "alpha(opacity=");
// remove empty rules.
content = content.replace(/[^\};\{\/]+\{\}/g, "");
// some source control tools don't like it when files containing lines longer
// than, say 8000 characters, are checked in. The linebreak option is used in
// that case to split long lines after a specific column.
if (options.maxLineLen > 0) {
for (i = 0, len = content.length; i < len; i += 1) {
c = content.charAt(i);
line.push(c);
if (c === '}' && line.length > options.maxLineLen) {
lines.push(line.join(''));
line = [];
}
}
if (line.length) {
lines.push(line.join(''));
}
content = lines.join('\n');
}
// replace multiple semi-colons in a row by a single one
// see SF bug #1980989
content = content.replace(/;;+/g, ";");
// trim the final string (for any leading or trailing white spaces)
content = content.replace(/(^\s*|\s*$)/g, "");
// restore preserved comments and strings
content = content.replace(/___PRESERVED_TOKEN_(\d+)___/g, function (token, f1) {
return preservedTokens[parseInt(f1, 10)];
});
// restore preserved newlines
content = content.replace(/___PRESERVED_NEWLINE___/g, '\n');
// return
return content;
},
// Uglify CSS files
processFiles: function(filenames, options) {
var nFiles = filenames.length,
uglies = [],
index,
filename,
content;
// process files
for (index = 0; index < nFiles; index += 1) {
filename = filenames[index];
try {
content = fs.readFileSync(filename, 'utf8');
if (content.length) {
uglies.push(uglifycss.processString(content, options));
}
} catch (e) {
util.error('unable to process "' + filename + '" with ' + e);
process.exit(1);
}
}
// return concat'd results
return uglies.join('');
}
};