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