/usr/lib/nodejs/clean-css/clean.js is in node-clean-css 1.0.12-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 | /**
* Clean-css - https://github.com/GoalSmashers/clean-css
* Released under the terms of MIT license
*
* Copyright (C) 2011-2013 GoalSmashers.com
*/
var fs = require('fs');
var path = require('path');
var existsSync = fs.existsSync || path.existsSync;
var CleanCSS = {
colors: {
toHex: {
aqua: '#0ff',
black: '#000',
blue: '#00f',
fuchsia: '#f0f',
white: '#fff',
yellow: '#ff0'
},
toName: {
'#000080': 'navy',
'#008000': 'green',
'#008080': 'teal',
'#800000': 'maroon',
'#800080': 'purple',
'#808000': 'olive',
'#808080': 'gray',
'#c0c0c0': 'silver',
'#f00': 'red'
}
},
process: function(data, options) {
var context = {
specialComments: [],
freeTextBlocks: [],
urlBlocks: []
};
var replace = function() {
if (typeof arguments[0] == 'function')
arguments[0]();
else
data = data.replace.apply(data, arguments);
};
var lineBreak = process.platform == 'win32' ? '\r\n' : '\n';
this.lineBreak = lineBreak;
options = options || {};
// * - leave all important comments
// 1 - leave first important comment only
// 0 - strip all important comments
options.keepSpecialComments = 'keepSpecialComments' in options ?
options.keepSpecialComments :
'*';
options.keepBreaks = options.keepBreaks || false;
//active by default
if (options.processImport === undefined) {
options.processImport = true;
}
// replace function
if (options.debug) {
var originalReplace = replace;
replace = function(pattern, replacement) {
var name = typeof pattern == 'function' ?
/function (\w+)\(/.exec(pattern.toString())[1] :
pattern;
var start = process.hrtime();
originalReplace(pattern, replacement);
var itTook = process.hrtime(start);
console.log('%d ms: ' + name, 1000 * itTook[0] + itTook[1] / 1000000.0);
};
}
var removeComments = function() {
replace(function stripComments() {
data = CleanCSS._stripComments(context, data);
});
};
removeComments();
// replace all escaped line breaks
replace(/\\(\r\n|\n)/mg, '');
if (options.processImport) {
// inline all imports
replace(function inlineImports() {
data = CleanCSS._inlineImports(data, {
root: options.root || process.cwd(),
relativeTo: options.relativeTo
});
});
// strip comments with inlined imports
if (data.indexOf('/*') > -1) {
removeComments();
}
}
// strip parentheses in urls if possible (no spaces inside)
replace(/url\((['"])([^\)]+)['"]\)/g, function(match, quote, url) {
if (url.match(/[ \t]/g) !== null || url.indexOf('data:') === 0)
return 'url(' + quote + url + quote + ')';
else
return 'url(' + url + ')';
});
// strip parentheses in animation & font names
replace(/(animation|animation\-name|font|font\-family):([^;}]+)/g, function(match, propertyName, fontDef) {
return propertyName + ':' + fontDef.replace(/['"]([\w\-]+)['"]/g, '$1');
});
// strip parentheses in @keyframes
replace(/@(\-moz\-|\-o\-|\-webkit\-)?keyframes ([^{]+)/g, function(match, prefix, name) {
prefix = prefix || '';
return '@' + prefix + 'keyframes ' + (name.indexOf(' ') > -1 ? name : name.replace(/['"]/g, ''));
});
// IE shorter filters, but only if single (IE 7 issue)
replace(/progid:DXImageTransform\.Microsoft\.(Alpha|Chroma)(\([^\)]+\))([;}'"])/g, function(match, filter, args, suffix) {
return filter.toLowerCase() + args + suffix;
});
// strip parentheses in attribute values
replace(/\[([^\]]+)\]/g, function(match, content) {
var eqIndex = content.indexOf('=');
if (eqIndex < 0 && content.indexOf('\'') < 0 && content.indexOf('"') < 0)
return match;
var key = content.substring(0, eqIndex);
var value = content.substring(eqIndex + 1, content.length);
if (/^['"](?:[a-zA-Z][a-zA-Z\d\-_]+)['"]$/.test(value))
return '[' + key + '=' + value.substring(1, value.length - 1) + ']';
else
return match;
});
// replace all free text content with a placeholder
replace(function stripFreeText() {
data = CleanCSS._stripFreeText(context, data);
});
// replace url(...) with a placeholder
replace(function stripUrls() {
data = CleanCSS._stripUrls(context, data);
});
// line breaks
if (!options.keepBreaks)
replace(/[\r]?\n/g, ' ');
// multiple whitespace
replace(/[\t ]+/g, ' ');
// multiple semicolons (with optional whitespace)
replace(/;[ ]?;+/g, ';');
// multiple line breaks to one
replace(/ (?:\r\n|\n)/g, lineBreak);
replace(/(?:\r\n|\n)+/g, lineBreak);
// remove spaces around selectors
replace(/ ([+~>]) /g, '$1');
// remove extra spaces inside content
replace(/([!\(\{\}:;=,\n]) /g, '$1');
replace(/ ([!\)\{\};=,\n])/g, '$1');
replace(/(?:\r\n|\n)\}/g, '}');
replace(/([\{;,])(?:\r\n|\n)/g, '$1');
replace(/ :([^\{\};]+)([;}])/g, ':$1$2');
// restore spaces inside IE filters (IE 7 issue)
replace(/progid:[^(]+\(([^\)]+)/g, function(match) {
return match.replace(/,/g, ', ');
});
// trailing semicolons
replace(/;\}/g, '}');
// hsl to hex colors
replace(/hsl\((\d+),(\d+)%?,(\d+)%?\)/g, function(match, hue, saturation, lightness) {
var asRgb = CleanCSS._hslToRgb(hue, saturation, lightness);
var redAsHex = asRgb[0].toString(16);
var greenAsHex = asRgb[1].toString(16);
var blueAsHex = asRgb[2].toString(16);
return '#' +
((redAsHex.length == 1 ? '0' : '') + redAsHex) +
((greenAsHex.length == 1 ? '0' : '') + greenAsHex) +
((blueAsHex.length == 1 ? '0' : '') + blueAsHex);
});
// rgb to hex colors
replace(/rgb\((\d+),(\d+),(\d+)\)/g, function(match, red, green, blue) {
var redAsHex = parseInt(red, 10).toString(16);
var greenAsHex = parseInt(green, 10).toString(16);
var blueAsHex = parseInt(blue, 10).toString(16);
return '#' +
((redAsHex.length == 1 ? '0' : '') + redAsHex) +
((greenAsHex.length == 1 ? '0' : '') + greenAsHex) +
((blueAsHex.length == 1 ? '0' : '') + blueAsHex);
});
// long hex to short hex colors
replace(/([,: \(])#([0-9a-f]{6})/gi, function(match, prefix, color) {
if (color[0] == color[1] && color[2] == color[3] && color[4] == color[5])
return prefix + '#' + color[0] + color[2] + color[4];
else
return prefix + '#' + color;
});
// replace color name with hex values if shorter (or the other way around)
['toHex', 'toName'].forEach(function(type) {
var pattern = '(' + Object.keys(CleanCSS.colors[type]).join('|') + ')';
var colorSwitcher = function(match, prefix, colorValue, suffix) {
return prefix + CleanCSS.colors[type][colorValue.toLowerCase()] + suffix;
};
replace(new RegExp('([ :,\\(])' + pattern + '([;\\}!\\) ])', 'ig'), colorSwitcher);
replace(new RegExp('(,)' + pattern + '(,)', 'ig'), colorSwitcher);
});
// replace font weight with numerical value
replace(/(font|font\-weight):(normal|bold)([ ;\}!])/g, function(match, property, weight, suffix) {
if (weight == 'normal')
return property + ':400' + suffix;
else if (weight == 'bold')
return property + ':700' + suffix;
else
return match;
});
// zero + unit to zero
replace(/(\s|:|,)0(?:px|em|ex|cm|mm|in|pt|pc|%)/g, '$1' + '0');
replace(/rect\(0(?:px|em|ex|cm|mm|in|pt|pc|%)/g, 'rect(0');
// fraction zeros removal
replace(/\.([1-9]*)0+(\D)/g, function(match, nonZeroPart, suffix) {
return (nonZeroPart ? '.' : '') + nonZeroPart + suffix;
});
// restore 0% in hsl/hsla
replace(/(hsl|hsla)\(([^\)]+)\)/g, function(match, colorFunction, colorDef) {
var tokens = colorDef.split(',');
if (tokens[1] == '0')
tokens[1] = '0%';
if (tokens[2] == '0')
tokens[2] = '0%';
return colorFunction + '(' + tokens.join(',') + ')';
});
// none to 0
replace(/(border|border-top|border-right|border-bottom|border-left|outline):none/g, '$1:0');
// background:none to 0
replace(/(background):none([;}])/g, '$1:0$2');
// multiple zeros into one
replace(/box-shadow:0 0 0 0([^\.])/g, 'box-shadow:0 0$1');
replace(/:0 0 0 0([^\.])/g, ':0$1');
replace(/([: ,=\-])0\.(\d)/g, '$1.$2');
// shorthand notations
var shorthandRegex = function(repeats, hasSuffix) {
var pattern = '(padding|margin|border\\-width|border\\-color|border\\-style|border\\-radius):';
for (var i = 0; i < repeats; i++) {
pattern += '([\\d\\w\\.%#\\(\\),]+)' + (i < repeats - 1 ? ' ' : '');
}
return new RegExp(pattern + (hasSuffix ? '([;}])' : ''), 'g');
};
// 4 size values into less
replace(shorthandRegex(4), function(match, property, size1, size2, size3, size4) {
if (size1 === size2 && size1 === size3 && size1 === size4)
return property + ':' + size1;
else if (size1 === size3 && size2 === size4)
return property + ':' + size1 + ' ' + size2;
else if (size2 === size4)
return property + ':' + size1 + ' ' + size2 + ' ' + size3;
else
return match;
});
// 3 size values into less
replace(shorthandRegex(3, true), function(match, property, size1, size2, size3, suffix) {
if (size1 === size2 && size1 === size3)
return property + ':' + size1 + suffix;
else if (size1 === size3)
return property + ':' + size1 + ' ' + size2 + suffix;
else
return match;
});
// same 2 values into one
replace(shorthandRegex(2, true), function(match, property, size1, size2, suffix) {
if (size1 === size2)
return property + ':' + size1 + suffix;
else
return match;
});
// restore rect(...) zeros syntax for 4 zeros
replace(/rect\(\s?0(\s|,)0[ ,]0[ ,]0\s?\)/g, 'rect(0$10$10$10)');
// remove universal selector when not needed (*#id, *.class etc)
replace(/\*([\.#:\[])/g, '$1');
// Restore spaces inside calc back
replace(/calc\([^\}]+\}/g, function(match) {
return match.replace(/\+/g, ' + ');
});
// Restore urls, content content, and special comments (in that order)
replace(/__URL__/g, function() {
return context.urlBlocks.shift();
});
replace(/__CSSFREETEXT__/g, function() {
return context.freeTextBlocks.shift();
});
var specialCommentsCount = context.specialComments.length;
var breakSuffix = options.keepBreaks ? lineBreak : '';
replace(new RegExp('__CSSCOMMENT__(' + lineBreak + '| )?', 'g'), function() {
switch (options.keepSpecialComments) {
case '*':
return context.specialComments.shift() + breakSuffix;
case 1:
return context.specialComments.length == specialCommentsCount ?
context.specialComments.shift() + breakSuffix :
'';
case 0:
return '';
}
});
// move first charset to the beginning
replace(function moveCharset() {
// get first charset in stylesheet
var match = data.match(/@charset [^;]+;/);
var firstCharset = match ? match[0] : null;
if (!firstCharset)
return;
// reattach first charset and remove all subsequent
data = firstCharset +
(options.keepBreaks ? lineBreak : '') +
data.replace(new RegExp('@charset [^;]+;(' + lineBreak + ')?', 'g'), '');
});
if (options.removeEmpty) {
// empty elements
replace(/[^\{\}]+\{\}/g, '');
// empty @media declarations
replace(/@media [^\{]+\{\}/g, '');
}
// trim spaces at beginning and end
return data.trim();
},
// Inlines all imports taking care of repetitions, unknown files, and cilcular dependencies
_inlineImports: function(data, options) {
var tempData = [];
var nextStart = 0;
var nextEnd = 0;
var cursor = 0;
options.relativeTo = options.relativeTo || options.root;
options._baseRelativeTo = options._baseRelativeTo || options.relativeTo;
options.visited = options.visited || [];
var inlinedFile = function() {
var importedFile = data
.substring(data.indexOf(' ', nextStart) + 1, nextEnd)
.replace(/^url\(/, '')
.replace(/\)$/, '')
.replace(/['"]/g, '');
if (/^(http|https):\/\//.test(importedFile) || /^\/\//.test(importedFile))
return '@import url(' + importedFile + ');';
var relativeTo = importedFile[0] == '/' ?
options.root :
options.relativeTo;
var fullPath = path.resolve(path.join(relativeTo, importedFile));
if (existsSync(fullPath) && fs.statSync(fullPath).isFile() && options.visited.indexOf(fullPath) == -1) {
options.visited.push(fullPath);
var importedData = fs.readFileSync(fullPath, 'utf8');
var importRelativeTo = path.dirname(fullPath);
importedData = CleanCSS._rebaseRelativeURLs(importedData, importRelativeTo, options._baseRelativeTo);
return CleanCSS._inlineImports(importedData, {
root: options.root,
relativeTo: importRelativeTo,
_baseRelativeTo: options.baseRelativeTo,
visited: options.visited
});
} else {
return '';
}
};
for (; nextEnd < data.length; ) {
nextStart = data.indexOf('@import', cursor);
if (nextStart == -1)
break;
nextEnd = data.indexOf(';', nextStart);
if (nextEnd == -1)
break;
tempData.push(data.substring(cursor, nextStart));
tempData.push(inlinedFile());
cursor = nextEnd + 1;
}
return tempData.length > 0 ?
tempData.join('') + data.substring(cursor, data.length) :
data;
},
_rebaseRelativeURLs: function(data, fromBase, toBase) {
var tempData = [];
var nextStart = 0;
var nextEnd = 0;
var cursor = 0;
for (; nextEnd < data.length; ) {
nextStart = data.indexOf('url(', nextEnd);
if (nextStart == -1)
break;
nextEnd = data.indexOf(')', nextStart + 4);
if (nextEnd == -1)
break;
tempData.push(data.substring(cursor, nextStart));
var url = data.substring(nextStart + 4, nextEnd).replace(/['"]/g, '');
if (url[0] != '/' && url.indexOf('data:') !== 0 && url.substring(url.length - 4) != '.css') {
url = path.relative(toBase, path.join(fromBase, url)).replace(/\\/g, '/');
}
tempData.push('url(' + url + ')');
cursor = nextEnd + 1;
}
return tempData.length > 0 ?
tempData.join('') + data.substring(cursor, data.length) :
data;
},
// Strip special comments (/*! ... */) by replacing them by __CSSCOMMENT__ marker
// for further restoring. Plain comments are removed. It's done by scanning datq using
// String#indexOf scanning instead of regexps to speed up the process.
_stripComments: function(context, data) {
var tempData = [];
var nextStart = 0;
var nextEnd = 0;
var cursor = 0;
for (; nextEnd < data.length; ) {
nextStart = data.indexOf('/*', nextEnd);
nextEnd = data.indexOf('*/', nextStart + 2);
if (nextStart == -1 || nextEnd == -1)
break;
tempData.push(data.substring(cursor, nextStart));
if (data[nextStart + 2] == '!') {
// in case of special comments, replace them with a placeholder
context.specialComments.push(data.substring(nextStart, nextEnd + 2));
tempData.push('__CSSCOMMENT__');
}
cursor = nextEnd + 2;
}
return tempData.length > 0 ?
tempData.join('') + data.substring(cursor, data.length) :
data;
},
// Strip content tags by replacing them by the __CSSFREETEXT__
// marker for further restoring. It's done via string scanning
// instead of regexps to speed up the process.
_stripFreeText: function(context, data) {
var tempData = [];
var nextStart = 0;
var nextEnd = 0;
var cursor = 0;
var matchedParenthesis = null;
var singleParenthesis = "'";
var doubleParenthesis = '"';
var dataLength = data.length;
for (; nextEnd < data.length; ) {
var nextStartSingle = data.indexOf(singleParenthesis, nextEnd + 1);
var nextStartDouble = data.indexOf(doubleParenthesis, nextEnd + 1);
if (nextStartSingle == -1)
nextStartSingle = dataLength;
if (nextStartDouble == -1)
nextStartDouble = dataLength;
if (nextStartSingle < nextStartDouble) {
nextStart = nextStartSingle;
matchedParenthesis = singleParenthesis;
} else {
nextStart = nextStartDouble;
matchedParenthesis = doubleParenthesis;
}
if (nextStart == -1)
break;
nextEnd = data.indexOf(matchedParenthesis, nextStart + 1);
if (nextStart == -1 || nextEnd == -1)
break;
tempData.push(data.substring(cursor, nextStart));
tempData.push('__CSSFREETEXT__');
context.freeTextBlocks.push(data.substring(nextStart, nextEnd + 1));
cursor = nextEnd + 1;
}
return tempData.length > 0 ?
tempData.join('') + data.substring(cursor, data.length) :
data;
},
// Strip urls by replacing them by the __URL__
// marker for further restoring. It's done via string scanning
// instead of regexps to speed up the process.
_stripUrls: function(context, data) {
var nextStart = 0;
var nextEnd = 0;
var cursor = 0;
var tempData = [];
for (; nextEnd < data.length; ) {
nextStart = data.indexOf('url(', nextEnd);
if (nextStart == -1)
break;
nextEnd = data.indexOf(')', nextStart);
tempData.push(data.substring(cursor, nextStart));
tempData.push('__URL__');
context.urlBlocks.push(data.substring(nextStart, nextEnd + 1));
cursor = nextEnd + 1;
}
return tempData.length > 0 ?
tempData.join('') + data.substring(cursor, data.length) :
data;
},
// HSL to RGB converter. Both methods taken and adapted from:
// http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
_hslToRgb: function(h, s, l) {
var r, g, b;
h = ~~h / 360;
s = ~~s / 100;
l = ~~l / 100;
if (s === 0) {
r = g = b = l; // achromatic
} else {
var q = l < 0.5 ?
l * (1 + s) :
l + s - l * s;
var p = 2 * l - q;
r = this._hueToRgb(p, q, h + 1/3);
g = this._hueToRgb(p, q, h);
b = this._hueToRgb(p, q, h - 1/3);
}
return [~~(r * 255), ~~(g * 255), ~~(b * 255)];
},
_hueToRgb: function(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
};
module.exports = CleanCSS;
|