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