From 9ac9de00b1049f187351f7fa0f52e0fda7adb5a6 Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Sat, 20 Oct 2012 20:46:56 +0100 Subject: [PATCH] Reformatted lib/clean.js so it's eaasier to follow the rules. --- lib/clean.js | 120 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 86 insertions(+), 34 deletions(-) diff --git a/lib/clean.js b/lib/clean.js index 4819c3b5..d3b442ad 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -52,20 +52,39 @@ var CleanCSS = { data = CleanCSS._stripContent(context, data); }); - replace(/;\s*;+/g, ';') // whitespace between semicolons & multiple semicolons - replace(/\n/g, '') // line breaks - replace(/\s+/g, ' ') // multiple whitespace - replace(/ !important/g, '!important') // whitespace before !important - replace(/[ ]?,[ ]?/g, ',') // space with a comma - replace(/progid:[^(]+\(([^\)]+)/g, function(match, contents) { // restore spaces inside IE filters (IE 7 issue) + // whitespace between semicolons & multiple semicolons + replace(/;\s*;+/g, ';'); + + // line breaks + replace(/\n/g, ''); + + // multiple whitespace + replace(/\s+/g, ' '); + + // whitespace before !important + replace(/ !important/g, '!important'); + + // space with a comma + replace(/[ ]?,[ ]?/g, ','); + + // restore spaces inside IE filters (IE 7 issue) + replace(/progid:[^(]+\(([^\)]+)/g, function(match, contents) { return match.replace(/,/g, ', '); - }) - replace(/ ([+~>]) /g, '$1') // replace spaces around selectors - replace(/\{([^}]+)\}/g, function(match, contents) { // whitespace inside content + }); + + // replace spaces around selectors + replace(/ ([+~>]) /g, '$1'); + + // whitespace inside content + replace(/\{([^}]+)\}/g, function(match, contents) { return '{' + contents.trim().replace(/(\s*)([;:=\s])(\s*)/g, '$2') + '}'; - }) - replace(/;}/g, '}') // trailing semicolons - replace(/rgb\s*\(([^\)]+)\)/g, function(match, color) { // rgb to hex colors + }); + + // trailing semicolons + replace(/;}/g, '}'); + + // rgb to hex colors + replace(/rgb\s*\(([^\)]+)\)/g, function(match, color) { var parts = color.split(','); var encoded = '#'; for (var i = 0; i < 3; i++) { @@ -73,37 +92,69 @@ var CleanCSS = { encoded += asHex.length == 1 ? '0' + asHex : asHex; } return encoded; - }) - replace(/([^"'=\s])\s*#([0-9a-f]{6})/gi, function(match, prefix, color) { // long hex to short hex + }); + + // long hex to short hex + replace(/([^"'=\s])\s*#([0-9a-f]{6})/gi, function(match, prefix, color) { if (color[0] == color[1] && color[2] == color[3] && color[4] == color[5]) return (prefix + (/:$/.test(prefix) ? '' : ' ')) + '#' + color[0] + color[2] + color[4]; else return (prefix + (/:$/.test(prefix) ? '' : ' ')) + '#' + color; - }) - replace(/(color|background):(\w+)/g, function(match, property, colorName) { // replace standard colors with hex values (only if color name is longer then hex value) + }); + + // replace standard colors with hex values (only if color name is longer then hex value) + replace(/(color|background):(\w+)/g, function(match, property, colorName) { if (CleanCSS.colors[colorName]) return property + ':' + CleanCSS.colors[colorName]; else return match; - }) - replace(/([: ,\(])#f00/g, '$1red') // replace #f00 with red as it's shorter - replace(/font\-weight:(\w+)/g, function(match, weight) { // replace font weight with numerical value + }); + + // replace #f00 with red as it's shorter + replace(/([: ,\(])#f00/g, '$1red'); + + // replace font weight with numerical value + replace(/font\-weight:(\w+)/g, function(match, weight) { if (weight == 'normal') return 'font-weight:400'; else if (weight == 'bold') return 'font-weight:700'; else return match; - }) - replace(/progid:DXImageTransform\.Microsoft\.(Alpha|Chroma)(\([^\)]+\))([;}'"])/g, function(match, filter, args, suffix) { // IE shorter filters but only if single (IE 7 issue) + }); + + // 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; - }) - replace(/(\s|:)0(px|em|ex|cm|mm|in|pt|pc|%)/g, '$1' + '0') // zero + unit to zero - replace(/(border|border-top|border-right|border-bottom|border-left|outline):none/g, '$1:0') // none to 0 - replace(/(background):none([;}])/g, '$1:0$2') // background:none to 0 - replace(/0 0 0 0([^\.])/g, '0$1') // multiple zeros into one - replace(/([: ,=\-])0\.(\d)/g, '$1.$2') - if (options.removeEmpty) replace(/[^}]+?{\s*?}/g, '') // empty elements - if (data.indexOf('charset') > 0) replace(/(.+)(@charset [^;]+;)/, '$2$1') // move first charset to the beginning - replace(/(.)(@charset [^;]+;)/g, '$1') // remove all extra charsets that are not at the beginning - replace(/\*([\.#:\[])/g, '$1') // remove universal selector when not needed (*#id, *.class etc) - replace(/ {/g, '{') // whitespace before definition - replace(/\} /g, '}') // whitespace after definition + }); + + // zero + unit to zero + replace(/(\s|:)0(px|em|ex|cm|mm|in|pt|pc|%)/g, '$1' + '0'); + + // 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(/0 0 0 0([^\.])/g, '0$1'); + replace(/([: ,=\-])0\.(\d)/g, '$1.$2'); + + // empty elements + if (options.removeEmpty) + replace(/[^}]+?{\s*?}/g, ''); + + // move first charset to the beginning + if (data.indexOf('charset') > 0) + replace(/(.+)(@charset [^;]+;)/, '$2$1'); + + // remove all extra charsets that are not at the beginning + replace(/(.)(@charset [^;]+;)/g, '$1'); + + // remove universal selector when not needed (*#id, *.class etc) + replace(/\*([\.#:\[])/g, '$1'); + + // whitespace before definition + replace(/ {/g, '{'); + + // whitespace after definition + replace(/\} /g, '}'); // Get the special comments, content content, and spaces inside calc back var specialCommentsCount = context.specialComments.length; @@ -127,7 +178,8 @@ var CleanCSS = { return context.contentBlocks.shift(); }); - return data.trim() // trim spaces at beginning and end + // trim spaces at beginning and end + return data.trim(); }, // Strips special comments (/*! ... */) by replacing them by __CSSCOMMENT__ marker -- 2.34.1