mirror of
https://github.com/mrclay/minify.git
synced 2025-08-17 19:37:22 +02:00
+ preserveComments option to Minify_Javascript (default true), test case, and doc comments
This commit is contained in:
@@ -13,8 +13,62 @@ require dirname(__FILE__) . '/3rd_party/jsmin.php';
|
|||||||
* @author Stephen Clay <steve@mrclay.org>
|
* @author Stephen Clay <steve@mrclay.org>
|
||||||
*/
|
*/
|
||||||
class Minify_Javascript {
|
class Minify_Javascript {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minify a Javascript string
|
||||||
|
*
|
||||||
|
* @param string $js input javascript
|
||||||
|
*
|
||||||
|
* @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.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public static function minify($js, $options = array()) {
|
public static function minify($js, $options = array()) {
|
||||||
return trim(JSMin::minify($js));
|
if (isset($options['preserveComments'])
|
||||||
|
&& !$options['preserveComments']) {
|
||||||
|
return trim(JSMin::minify($js));
|
||||||
|
}
|
||||||
|
$ret = '';
|
||||||
|
while (1) {
|
||||||
|
list($beforeComment, $comment, $afterComment)
|
||||||
|
= self::_nextYuiComment($js);
|
||||||
|
$ret .= trim(JSMin::minify($beforeComment));
|
||||||
|
if (false === $comment) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$ret .= $comment;
|
||||||
|
$js = $afterComment;
|
||||||
|
}
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract comments that YUI Compressor preserves.
|
||||||
|
*
|
||||||
|
* @param string $js 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($js)
|
||||||
|
{
|
||||||
|
return (
|
||||||
|
(false !== ($start = strpos($js, '/*!')))
|
||||||
|
&& (false !== ($end = strpos($js, '*/')))
|
||||||
|
&& ($start < $end)
|
||||||
|
)
|
||||||
|
? array(
|
||||||
|
substr($js, 0, $start)
|
||||||
|
,"\n/*" . substr($js, $start + 3, $end - $start - 1) . "\n"
|
||||||
|
,substr($js, -(strlen($js) - $end - 2))
|
||||||
|
)
|
||||||
|
: array($js, false, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
// is.js
|
/*! is.js
|
||||||
|
|
||||||
// (c) 2001 Douglas Crockford
|
|
||||||
// 2001 June 3
|
|
||||||
|
|
||||||
|
(c) 2001 Douglas Crockford
|
||||||
|
2001 June 3
|
||||||
|
*/
|
||||||
|
|
||||||
// is
|
// is
|
||||||
|
|
||||||
@@ -21,6 +21,9 @@ var is = {
|
|||||||
parseFloat(navigator.appVersion),
|
parseFloat(navigator.appVersion),
|
||||||
win: navigator.platform == 'Win32'
|
win: navigator.platform == 'Win32'
|
||||||
}
|
}
|
||||||
|
/*!*
|
||||||
|
* preserve this comment, too
|
||||||
|
*/
|
||||||
is.mac = is.ua.indexOf('mac') >= 0;
|
is.mac = is.ua.indexOf('mac') >= 0;
|
||||||
if (is.ua.indexOf('opera') >= 0) {
|
if (is.ua.indexOf('opera') >= 0) {
|
||||||
is.ie = is.ns = false;
|
is.ie = is.ns = false;
|
||||||
|
9
web/test/_test_files/js/before.min.js
vendored
9
web/test/_test_files/js/before.min.js
vendored
@@ -1,3 +1,12 @@
|
|||||||
|
|
||||||
|
/* is.js
|
||||||
|
|
||||||
|
(c) 2001 Douglas Crockford
|
||||||
|
2001 June 3
|
||||||
|
*/
|
||||||
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'}
|
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'}
|
||||||
|
/**
|
||||||
|
* preserve this comment, too
|
||||||
|
*/
|
||||||
is.mac=is.ua.indexOf('mac')>=0;if(is.ua.indexOf('opera')>=0){is.ie=is.ns=false;is.opera=true;}
|
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;}
|
if(is.ua.indexOf('gecko')>=0){is.ie=is.ns=false;is.gecko=true;}
|
3
web/test/_test_files/js/before_noComments.min.js
vendored
Normal file
3
web/test/_test_files/js/before_noComments.min.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
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;}
|
@@ -8,7 +8,7 @@ function test_Javascript()
|
|||||||
global $thisDir;
|
global $thisDir;
|
||||||
|
|
||||||
$src = file_get_contents($thisDir . '/_test_files/js/before.js');
|
$src = file_get_contents($thisDir . '/_test_files/js/before.js');
|
||||||
$minExpected = file_get_contents($thisDir . '/_test_files/js/before.min.js');;
|
$minExpected = file_get_contents($thisDir . '/_test_files/js/before.min.js');
|
||||||
$minOutput = Minify_Javascript::minify($src);
|
$minOutput = Minify_Javascript::minify($src);
|
||||||
|
|
||||||
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
|
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
|
||||||
@@ -18,6 +18,20 @@ function test_Javascript()
|
|||||||
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
||||||
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//$src = file_get_contents($thisDir . '/_test_files/js/before.js');
|
||||||
|
$minExpected = file_get_contents($thisDir . '/_test_files/js/before_noComments.min.js');
|
||||||
|
$minOutput = Minify_Javascript::minify($src, array(
|
||||||
|
'preserveComments' => false
|
||||||
|
));
|
||||||
|
|
||||||
|
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
|
||||||
|
|
||||||
|
if (__FILE__ === $_SERVER['SCRIPT_FILENAME']) {
|
||||||
|
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
|
||||||
|
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
||||||
|
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test_Javascript();
|
test_Javascript();
|
||||||
|
Reference in New Issue
Block a user