1 //<script type="text/javascript">
4 * Author: Julien Lecomte <jlecomte@yahoo-inc.com>
5 * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
6 * Code licensed under the BSD License:
7 * http://developer.yahoo.net/yui/license.txt
9 * This code is a port of Isaac Schlueter's cssmin utility.
11 * Usage: seed buildSDK/cssmini.js
14 var pa = GLib.get_current_dir();
15 File = imports[pa +'buildSDK/File.js'];
16 GLib = imports.gi.GLib;
17 // let's see if this works..
18 // should be run from top level..
21 //println(pack(File.read(pa+'/css/basic-dialog.css')));
23 File.write(pa+'/css/roojs.css',
25 pack(File.read(pa+'/css/reset-min.css'))+"\n"+
26 pack(File.read(pa+'/css/core.css'))+"\n"+
27 pack(File.read(pa+'/css/tabs.css'))+"\n"+
28 pack(File.read(pa+'/css/form.css'))+"\n"+
29 pack(File.read(pa+'/css/button.css')) +"\n"+
30 pack(File.read(pa+'/css/toolbar.css'))+"\n"+
31 pack(File.read(pa+'/css/resizable.css'))+"\n"+
32 pack(File.read(pa+'/css/grid.css'))+"\n"+
33 pack(File.read(pa+'/css/layout.css'))+"\n"+
34 pack(File.read(pa+'/css/basic-dialog.css'))+"\n"+
35 pack(File.read(pa+'/css/dd.css'))+"\n"+
36 pack(File.read(pa+'/css/tree.css'))+"\n" +
37 pack(File.read(pa+'/css/qtips.css'))+"\n"+
38 pack(File.read(pa+'/css/date.css')) +"\n"+
39 //pack(File.read(pa+'/css/menu.css')) +"\n"+
40 //pack(File.read(pa+'/css/box.css')) +"\n"+
41 pack(File.read(pa+'/css/combo.css')) +"\n"+
43 pack(File.read(pa+'/css/inline-editor.css'))+"\n"
49 println("written css/roojs.css");
64 function removeComments(str)
70 while ((si = str.indexOf("/*")) > -1) {
71 ei = str.indexOf("*/");
75 if (str.charAt(ei-1) == '\\') { // eg. /* \*/
78 // Looks like a comment to hide rules from IE Mac.
79 // Leave this comment, and the following one, alone...
82 r += str.substring(0, ei) ;
83 str = str.substring(ei);
89 r += str.substring(0, ei) ;
90 str = str.substring(ei+2);
94 r += str.substring(0, si) ;
95 str = str.substring(ei+2);
99 r += str.substring(0, 2);
100 str = str.substring(2);
108 istr = removeComments(istr);
109 //println("after comments remove: " + istr);
111 // Normalize all whitespace strings to single spaces. Easier to work with that way.
112 istr = istr.replace(/\s+/g, ' ');
114 // Make a pseudo class for the Box Model Hack ????
115 //css = css.replaceAll("\"\\\\\"}\\\\\"\"", "___PSEUDOCLASSBMH___");
116 istr = istr.replace(/"\\"\}\\"/g, "___PSEUDOCLASSBMH___");
118 // Remove the spaces before the things that should not have spaces before them.
119 // But, be careful not to turn "p :link {...}" into "p:link{...}"
120 // Swap out any pseudo-class colons with the token, and then swap back.
122 istr = istr.replace(/(^|\})(([^\{:])+:)+([^\{]*\{)/g, function(m) {
123 return m.replace(/:/g, "___PSEUDOCLASSCOLON___");
127 istr = istr.replace(/\s+([!{};:>+\(\)\],])/g, "$1");
128 istr = istr.replace(/___PSEUDOCLASSCOLON___/g, ":");
130 // Remove the spaces after the things that should not have spaces after them.
131 istr = istr.replace(/([!{}:;>+\(\[,])\s+/g, "$1");
133 // Add the semicolon where it's missing.
134 istr = istr.replace(/([^;\}])\}/g, "$1;}");
136 // Replace 0(px,em,%) with 0.
137 istr = istr.replace(/([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)/g, "$1$2");
139 // Replace 0 0 0 0; with 0. (by this time, we have reduced spaces etc..
140 istr = istr.replace(/:0 0 0 0;/g, ":0;");
141 istr = istr.replace(/:0 0 0;/g, ":0;");
142 istr = istr.replace(/:0 0;/g, ":0;");
143 // Replace background-position:0; with background-position:0 0;
144 istr = istr.replace(/background-position:0;/g, "background-position:0 0;");
146 // Replace 0.6 to .6, but only when preceded by : or a white-space
147 istr = istr.replace(/(:|\s)0+\.(\d+)/g, "$1.$2");
149 // Shorten colors from rgb(51,102,153) to #336699
150 // This makes it more likely that it'll get further compressed in the next step.
152 istr = istr.replace(/rgb\s*\(\s*([0-9,\s]+)\s*\)/g, function(m, c)
154 var rgb = c.split(',');
156 for (var i = 0; i < rgb.length; i++) {
157 var val = parseInt(rgb[i]);
161 hx+=val.toString(16);
169 // Shorten colors from #AABBCC to #ABC. Note that we want to make sure
170 // the color is not preceded by either ", " or =. Indeed, the property
171 // filter: chroma(color="#FFFFFF");
173 // filter: chroma(color="#FFF");
174 // which makes the filter break in IE.
176 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,
177 function(m, g1,g2,g3,g4,g5,g6,g7,g8) {
181 if (g3.toLowerCase() == g4.toLowerCase() &&
182 g5.toLowerCase() == g6.toLowerCase() &&
183 g7.toLowerCase() == g8.toLowerCase()) {
185 return g1+g2+'#' + g3+g5+g7;
193 // Remove empty rules.
194 istr = istr.replace(/[^\}]+\{;\}/g, "");
196 if (linebreakpos >= 0) {
197 // Some source control tools don't like it when files containing lines longer
198 // than, say 8000 characters, are checked in. The linebreak option is used in
199 // that case to split long lines after a specific column.
201 int linestartpos = 0;
202 sb = new StringBuffer(css);
203 while (i < sb.length()) {
204 char c = sb.charAt(i++);
205 if (c == '}' && i - linestartpos > linebreakpos) {
214 // Replace the pseudo class for the Box Model Hack
215 istr = istr.replace(/___PSEUDOCLASSBMH___/g, "\"\\\\\"}\\\\\"\"");
217 // Trim the final string (for any leading or trailing white spaces)
218 istr = istr.replace(/^\s+/g, '');
219 istr = istr.replace(/\s+$/g, '');
222 // Write the output...