Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implementation of YUI rules

  • Loading branch information...
commit 2f80400b765eff3e380b00ccec3ece55120bc7fb 1 parent 8a79f83
Johan BLEUZEN authored
Showing with 66 additions and 14 deletions.
  1. +3 −1 README
  2. +63 −13 cssmin.js
View
4 README
@@ -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.
------------------------------------------------------------------------------
View
76 cssmin.js
@@ -1,8 +1,5 @@
/*
- node-cssmin - a css minifier
-
-
-
+ node-cssmin - a simple css minifier
*/
@@ -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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.