2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
5 * cssmin.php - A simple CSS minifier.
9 * include("cssmin.php");
10 * file_put_contents("path/to/target.css", cssmin::minify(file_get_contents("path/to/source.css")));
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.
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)
30 * Minifies stylesheet definitions
33 * $css_minified = cssmin::minify(file_get_contents("path/to/target/file.css"));
36 * @param string $css Stylesheet definitions as string
37 * @param array|string $options Array or comma speperated list of options:
39 * - remove-last-semicolon: Removes the last semicolon in
40 * the style definition of an element (activated by default).
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
47 * @return string Minified stylesheet definitions
49 public static function minify($css, $options = "remove-last-semicolon")
51 $options = ($options == "") ? array() : (is_array($options) ? $options : explode(",", $options));
52 if (in_array("preserve-urls", $options))
54 // Encode url() to base64
55 $css = preg_replace_callback("/url\s*\((.*)\)/siU", "cssmin_encode_url", $css);
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))
67 // Removes the last semicolon of every style definition
68 $css = str_replace(";}", "}", $css);
71 if (in_array("preserve-urls", $options))
74 $css = preg_replace_callback("/url\s*\((.*)\)/siU", "cssmin_encode_url", $css);
79 * Return a array structure of a stylesheet definitions.
82 * $css_structure = cssmin::toArray(file_get_contents("path/to/target/file.css"));
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
89 public static function toArray($css, $options = "")
92 $css = cssmin::minify($css, $options);
94 preg_match_all("/(.+){(.+:.+);}/U", $css, $items);
95 if (count($items[0]) > 0)
97 for ($i = 0; $i < count($items[0]); $i++)
99 $keys = explode(",", $items[1][$i]);
100 $styles_tmp = explode(";", $items[2][$i]);
102 foreach ($styles_tmp as $style)
104 $style_tmp = explode(":", $style);
105 $styles[$style_tmp[0]] = $style_tmp[1];
109 "keys" => cssmin_array_clean($keys),
110 "styles" => cssmin_array_clean($styles)
117 * Return a array structure created by {@link cssmin::toArray()} to a string.
120 * $css_string = cssmin::toString($css_structure);
126 public static function toString(array $array)
129 foreach ($array as $item)
131 $r .= implode(",", $item["keys"]) . "{";
132 foreach ($item["styles"] as $key => $value)
134 $r .= $key . ":" . $value . ";";
143 * Trims all elements of the array and removes empty elements.
145 * @param array $array
148 function cssmin_array_clean(array $array)
151 if (cssmin_array_is_assoc($array))
153 foreach ($array as $key => $value)
155 $r[$key] = trim($value);
160 foreach ($array as $value)
162 if (trim($value) != "")
171 * Return if a value is a associative array.
173 * @param array $array
176 function cssmin_array_is_assoc($array)
178 if (!is_array($array))
184 krsort($array, SORT_STRING);
185 return !is_numeric(key($array));
189 * Encodes a url() expression.
191 * @param array $match
194 function cssmin_encode_url($match)
196 return "url(" . base64_encode(trim($match[1])) . ")";
199 * Decodes a url() expression.
201 * @param array $match
204 function cssmin_decode_url($match)
206 return "url(" . base64_decode($match[1]) . ")";