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">
14 // let's see if this works..
15 // should be run from top level..
17 var pa = File.getcwd();
18 //println(pack(File.read(pa+'/css/basic-dialog.css')));
20 File.write(pa+'/cssX/roojs.css',
22 pack(File.read(pa+'/cssX/reset-min.css'))+"\n"+
23 pack(File.read(pa+'/cssX/core.css'))+"\n"+
24 pack(File.read(pa+'/cssX/tabs.css'))+"\n"+
25 pack(File.read(pa+'/cssX/form.css'))+"\n"+
26 pack(File.read(pa+'/cssX/button.css')) +"\n"+
27 pack(File.read(pa+'/cssX/toolbar.css'))+"\n"+
28 pack(File.read(pa+'/cssX/resizable.css'))+"\n"+
29 pack(File.read(pa+'/cssX/grid.css'))+"\n"+
30 pack(File.read(pa+'/cssX/layout.css'))+"\n"+
31 pack(File.read(pa+'/cssX/basic-dialog.css'))+"\n"+
32 pack(File.read(pa+'/cssX/dd.css'))+"\n"+
33 pack(File.read(pa+'/cssX/tree.css'))+"\n" +
34 pack(File.read(pa+'/cssX/qtips.css'))+"\n"+
35 pack(File.read(pa+'/cssX/date-picker.css')) +"\n"+
36 pack(File.read(pa+'/cssX/menu.css')) +"\n"+
37 pack(File.read(pa+'/cssX/box.css')) +"\n"+
38 pack(File.read(pa+'/cssX/combo.css')) +"\n"+
39 pack(File.read(pa+'/cssX/editor.css'))+"\n"
45 println("written cssX/roojs.css");
60 function removeComments(str)
66 while ((si = str.indexOf("/*")) > -1) {
67 ei = str.indexOf("*/");
71 if (str.charAt(ei-1) == '\\') { // eg. /* \*/
74 // Looks like a comment to hide rules from IE Mac.
75 // Leave this comment, and the following one, alone...
78 r += str.substring(0, ei) ;
79 str = str.substring(ei);
85 r += str.substring(0, ei) ;
86 str = str.substring(ei+2);
90 r += str.substring(0, si) ;
91 str = str.substring(ei+2);
95 r += str.substring(0, 2);
96 str = str.substring(2);
104 istr = removeComments(istr);
105 //println("after comments remove: " + istr);
107 // Normalize all whitespace strings to single spaces. Easier to work with that way.
108 istr = istr.replace(/\s+/g, ' ');
110 // Make a pseudo class for the Box Model Hack ????
111 //css = css.replaceAll("\"\\\\\"}\\\\\"\"", "___PSEUDOCLASSBMH___");
112 istr = istr.replace(/"\\"\}\\"/g, "___PSEUDOCLASSBMH___");
114 // Remove the spaces before the things that should not have spaces before them.
115 // But, be careful not to turn "p :link {...}" into "p:link{...}"
116 // Swap out any pseudo-class colons with the token, and then swap back.
118 istr = istr.replace(/(^|\})(([^\{:])+:)+([^\{]*\{)/g, function(m) {
119 return m.replace(/:/g, "___PSEUDOCLASSCOLON___");
123 istr = istr.replace(/\s+([!{};:>+\(\)\],])/g, "$1");
124 istr = istr.replace(/___PSEUDOCLASSCOLON___/g, ":");
126 // Remove the spaces after the things that should not have spaces after them.
127 istr = istr.replace(/([!{}:;>+\(\[,])\s+/g, "$1");
129 // Add the semicolon where it's missing.
130 istr = istr.replace(/([^;\}])\}/g, "$1;}");
132 // Replace 0(px,em,%) with 0.
133 istr = istr.replace(/([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)/g, "$1$2");
135 // Replace 0 0 0 0; with 0. (by this time, we have reduced spaces etc..
136 istr = istr.replace(/:0 0 0 0;/g, ":0;");
137 istr = istr.replace(/:0 0 0;/g, ":0;");
138 istr = istr.replace(/:0 0;/g, ":0;");
139 // Replace background-position:0; with background-position:0 0;
140 istr = istr.replace(/background-position:0;/g, "background-position:0 0;");
142 // Replace 0.6 to .6, but only when preceded by : or a white-space
143 istr = istr.replace(/(:|\s)0+\.(\d+)/g, "$1.$2");
145 // Shorten colors from rgb(51,102,153) to #336699
146 // This makes it more likely that it'll get further compressed in the next step.
148 istr = istr.replace(/rgb\s*\(\s*([0-9,\s]+)\s*\)/g, function(m, c)
150 var rgb = c.split(',');
152 for (var i = 0; i < rgb.length; i++) {
153 var val = parseInt(rgb[i]);
157 hx+=val.toString(16);
165 // Shorten colors from #AABBCC to #ABC. Note that we want to make sure
166 // the color is not preceded by either ", " or =. Indeed, the property
167 // filter: chroma(color="#FFFFFF");
169 // filter: chroma(color="#FFF");
170 // which makes the filter break in IE.
172 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,
173 function(m, g1,g2,g3,g4,g5,g6,g7,g8) {
177 if (g3.toLowerCase() == g4.toLowerCase() &&
178 g5.toLowerCase() == g6.toLowerCase() &&
179 g7.toLowerCase() == g8.toLowerCase()) {
181 return g1+g2+'#' + g3+g5+g7;
189 // Remove empty rules.
190 istr = istr.replace(/[^\}]+\{;\}/g, "");
192 if (linebreakpos >= 0) {
193 // Some source control tools don't like it when files containing lines longer
194 // than, say 8000 characters, are checked in. The linebreak option is used in
195 // that case to split long lines after a specific column.
197 int linestartpos = 0;
198 sb = new StringBuffer(css);
199 while (i < sb.length()) {
200 char c = sb.charAt(i++);
201 if (c == '}' && i - linestartpos > linebreakpos) {
210 // Replace the pseudo class for the Box Model Hack
211 istr = istr.replace(/___PSEUDOCLASSBMH___/g, "\"\\\\\"}\\\\\"\"");
213 // Trim the final string (for any leading or trailing white spaces)
214 istr = istr.replace(/^\s+/g, '');
215 istr = istr.replace(/\s+$/g, '');
218 // Write the output...