Skip to content

Commit

Permalink
Implementation of YUI rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan BLEUZEN committed Sep 30, 2010
1 parent 8a79f83 commit 2f80400
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
This is a node module that minimize CSS files (cssmin).

I used most rules for minimizing from Isaac Z. Schlueter cssmin git
I used most rules for minimizing from Isaac Schlueter cssmin git
http://github.com/isaacs/cssmin

And I also used YUICompressor source code for CSS minification.

------------------------------------------------------------------------------

Expand Down
76 changes: 63 additions & 13 deletions cssmin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/*
node-cssmin - a css minifier
node-cssmin - a simple css minifier
*/


Expand All @@ -14,22 +11,75 @@ function cssmin(input, linebreak) {
linebreak = 0;
}

// normalize whitespace
/*
* Normalize whitespace by removing \t, \n, ...
*/
output = input.replace(/\s+/g, " ");

// Remove comments
output = output.replace(/\/\*(.*?)\*\//g, "");

// Remove extra whitespace
/**
* Remove comments from source
* Comments starting with ! are preserved
*/
output = output.replace(/\/\*[^!](.*?)\*\//g, "");

/**
* Remove extra whitespace on blocks
*/
output = output.replace(/([!{}:;>+\(\[,])\s+/g, "$1");

/**
* Restore certain space for @webkit that would fail
*
*/
output = output.replace(/(@media[^{]*[^\s])\(/, "$1 (");

/**
* Remove unnecessary 0px, 0em,... Turning them into 0
*/
output = output.replace(/([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)/g, "$1$2");

/**
* Combine multiple 0 into one
*/
output = output.replace(/:0 0 0 0(;|})/, ":0$1");
output = output.replace(/:0 0 0(;|})/, ":0$1");
output = output.replace(/:0 0(;|})/, ":0$1");
/* Restore background-position:0; with background-position:0 0; */
output =output.replace(/background-position:0(;|})/, "background-position:0 0$1");

/**
* Remove multiple semi-colon in a row
*/
output = output.replace(/;;+/g,";")

/**
* Remove the final semi-colon of block
*/
output = output.replace(/;(})/g, "}");

/**
* Remove empty rules
*/
output = output.replace(/[^}{;]+{}/, "");

/**
* Removing first white if exist
*/
output = output.replace(/^ /, "");

/**
* Removing last white space if exist
*/
output = output.replace(/ /, "");

/**
* Linebreak is an option that generate one block per line.
* Could be useful for debug.
*/
if(linebreak == 1){
// Option for debug, that adds a linebreak after each rule
output = output.replace(/(})/g, "$1\n");
output = output.replace(/(\*\/|})( ?)/g, "$1\n");
}

// Remove unnecessary 0px, 0em,... Turning them into 0;
output = output.replace(/([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)/g, "$1$2");

return output;
}

0 comments on commit 2f80400

Please sign in to comment.