]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarTheme/cssmin.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarTheme / cssmin.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
4 /**
5  * cssmin.php - A simple CSS minifier.
6  * --
7  * 
8  * <code>
9  * include("cssmin.php");
10  * file_put_contents("path/to/target.css", cssmin::minify(file_get_contents("path/to/source.css")));
11  * </code>
12  * --
13  * 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
15  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
16  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
17  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  * --
20  *
21  * @package     cssmin
22  * @author              Joe Scylla <joe.scylla@gmail.com>
23  * @copyright   2008 Joe Scylla <joe.scylla@gmail.com>
24  * @license     http://opensource.org/licenses/mit-license.php MIT License
25  * @version     1.0.1.b3 (2008-10-02)
26  */
27 class cssmin
28         {
29         /**
30          * Minifies stylesheet definitions
31          *
32          * <code>
33          * $css_minified = cssmin::minify(file_get_contents("path/to/target/file.css"));
34          * </code>
35          * 
36          * @param       string                  $css            Stylesheet definitions as string
37          * @param       array|string    $options        Array or comma speperated list of options:
38          *                                                                              
39          *                                                                              - remove-last-semicolon: Removes the last semicolon in 
40          *                                                                              the style definition of an element (activated by default).
41          *                                                                              
42          *                                                                              - preserve-urls: Preserves every url defined in an url()-
43          *                                                                              expression. This option is only required if you have 
44          *                                                                              defined really uncommon urls with multiple spaces or 
45          *                                                                              combination of colon, semi-colon, braces with leading or 
46          *                                                                              following spaces.
47          * @return      string                  Minified stylesheet definitions
48          */
49         public static function minify($css, $options = "remove-last-semicolon")
50                 {
51                 $options = ($options == "") ? array() : (is_array($options) ? $options : explode(",", $options));
52                 if (in_array("preserve-urls", $options))
53                         {
54                         // Encode url() to base64
55                         $css = preg_replace_callback("/url\s*\((.*)\)/siU", "cssmin_encode_url", $css);
56                         }
57                 // Remove comments
58                 $css = preg_replace("/\/\*[\d\D]*?\*\/|\t+/", " ", $css);
59                 // Replace CR, LF and TAB to spaces
60                 $css = str_replace(array("\n", "\r", "\t"), " ", $css);
61                 // Replace multiple to single space
62                 $css = preg_replace("/\s\s+/", " ", $css);
63                 // Remove unneeded spaces
64                 $css = preg_replace("/\s*({|}|\[|=|~|\+|>|\||;|:|,)\s*/", "$1", $css);
65                 if (in_array("remove-last-semicolon", $options))
66                         {
67                         // Removes the last semicolon of every style definition
68                         $css = str_replace(";}", "}", $css);
69                         }
70                 $css = trim($css);
71                 if (in_array("preserve-urls", $options))
72                         {
73                         // Decode url()
74                         $css = preg_replace_callback("/url\s*\((.*)\)/siU", "cssmin_encode_url", $css);
75                         }
76                 return $css;
77                 }
78         /**
79          * Return a array structure of a stylesheet definitions.
80          *
81          * <code>
82          * $css_structure = cssmin::toArray(file_get_contents("path/to/target/file.css"));
83          * </code>
84          * 
85          * @param       string          $css                    Stylesheet definitions as string
86          * @param       string          $options                Options for {@link cssmin::minify()}
87          * @return      array                                           Structure of the stylesheet definitions as array
88          */
89         public static function toArray($css, $options = "")
90                 {
91                 $r = array();
92                 $css = cssmin::minify($css, $options);
93                 $items = array();
94                 preg_match_all("/(.+){(.+:.+);}/U", $css, $items);
95                 if (count($items[0]) > 0)
96                         {
97                         for ($i = 0; $i < count($items[0]); $i++)
98                                 {
99                                 $keys           = explode(",", $items[1][$i]);
100                                 $styles_tmp     = explode(";", $items[2][$i]);
101                                 $styles = array();
102                                 foreach ($styles_tmp as $style)
103                                         {
104                                         $style_tmp = explode(":", $style);
105                                         $styles[$style_tmp[0]] = $style_tmp[1];
106                                         }
107                                 $r[] = array
108                                         (
109                                         "keys"          => cssmin_array_clean($keys),
110                                         "styles"        => cssmin_array_clean($styles)
111                                         );
112                                 }
113                         }
114                 return $r;
115                 }
116         /**
117          * Return a array structure created by {@link cssmin::toArray()} to a string.
118          *
119          * <code>
120          * $css_string = cssmin::toString($css_structure);
121          * </code>
122          * 
123          * @param       array           $css
124          * @return      array
125          */
126         public static function toString(array $array)
127                 {
128                 $r = "";
129                 foreach ($array as $item)
130                         {
131                         $r .= implode(",", $item["keys"]) . "{";
132                         foreach ($item["styles"] as $key => $value)
133                                 {
134                                 $r .= $key . ":" . $value . ";";
135                                 }
136                         $r .= "}";
137                         }
138                 return $r;
139                 }
140         }
141
142 /**
143  * Trims all elements of the array and removes empty elements. 
144  *
145  * @param       array           $array
146  * @return      array
147  */
148 function cssmin_array_clean(array $array)
149         {
150         $r = array();
151         if (cssmin_array_is_assoc($array))
152                 {
153                 foreach ($array as $key => $value)
154                         {
155                         $r[$key] = trim($value);
156                         }
157                 }
158         else
159                 {
160                 foreach ($array as $value)
161                         {
162                         if (trim($value) != "")
163                                 {
164                                 $r[] = trim($value);
165                                 }
166                         }
167                 }
168         return $r;
169         }
170 /**
171  * Return if a value is a associative array.
172  *
173  * @param       array           $array
174  * @return      bool
175  */
176 function cssmin_array_is_assoc($array)
177         {
178         if (!is_array($array))
179                 {
180                 return false;
181                 }
182         else
183                 {
184                 krsort($array, SORT_STRING);
185                 return !is_numeric(key($array));
186                 }
187         }
188 /**
189  * Encodes a url() expression.
190  *
191  * @param       array   $match
192  * @return      string
193  */
194 function cssmin_encode_url($match)
195         {
196         return "url(" . base64_encode(trim($match[1])) . ")";
197         }
198 /**
199  * Decodes a url() expression.
200  *
201  * @param       array   $match
202  * @return      string
203  */
204 function cssmin_decode_url($match)
205         {
206         return "url(" . base64_decode($match[1]) . ")";
207         }
208 ?>