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

Minify_HTML: more efficient removal of ws outside tags, and possible fix to Issue 46

test/test_HTML.php: + timing when called directly
This commit is contained in:
Steve Clay
2008-08-13 14:00:11 +00:00
parent 961e04ca30
commit d2746cb43c
2 changed files with 16 additions and 8 deletions

View File

@@ -71,16 +71,17 @@ class Minify_HTML {
$html = preg_replace('/^\\s+|\\s+$/m', '', $html); $html = preg_replace('/^\\s+|\\s+$/m', '', $html);
// remove ws around block/undisplayed elements // remove ws around block/undisplayed elements
$html = preg_replace('/\\s*(<\\/?(?:area|base(?:font)?|blockquote|body' $html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
.'|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form' .'|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form'
.'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta' .'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta'
.'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)' .'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)'
.'|ul)\\b[^>]*>)/i', '$1', $html); .'|ul)\\b[^>]*>)/i', '$1', $html);
// remove ws between and inside elements. // remove ws outside of all elements
$html = preg_replace('/>\\s+(\\S[\\s\\S]*?)?</', "> $1<", $html); $html = preg_replace_callback(
$html = preg_replace('/>(\\S[\\s\\S]*?)?\\s+</', ">$1 <", $html); '/>([^<]+)</'
$html = preg_replace('/>\\s+</', "> <", $html); ,array('Minify_HTML', '_outsideTagCB')
,$html);
// fill placeholders // fill placeholders
self::_fillPlaceholders($html, self::$_pres, 'PRE'); self::_fillPlaceholders($html, self::$_pres, 'PRE');
@@ -113,6 +114,11 @@ class Minify_HTML {
protected static $_cssMinifier = null; protected static $_cssMinifier = null;
protected static $_jsMinifier = null; protected static $_jsMinifier = null;
protected static function _outsideTagCB($m)
{
return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
}
protected static function _removePreCB($m) protected static function _removePreCB($m)
{ {
self::$_pres[] = $m[1]; self::$_pres[] = $m[1];

View File

@@ -12,17 +12,19 @@ function test_HTML()
$src = file_get_contents($thisDir . '/_test_files/html/before.html'); $src = file_get_contents($thisDir . '/_test_files/html/before.html');
$minExpected = file_get_contents($thisDir . '/_test_files/html/before.min.html'); $minExpected = file_get_contents($thisDir . '/_test_files/html/before.min.html');
$time = microtime(true);
$minOutput = Minify_HTML::minify($src, array( $minOutput = Minify_HTML::minify($src, array(
'cssMinifier' => array('Minify_CSS', 'minify') 'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', 'minify') ,'jsMinifier' => array('Minify_Javascript', 'minify')
)); ));
$time = microtime(true) - $time;
$passed = assertTrue($minExpected === $minOutput, 'Minify_HTML'); $passed = assertTrue($minExpected === $minOutput, 'Minify_HTML');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n"; echo "\n---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n"
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n"; , "---Expected: ", strlen($minExpected), " bytes\n\n{$minExpected}\n\n"
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n"; , "---Source: ", strlen($src), " bytes\n\n{$src}\n\n\n";
} }
} }