1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-06 06:07:24 +02:00

+ YUI Compressor-style comment preservation to CSS.php, adjusted tests to match

This commit is contained in:
Steve Clay
2008-05-14 12:58:54 +00:00
parent 2cbf5c9b39
commit 1c1a6f327d
7 changed files with 88 additions and 32 deletions

View File

@@ -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 '/*/*/';
}

View File

@@ -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);
}
}

View File

@@ -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 */

View File

@@ -1 +1,2 @@
/* comments containing the word "copyright" are left in, though */
/* YUI Compressor style comments are preserved */

View File

@@ -49,7 +49,7 @@ if (is.ua.indexOf('gecko') >= 0) {
</script>
<!--[if IE 6]>
<style type="text/css">
/* copyright: you'll need CDATA for this -- < & */
/*! copyright: you'll need CDATA for this < & */
body {background:white;}
</style>
<![endif]-->

View File

@@ -1,7 +1,9 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" ><head><meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /><meta name="author" content="Dave Shea" /><meta name="keywords" content="design, css, cascading, style, sheets, xhtml, graphic design, w3c, web standards, visual, display" /><meta name="description" content="A demonstration of what can be accomplished visually through CSS-based design." /><meta name="robots" content="all" /><title>css Zen Garden: The Beauty in CSS Design</title><script type="text/javascript">var is={ie:navigator.appName=='Microsoft Internet Explorer',java:navigator.javaEnabled(),ns:navigator.appName=='Netscape',ua:navigator.userAgent.toLowerCase(),version:parseFloat(navigator.appVersion.substr(21))||parseFloat(navigator.appVersion),win:navigator.platform=='Win32'}
is.mac=is.ua.indexOf('mac')>=0;if(is.ua.indexOf('opera')>=0){is.ie=is.ns=false;is.opera=true;}
if(is.ua.indexOf('gecko')>=0){is.ie=is.ns=false;is.gecko=true;}</script><script type="text/javascript">/*<![CDATA[*/var i=0;while(++i<10)
{}/*]]>*/</script><script type="text/javascript">i=1;</script><script type="text/javascript">/*<![CDATA[*/(i<1);/*]]>*/</script><!--[if IE 6]><style type="text/css">/*<![CDATA[*//* copyright: you'll need CDATA for this -- < & */body{background:white}/*]]>*/</style><![endif]--><style type="text/css" title="currentStyle" media="screen">@import "/001/001.css";/*\*/css hack{}/**//*/*/css hack{}/**/css hack{display/**/:/**/none;display:none}</style><link
{}/*]]>*/</script><script type="text/javascript">i=1;</script><script type="text/javascript">/*<![CDATA[*/(i<1);/*]]>*/</script><!--[if IE 6]><style type="text/css">/*<![CDATA[*/
/* copyright: you'll need CDATA for this < & */
body{background:white}/*]]>*/</style><![endif]--><style type="text/css" title="currentStyle" media="screen">@import "/001/001.css";/*\*/css hack{}/**//*/*/css hack{}/**/css hack{display/**/:/**/none;display:none}</style><link
rel="Shortcut Icon"
type="image/x-icon"
href="http://www.csszengarden.com/favicon.ico" /><link

View File

@@ -72,8 +72,8 @@ function test_HTTP_Encoder()
$variedLength = strlen($variedContent);
$encodingTests = array(
array('method' => 'deflate', 'exp' => 32156)
,array('method' => 'gzip', 'exp' => 32174)
array('method' => 'deflate', 'exp' => 32157)
,array('method' => 'gzip', 'exp' => 32175)
,array('method' => 'compress', 'exp' => 32210)
);
@@ -86,7 +86,7 @@ function test_HTTP_Encoder()
$ret = strlen($e->getContent());
$desc = "HTTP_Encoder : {$test['method']} -> "
. sprintf('%4.2f%%', $ret/$variedLength*100);
. sprintf('%d(%4.2f%%)', $ret, $ret/$variedLength*100);
$passed = assertTrue($ret == $test['exp'], $desc);