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