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