diff --git a/composer.json b/composer.json index 21ae0f7..aa6409d 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "ext-openssl": "*", "ext-curl": "*", "ext-mbstring": "*", - "websharks/js-minifier": "dev-master" + "websharks/js-minifier": "dev-master", + "websharks/css-minifier": "dev-master" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/composer.lock b/composer.lock index 18af7a9..317c83f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,74 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "c58e108b7582673e4281cb68f12e37d8", + "hash": "eef42e25726c877b291c8a0dcd93c94c", "packages": [ + { + "name": "websharks/css-minifier", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/websharks/css-minifier.git", + "reference": "7460d5ff99090053a087726f49fbda367ddf55c1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/websharks/css-minifier/zipball/7460d5ff99090053a087726f49fbda367ddf55c1", + "reference": "7460d5ff99090053a087726f49fbda367ddf55c1", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": ">=5.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "WebSharks\\CssMinifier\\": "src/includes/classes" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-3.0+" + ], + "authors": [ + { + "name": "websharks", + "homepage": "http://websharks-inc.com/", + "role": "company" + }, + { + "name": "jaswsinc", + "homepage": "http://jaswsinc.com/", + "role": "developer" + }, + { + "name": "raamdev", + "homepage": "http://raam.org/", + "role": "developer" + } + ], + "description": "Compresses CSS.", + "homepage": "https://github.com/websharks/css-minifier", + "keywords": [ + "compressor", + "css", + "websharks" + ], + "time": "2015-05-10 22:25:57" + }, { "name": "websharks/js-minifier", "version": "dev-master", "source": { "type": "git", "url": "https://github.com/websharks/js-minifier.git", - "reference": "212cca9bb69a1e591219a86aa88c285cc6362e30" + "reference": "c138a090cc60f549e7071e782904f9ed69a27a42" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/websharks/js-minifier/zipball/212cca9bb69a1e591219a86aa88c285cc6362e30", - "reference": "212cca9bb69a1e591219a86aa88c285cc6362e30", + "url": "https://api.github.com/repos/websharks/js-minifier/zipball/c138a090cc60f549e7071e782904f9ed69a27a42", + "reference": "c138a090cc60f549e7071e782904f9ed69a27a42", "shasum": "" }, "require": { @@ -59,14 +113,15 @@ "javascript", "websharks" ], - "time": "2015-05-10 11:46:24" + "time": "2015-05-10 23:03:53" } ], "packages-dev": [], "aliases": [], "minimum-stability": "dev", "stability-flags": { - "websharks/js-minifier": 20 + "websharks/js-minifier": 20, + "websharks/css-minifier": 20 }, "prefer-stable": true, "prefer-lowest": false, diff --git a/src/includes/classes/Core.php b/src/includes/classes/Core.php index 59bd1cd..15ec283 100644 --- a/src/includes/classes/Core.php +++ b/src/includes/classes/Core.php @@ -1877,12 +1877,24 @@ protected function maybeCompressCssCode($css) if (!$css || !empty($disabled)) { goto finale; // Nothing to do. } + if (strlen($css) > 1000000) { + // Exclude VERY large files. Too time-consuming. + // Should really be compressed ahead-of-time anyway. + goto finale; // Don't compress HUGE files. + } + try { // Catch CSS compression-related exceptions. + if (!($compressed_css = \WebSharks\CssMinifier\Core::compress($css))) { + // `E_USER_NOTICE` to avoid a show-stopping problem. + trigger_error('CSS compression failure.', E_USER_NOTICE); + } else { + $css = $compressed_css; + } // Use compressed CSS file. + } catch (\Exception $exception) { + trigger_error($exception->getMessage(), E_USER_NOTICE); + } $regex = '/(?:[a-z0-9]+\:)?\/\/'.preg_quote($this->currentUrlHost(), '/').'\//i'; $css = preg_replace($regex, '/', $css); // To absolute paths. - if (($compressed_css = $this->compressCss($css))) { - $css = $compressed_css; - } finale: // Target point; finale/return value. if ($css) { @@ -1898,65 +1910,6 @@ protected function maybeCompressCssCode($css) return $css; } - /** - * Compresses CSS code (as quickly as possible). - * - * @since 140417 Initial release. - * - * @param string $css Any CSS code (excluding `` tags please). - * - * @return string Compressed CSS code. This removes CSS comments, extra whitespace, and it compresses HEX color codes whenever possible. - * In addition, this will also remove any unnecessary `;` line terminators to further optimize the overall file size. - * - * @TODO There are some additional optimizations we could do when it comes to shorthand CSS. - * e.g. margin: 0 auto 0 auto; could be converted to margin: 0 auto; - */ - protected function compressCss($css) - { - if (!($css = (string) $css)) { - return $css; // Nothing to do. - } - $static = &static::$static[__FUNCTION__]; - - if (!isset($static['replace'], $static['with'], $static['colors'])) { - $de_spacifiables = array('{', '}', '!=', '|=', '^=', '$=', '*=', '~=', '=', '~', ':', ';', ',', '>'); - $de_spacifiables = implode('|', $this->pregQuoteDeep($de_spacifiables, '/')); - - $static['replace'] = array( - 'comments' => '/\/\*.*?\*\//s', - 'line_breaks' => '/['."\r\n\t".']+/', - 'extra_spaces' => '/ +/', - 'de_spacifiables' => '/ *('.$de_spacifiables.') */', - 'unnecessary_;s' => '/;\}/', - ); - $static['with'] = array('', '', ' ', '${1}', '}'); - $static['colors'] = '/(?P[:,\h]+#)(?P[a-z0-9]{6})/i'; - } - $css = preg_replace($static['replace'], $static['with'], $css); - $css = preg_replace_callback($static['colors'], array($this, 'maybeCompressCssColorCb'), $css); - - return $css ? trim($css) : $css; - } - - /** - * Compresses HEX color codes. - * - * @since 140417 Initial release. - * - * @param array $m Regular expression matches. - * - * @return string Full match with compressed HEX color code. - */ - protected function maybeCompressCssColorCb(array $m) - { - $m['hex'] = strtoupper($m['hex']); // Convert to uppercase for easy comparison. - - if ($m['hex'][0] === $m['hex'][1] && $m['hex'][2] === $m['hex'][3] && $m['hex'][4] === $m['hex'][5]) { - return $m['context'].$m['hex'][0].$m['hex'][2].$m['hex'][4]; - } - return $m[0]; - } - /********************************************************************************************************/ /*