1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-08 23:26:43 +02:00

Minify_HTML: simplified placeholder logic

This commit is contained in:
Steve Clay
2008-08-17 17:41:13 +00:00
parent 2b8b1a5d25
commit 711fbeb365

View File

@@ -39,6 +39,7 @@ class Minify_HTML {
self::$_isXhtml = (false !== strpos($html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML')); self::$_isXhtml = (false !== strpos($html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
self::$_replacementHash = 'MINIFYHTML' . md5(time()); self::$_replacementHash = 'MINIFYHTML' . md5(time());
self::$_placeholders = array();
// replace SCRIPTs (and minify) with placeholders // replace SCRIPTs (and minify) with placeholders
$html = preg_replace_callback( $html = preg_replace_callback(
@@ -82,38 +83,32 @@ class Minify_HTML {
'/>([^<]+)</' '/>([^<]+)</'
,array('Minify_HTML', '_outsideTagCB') ,array('Minify_HTML', '_outsideTagCB')
,$html); ,$html);
// fill placeholders // fill placeholders
self::_fillPlaceholders($html, self::$_pres, 'PRE'); $html = str_replace(
self::_fillPlaceholders($html, self::$_tas, 'TEXTAREA'); array_keys(self::$_placeholders)
self::_fillPlaceholders($html, self::$_scripts, 'SCRIPT'); ,array_values(self::$_placeholders)
self::_fillPlaceholders($html, self::$_styles, 'STYLE'); ,$html
);
self::$_placeholders = array();
// use newlines before 1st attribute in open tags (to limit line lengths) // use newlines before 1st attribute in open tags (to limit line lengths)
$html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $html); $html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $html);
self::$_cssMinifier = self::$_jsMinifier = null; self::$_cssMinifier = self::$_jsMinifier = null;
return $html; return $html;
} }
protected static function _fillPlaceholders(&$html, &$placeholderArray, $id) protected static function _reservePlace($content)
{ {
$i = count($placeholderArray); $placeholder = '%' . self::$_replacementHash . count(self::$_placeholders) . '%';
while ($i) { self::$_placeholders[$placeholder] = $content;
$html = str_replace( return $placeholder;
self::$_replacementHash . $id . $i
,array_pop($placeholderArray)
,$html);
$i--;
}
} }
protected static $_isXhtml = false; protected static $_isXhtml = false;
protected static $_replacementHash = null; protected static $_replacementHash = null;
protected static $_pres = array(); protected static $_placeholders = array();
protected static $_tas = array(); // textareas
protected static $_scripts = array();
protected static $_styles = array();
protected static $_cssMinifier = null; protected static $_cssMinifier = null;
protected static $_jsMinifier = null; protected static $_jsMinifier = null;
@@ -124,14 +119,12 @@ class Minify_HTML {
protected static function _removePreCB($m) protected static function _removePreCB($m)
{ {
self::$_pres[] = $m[1]; return self::_reservePlace($m[1]);
return self::$_replacementHash . 'PRE' . count(self::$_pres);
} }
protected static function _removeTaCB($m) protected static function _removeTaCB($m)
{ {
self::$_tas[] = $m[1]; return self::_reservePlace($m[1]);
return self::$_replacementHash . 'TEXTAREA' . count(self::$_tas);
} }
protected static function _removeStyleCB($m) protected static function _removeStyleCB($m)
@@ -150,13 +143,10 @@ class Minify_HTML {
: 'trim'; : 'trim';
$css = call_user_func($minifier, $css); $css = call_user_func($minifier, $css);
// store return self::_reservePlace(self::_needsCdata($css)
self::$_styles[] = self::_needsCdata($css) ? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>" : "{$openStyle}{$css}</style>"
: "{$openStyle}{$css}</style>"; );
return self::$_replacementHash . 'STYLE' . count(self::$_styles);
} }
protected static function _removeScriptCB($m) protected static function _removeScriptCB($m)
@@ -176,11 +166,10 @@ class Minify_HTML {
: 'trim'; : 'trim';
$js = call_user_func($minifier, $js); $js = call_user_func($minifier, $js);
// store return self::_reservePlace(self::_needsCdata($js)
self::$_scripts[] = self::_needsCdata($js) ? "{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>"
? "{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>" : "{$openScript}{$js}</script>"
: "{$openScript}{$js}</script>"; );
return self::$_replacementHash . 'SCRIPT' . count(self::$_scripts);
} }
protected static function _removeCdata($str) protected static function _removeCdata($str)