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