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