3 * Author: Julien Lecomte <jlecomte@yahoo-inc.com>
4 * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
5 * Code licensed under the BSD License:
6 * http://developer.yahoo.net/yui/license.txt
8 * This code is a port of Isaac Schlueter's cssmin utility.
10 <script type="text/javascript">
12 // usage. see - css=minify-bootstrap.js
18 pack = function (istr) {
21 function removeComments(str)
27 while ((si = str.indexOf("/*")) > -1) {
28 ei = str.indexOf("*/");
32 if (str.charAt(ei-1) == '\\') { // eg. /* \*/
35 // Looks like a comment to hide rules from IE Mac.
36 // Leave this comment, and the following one, alone...
39 r += str.substring(0, ei) ;
40 str = str.substring(ei);
46 r += str.substring(0, ei) ;
47 str = str.substring(ei+2);
51 r += str.substring(0, si) ;
52 str = str.substring(ei+2);
56 r += str.substring(0, 2);
57 str = str.substring(2);
65 istr = removeComments(istr);
66 //println("after comments remove: " + istr);
68 // Normalize all whitespace strings to single spaces. Easier to work with that way.
69 istr = istr.replace(/\s+/g, ' ');
71 // Make a pseudo class for the Box Model Hack ????
72 //css = css.replaceAll("\"\\\\\"}\\\\\"\"", "___PSEUDOCLASSBMH___");
73 istr = istr.replace(/"\\"\}\\"/g, "___PSEUDOCLASSBMH___");
75 // Remove the spaces before the things that should not have spaces before them.
76 // But, be careful not to turn "p :link {...}" into "p:link{...}"
77 // Swap out any pseudo-class colons with the token, and then swap back.
79 istr = istr.replace(/(^|\})(([^\{:])+:)+([^\{]*\{)/g, function(m) {
80 return m.replace(/:/g, "___PSEUDOCLASSCOLON___");
84 istr = istr.replace(/\s+([!{};:>+\(\)\],])/g, "$1");
85 istr = istr.replace(/___PSEUDOCLASSCOLON___/g, ":");
87 // Remove the spaces after the things that should not have spaces after them.
88 istr = istr.replace(/([!{}:;>+\(\[,])\s+/g, "$1");
90 // Add the semicolon where it's missing.
91 istr = istr.replace(/([^;\}])\}/g, "$1;}");
93 // Replace 0(px,em,%) with 0.
94 istr = istr.replace(/([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)/g, "$1$2");
96 // Replace 0 0 0 0; with 0. (by this time, we have reduced spaces etc..
97 istr = istr.replace(/:0 0 0 0;/g, ":0;");
98 istr = istr.replace(/:0 0 0;/g, ":0;");
99 istr = istr.replace(/:0 0;/g, ":0;");
100 // Replace background-position:0; with background-position:0 0;
101 istr = istr.replace(/background-position:0;/g, "background-position:0 0;");
103 // Replace 0.6 to .6, but only when preceded by : or a white-space
104 istr = istr.replace(/(:|\s)0+\.(\d+)/g, "$1.$2");
106 // Shorten colors from rgb(51,102,153) to #336699
107 // This makes it more likely that it'll get further compressed in the next step.
109 istr = istr.replace(/rgb\s*\(\s*([0-9,\s]+)\s*\)/g, function(m, c)
111 var rgb = c.split(',');
113 for (var i = 0; i < rgb.length; i++) {
114 var val = parseInt(rgb[i]);
118 hx+=val.toString(16);
126 // Shorten colors from #AABBCC to #ABC. Note that we want to make sure
127 // the color is not preceded by either ", " or =. Indeed, the property
128 // filter: chroma(color="#FFFFFF");
130 // filter: chroma(color="#FFF");
131 // which makes the filter break in IE.
133 istr = istr.replace(/([^"'=\s])(\s*)#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/g,
134 function(m, g1,g2,g3,g4,g5,g6,g7,g8) {
138 if (g3.toLowerCase() == g4.toLowerCase() &&
139 g5.toLowerCase() == g6.toLowerCase() &&
140 g7.toLowerCase() == g8.toLowerCase()) {
142 return g1+g2+'#' + g3+g5+g7;
150 // Remove empty rules.
151 istr = istr.replace(/[^\}]+\{;\}/g, "");
153 if (linebreakpos >= 0) {
154 // Some source control tools don't like it when files containing lines longer
155 // than, say 8000 characters, are checked in. The linebreak option is used in
156 // that case to split long lines after a specific column.
158 int linestartpos = 0;
159 sb = new StringBuffer(css);
160 while (i < sb.length()) {
161 char c = sb.charAt(i++);
162 if (c == '}' && i - linestartpos > linebreakpos) {
171 // Replace the pseudo class for the Box Model Hack
172 istr = istr.replace(/___PSEUDOCLASSBMH___/g, "\"\\\\\"}\\\\\"\"");
174 // Trim the final string (for any leading or trailing white spaces)
175 istr = istr.replace(/^\s+/g, '');
176 istr = istr.replace(/\s+$/g, '');
179 // Write the output...