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