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