diff --git a/lib/Minify/CSS.php b/lib/Minify/CSS.php index 05fde8f..8832518 100644 --- a/lib/Minify/CSS.php +++ b/lib/Minify/CSS.php @@ -24,12 +24,70 @@ class Minify_CSS { * * @param string $css * - * @param array $options optional. To enable URL rewriting, set the value + * @param array $options available options: + * + * 'preserveComments': (default true) multi-line comments that begin + * with "/*!" will be preserved with newlines before and after to + * enhance readability. + * + * @return string + */ + public static function minify($css, $options = array()) + { + if (isset($options['preserveComments']) + && !$options['preserveComments']) { + return self::_minify($css, $options); + } + $ret = ''; + while (1) { + list($beforeComment, $comment, $afterComment) + = self::_nextYuiComment($css); + $ret .= self::_minify($beforeComment, $options); + if (false === $comment) { + break; + } + $ret .= $comment; + $css = $afterComment; + } + return $ret; + } + + /** + * Extract comments that YUI Compressor preserves. + * + * @param string $in input + * + * @return array 3 elements are returned. If a YUI comment is found, the + * 2nd element is the comment and the 1st and 2nd are the surrounding + * strings. If no comment is found, the entire string is returned as the + * 1st element and the other two are false. + */ + private static function _nextYuiComment($in) + { + return ( + (false !== ($start = strpos($in, '/*!'))) + && (false !== ($end = strpos($in, '*/', $start + 3))) + ) + ? array( + substr($in, 0, $start) + ,"\n/*" . substr($in, $start + 3, $end - $start - 1) . "\n" + ,substr($in, -(strlen($in) - $end - 2)) + ) + : array($in, false, false); + } + + /** + * Minify a CSS string + * + * @param string $css + * + * @param array $options To enable URL rewriting, set the value * for key 'prependRelativePath'. * * @return string */ - public static function minify($css, $options = array()) { + protected static function _minify($css, $options) + { // preserve empty comment after '>' // http://www.webdevout.net/css-hacks#in_css-selectors $css = preg_replace('/>\\/\\*\\s*\\*\\//', '>/*keep*/', $css); @@ -44,7 +102,7 @@ class Minify_CSS { $css = preg_replace_callback('/\\s*\\/\\*([\\s\\S]*?)\\*\\/\\s*/' ,array('Minify_CSS', '_commentCB'), $css); - // compress whitespace. Yes, this will affect "copyright" comments. + // compress whitespace. $css = preg_replace('/\s+/', ' ', $css); // leave needed comments @@ -116,16 +174,11 @@ class Minify_CSS { protected static function _commentCB($m) { $m = $m[1]; - // $m is everything after the opening tokens and before the closing tokens - // but return will replace the entire comment. + // $m is everything after the opening tokens and before the closing + // tokens but return will replace the entire comment. if ($m === 'keep') { return '/*keep*/'; } - if (false !== strpos($m, 'copyright')) { - // contains copyright, preserve - self::$_inHack = false; - return "/*{$m}*/"; - } if (self::$_inHack) { // inversion: feeding only to one browser if (preg_match('/^\\/\\s*(\\S[\\s\\S]+?)\\s*\\/\\*/', $m, $n)) { @@ -137,7 +190,7 @@ class Minify_CSS { self::$_inHack = true; return '/*\\*/'; } - if (substr($m, 0, 1) === '/') { + if ($m[0] === '/') { self::$_inHack = true; return '/*/*/'; } diff --git a/lib/Minify/Javascript.php b/lib/Minify/Javascript.php index ab785a9..ea48f0b 100644 --- a/lib/Minify/Javascript.php +++ b/lib/Minify/Javascript.php @@ -17,17 +17,18 @@ class Minify_Javascript { /** * Minify a Javascript string * - * @param string $js input javascript + * @param string $js * * @param array $options available options: * * 'preserveComments': (default true) multi-line comments that begin * with "/*!" will be preserved with newlines before and after to - * preserve readability. + * enhance readability. * * @return string */ - public static function minify($js, $options = array()) { + public static function minify($js, $options = array()) + { if (isset($options['preserveComments']) && !$options['preserveComments']) { return trim(JSMin::minify($js)); @@ -49,26 +50,25 @@ class Minify_Javascript { /** * Extract comments that YUI Compressor preserves. * - * @param string $js input + * @param string $in input * * @return array 3 elements are returned. If a YUI comment is found, the * 2nd element is the comment and the 1st and 2nd are the surrounding - * strings. If no comment is found, the entire string is returned as the 1st - * element and the other two are false. + * strings. If no comment is found, the entire string is returned as the + * 1st element and the other two are false. */ - private static function _nextYuiComment($js) + private static function _nextYuiComment($in) { return ( - (false !== ($start = strpos($js, '/*!'))) - && (false !== ($end = strpos($js, '*/'))) - && ($start < $end) + (false !== ($start = strpos($in, '/*!'))) + && (false !== ($end = strpos($in, '*/', $start + 3))) ) ? array( - substr($js, 0, $start) - ,"\n/*" . substr($js, $start + 3, $end - $start - 1) . "\n" - ,substr($js, -(strlen($js) - $end - 2)) + substr($in, 0, $start) + ,"\n/*" . substr($in, $start + 3, $end - $start - 1) . "\n" + ,substr($in, -(strlen($in) - $end - 2)) ) - : array($js, false, false); + : array($in, false, false); } } diff --git a/web/test/_test_files/css/comments.css b/web/test/_test_files/css/comments.css index fa2930a..bb4eaed 100644 --- a/web/test/_test_files/css/comments.css +++ b/web/test/_test_files/css/comments.css @@ -1,6 +1,6 @@ /* block comments get removed */ -/* comments containing the word "copyright" are left in, though */ +/*! YUI Compressor style comments are preserved */ /* but all other comments are removed */ diff --git a/web/test/_test_files/css/comments.min.css b/web/test/_test_files/css/comments.min.css index 03b4e48..daf900f 100644 --- a/web/test/_test_files/css/comments.min.css +++ b/web/test/_test_files/css/comments.min.css @@ -1 +1,2 @@ -/* comments containing the word "copyright" are left in, though */ \ No newline at end of file + +/* YUI Compressor style comments are preserved */ diff --git a/web/test/_test_files/html/before.html b/web/test/_test_files/html/before.html index a50d5aa..a952635 100644 --- a/web/test/_test_files/html/before.html +++ b/web/test/_test_files/html/before.html @@ -49,7 +49,7 @@ if (is.ua.indexOf('gecko') >= 0) { diff --git a/web/test/_test_files/html/before.min.html b/web/test/_test_files/html/before.min.html index 454e1c6..2b5856e 100644 --- a/web/test/_test_files/html/before.min.html +++ b/web/test/_test_files/html/before.min.html @@ -1,7 +1,9 @@