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