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