diff --git a/lib/Minify/HTML.php b/lib/Minify/HTML.php
index df6be07..89cbaf0 100644
--- a/lib/Minify/HTML.php
+++ b/lib/Minify/HTML.php
@@ -7,11 +7,11 @@
/**
* Compress HTML
*
- * This is a heavy regex-based removal of whitespace, unnecessary
- * comments and tokens.
+ * This is a heavy regex-based removal of whitespace, unnecessary comments and
+ * tokens. IE conditional comments are preserved. There are also options to have
+ * STYLE and SCRIPT blocks compressed by callback functions.
*
- * IE conditional comments are preserved. There are also options to have STYLE
- * and SCRIPT blocks compressed by callback functions. A test suite is available.
+ * A test suite is available.
*
* @package Minify
* @author Stephen Clay
@@ -21,10 +21,11 @@ class Minify_HTML {
/**
* "Minify" an HTML page
*
- * @todo: To also minify embedded Javascript/CSS, you must...
- *
+ * @param string $html
+ * @param array $options
+ * @return string
*/
- public static function minify($string, $options = array()) {
+ public static function minify($html, $options = array()) {
if (isset($options['cssMinifier'])) {
self::$_cssMinifier = $options['cssMinifier'];
@@ -33,32 +34,35 @@ class Minify_HTML {
self::$_jsMinifier = $options['jsMinifier'];
}
- $html = trim($string);
+ $html = trim($html);
self::$_isXhtml = (false !== strpos($html, '$1 <", $html);
$html = preg_replace('/>\\s+', "> <", $html);
- // replace PREs
- $i = count(self::$_pres);
- while ($i > 0) {
- $rep = array_pop(self::$_pres);
- $html = str_replace(self::$_replacementHash . 'PRE' . $i, $rep, $html);
- $i--;
- }
-
- // replace TEXTAREAs
- $i = count(self::$_tas);
- while ($i > 0) {
- $rep = array_pop(self::$_tas);
- $html = str_replace(self::$_replacementHash . 'TEXTAREA' . $i, $rep, $html);
- $i--;
- }
-
- // replace SCRIPTs
- $i = count(self::$_scripts);
- while ($i > 0) {
- $rep = array_pop(self::$_scripts);
- $html = str_replace(self::$_replacementHash . 'SCRIPT' . $i, $rep, $html);
- $i--;
- }
-
- // replace STYLEs
- $i = count(self::$_styles);
- while ($i > 0) {
- $rep = array_pop(self::$_styles);
- $html = str_replace(self::$_replacementHash . 'STYLE' . $i, $rep, $html);
- $i--;
- }
+ // fill placeholders
+ self::_fillPlaceholders($html, self::$_pres, 'PRE');
+ self::_fillPlaceholders($html, self::$_tas, 'TEXTAREA');
+ self::_fillPlaceholders($html, self::$_scripts, 'SCRIPT');
+ self::_fillPlaceholders($html, self::$_styles, 'STYLE');
self::$_cssMinifier = self::$_jsMinifier = null;
return $html;
}
+
+ protected static function _fillPlaceholders(&$html, &$placeholderArray, $id)
+ {
+ $i = count($placeholderArray);
+ while ($i) {
+ $html = str_replace(
+ self::$_replacementHash . $id . $i
+ ,array_pop($placeholderArray)
+ ,$html);
+ $i--;
+ }
+ }
protected static $_isXhtml = false;
protected static $_replacementHash = null;
@@ -196,4 +186,3 @@ class Minify_HTML {
return (self::$_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
}
}
-