roojs-ui.js
[roojs1] / buildSDK / cssminify.js
1 //<script type="text/javascript">
2 /**
3  * YUI Compressor
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
8  *
9  * This code is a port of Isaac Schlueter's cssmin utility.
10  *
11  * Usage: seed buildSDK/cssminify.js {OUTPUT} {files........}
12  */ 
13
14
15
16 File = imports.File.File;
17 GLib = imports.gi.GLib;  
18 // let's see if this works..
19 // should be run from top level..
20 var pa = GLib.get_current_dir();
21
22 var args = Array.prototype.slice.call(Seed.argv);
23 args.shift(); //seed
24 args.shift(); // pack.js
25
26 var outname = args.shift();
27 //println(pack(File.read(pa+'/css/basic-dialog.css')));
28
29 //@import url("reset-min.css");
30 var out = '';
31 args.forEach(function(l) {
32     
33     //out += pack(File.read(pa+'/css/' + l)).replace(/\}/g, "}\n")+"\n";
34     out += pack(File.read(l)) + "\n";
35 });
36
37 File.write(outname, out); 
38     
39      
40 print("written " + outname);
41 // and the themese...
42 //ytheme-aero.css
43 //ytheme-gray.css
44 //ytheme-vista.css
45
46   
47   
48   
49   
50   
51         // string manip!?
52 function pack(istr) {
53     
54         
55         function removeComments(str)
56         {
57             var r = ''; //ret
58             var si =0;
59             var  iemac = false;
60             
61             while ((si = str.indexOf("/*")) > -1) {
62                 ei = str.indexOf("*/");
63                  
64                 
65                 if (ei >= si + 2) {
66                     if (str.charAt(ei-1) == '\\') {   // eg. /* \*/
67                         si = ei+2;
68                         iemac = true;
69                         // Looks like a comment to hide rules from IE Mac.
70                         // Leave this comment, and the following one, alone...
71                         // shunt:
72                         
73                         r += str.substring(0, ei) ;
74                         str = str.substring(ei);
75                         continue;
76                     }
77                     if (iemac) {
78                         si = ei + 2;
79                         iemac = false;    
80                         r += str.substring(0, ei) ;
81                         str = str.substring(ei+2);
82                         continue;
83                     }
84                     
85                     r += str.substring(0, si) ;
86                     str = str.substring(ei+2);
87                     continue;
88                 }
89                 // did not find end..
90                 r += str.substring(0, 2);
91                 str = str.substring(2);
92                     
93                     
94             }
95                 r += str;
96             return r;
97         }
98         
99         istr = removeComments(istr);
100         //println("after comments remove: " + istr);
101         
102         // Normalize all whitespace strings to single spaces. Easier to work with that way.
103         istr = istr.replace(/\s+/g, ' ');
104
105         // Make a pseudo class for the Box Model Hack ????
106         //css = css.replaceAll("\"\\\\\"}\\\\\"\"", "___PSEUDOCLASSBMH___");
107         istr = istr.replace(/"\\"\}\\"/g, "___PSEUDOCLASSBMH___");
108
109         // Remove the spaces before the things that should not have spaces before them.
110         // But, be careful not to turn "p :link {...}" into "p:link{...}"
111         // Swap out any pseudo-class colons with the token, and then swap back.
112         
113         istr = istr.replace(/(^|\})(([^\{:])+:)+([^\{]*\{)/g, function(m) {
114             return m.replace(/:/g, "___PSEUDOCLASSCOLON___");
115         });
116         
117         
118         istr = istr.replace(/\s+([!{};:>+\(\)\],])/g, "$1");
119         istr = istr.replace(/___PSEUDOCLASSCOLON___/g, ":");
120
121         // Remove the spaces after the things that should not have spaces after them.
122         istr = istr.replace(/([!{}:;>+\(\[,])\s+/g, "$1");
123
124         // Add the semicolon where it's missing.
125         istr = istr.replace(/([^;\}])\}/g, "$1;}");
126
127         // Replace 0(px,em,%) with 0.
128         istr = istr.replace(/([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)/g, "$1$2");
129
130         // Replace 0 0 0 0; with 0. (by this time, we have reduced spaces etc..
131         istr = istr.replace(/:0 0 0 0;/g, ":0;");
132         istr = istr.replace(/:0 0 0;/g, ":0;");
133         istr = istr.replace(/:0 0;/g, ":0;");
134         // Replace background-position:0; with background-position:0 0;
135         istr = istr.replace(/background-position:0;/g, "background-position:0 0;");
136
137         // Replace 0.6 to .6, but only when preceded by : or a white-space
138         istr = istr.replace(/(:|\s)0+\.(\d+)/g, "$1.$2");
139
140         // Shorten colors from rgb(51,102,153) to #336699
141         // This makes it more likely that it'll get further compressed in the next step.
142         
143         istr = istr.replace(/rgb\s*\(\s*([0-9,\s]+)\s*\)/g, function(m, c)
144             {
145                 var rgb = c.split(',');
146                 var hx = '';
147                 for (var  i = 0; i < rgb.length; i++) {
148                     var val = parseInt(rgb[i]);
149                     if (val < 16) {
150                         hx+="0";
151                     }
152                     hx+=val.toString(16);
153                     
154                 }
155                 return '#' + hx;
156             });
157         
158          
159
160         // Shorten colors from #AABBCC to #ABC. Note that we want to make sure
161         // the color is not preceded by either ", " or =. Indeed, the property
162         //     filter: chroma(color="#FFFFFF");
163         // would become
164         //     filter: chroma(color="#FFF");
165         // which makes the filter break in IE.
166         
167         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, 
168                 function(m, g1,g2,g3,g4,g5,g6,g7,g8) {
169                     
170                      
171                         
172                         if (g3.toLowerCase() == g4.toLowerCase() &&
173                             g5.toLowerCase() == g6.toLowerCase() &&
174                             g7.toLowerCase() == g8.toLowerCase()) {
175                                 
176                                 return g1+g2+'#' + g3+g5+g7;
177                         }
178                      
179                    
180                     return m;
181                     
182                 });
183          
184         // Remove empty rules.
185         istr = istr.replace(/[^\}]+\{;\}/g, "");
186 /*
187         if (linebreakpos >= 0) {
188             // Some source control tools don't like it when files containing lines longer
189             // than, say 8000 characters, are checked in. The linebreak option is used in
190             // that case to split long lines after a specific column.
191             int i = 0;
192             int linestartpos = 0;
193             sb = new StringBuffer(css);
194             while (i < sb.length()) {
195                 char c = sb.charAt(i++);
196                 if (c == '}' && i - linestartpos > linebreakpos) {
197                     sb.insert(i, '\n');
198                     linestartpos = i;
199                 }
200             }
201
202             css = sb.toString();
203         }
204 */
205         // Replace the pseudo class for the Box Model Hack
206         istr = istr.replace(/___PSEUDOCLASSBMH___/g, "\"\\\\\"}\\\\\"\"");
207
208         // Trim the final string (for any leading or trailing white spaces)
209         istr = istr.replace(/^\s+/g, '');
210         istr = istr.replace(/\s+$/g, '');
211
212
213         // Write the output...
214         return istr;
215 }