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