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