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
12 * Usage: gjs -I buildSDK buildSDK/cssmini.js
17 GLib = imports.gi.GLib;
19 // let's see if this works..
20 // should be run from top level..
21 var pa = GLib.get_current_dir();
25 //println(pack(File.read(pa+'/css/basic-dialog.css')));
27 var lines = String(GLib.file_get_contents(pa + '/css/roojs-debug.css')).split(/\n/);
28 //@import url("reset-min.css");
30 lines.forEach(function(l) {
31 if (!l.match(/^@import/)) {
34 l = l.replace(/^[^"]+"([^"]+)".*/, '$1');
36 //out += pack(File.read(pa+'/css/' + l)).replace(/\}/g, "}\n")+"\n";
37 out += pack(String(GLib.file_get_contents(pa+'/css/' + l))) + "\n";
39 var f = Gio.file_new_for_path(String(pa+'/css/roojs.css'));
40 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
41 data_out.put_string(out, null);
45 print("written css/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...