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

Merge pull request #48 from glensc/script-clean-comments

add jsCleanComments option to Minify_HTML
This commit is contained in:
Steve Clay
2013-03-23 12:55:43 -07:00

View File

@@ -1,22 +1,26 @@
<?php <?php
/** /**
* Class Minify_HTML * Class Minify_HTML
* @package Minify * @package Minify
*/ */
/** /**
* Compress HTML * Compress HTML
* *
* This is a heavy regex-based removal of whitespace, unnecessary comments and * This is a heavy regex-based removal of whitespace, unnecessary comments and
* tokens. IE conditional comments are preserved. There are also options to have * tokens. IE conditional comments are preserved. There are also options to have
* STYLE and SCRIPT blocks compressed by callback functions. * STYLE and SCRIPT blocks compressed by callback functions.
* *
* A test suite is available. * A test suite is available.
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
class Minify_HTML { class Minify_HTML {
/**
* @var boolean
*/
protected $_jsCleanComments = true;
/** /**
* "Minify" an HTML page * "Minify" an HTML page
@@ -27,21 +31,21 @@ class Minify_HTML {
* *
* 'cssMinifier' : (optional) callback function to process content of STYLE * 'cssMinifier' : (optional) callback function to process content of STYLE
* elements. * elements.
* *
* 'jsMinifier' : (optional) callback function to process content of SCRIPT * 'jsMinifier' : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored. * elements. Note: the type attribute is ignored.
* *
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype. * unset, minify will sniff for an XHTML doctype.
* *
* @return string * @return string
*/ */
public static function minify($html, $options = array()) { public static function minify($html, $options = array()) {
$min = new Minify_HTML($html, $options); $min = new self($html, $options);
return $min->process(); return $min->process();
} }
/** /**
* Create a minifier object * Create a minifier object
* *
@@ -51,13 +55,15 @@ class Minify_HTML {
* *
* 'cssMinifier' : (optional) callback function to process content of STYLE * 'cssMinifier' : (optional) callback function to process content of STYLE
* elements. * elements.
* *
* 'jsMinifier' : (optional) callback function to process content of SCRIPT * 'jsMinifier' : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored. * elements. Note: the type attribute is ignored.
* *
* 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block
*
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype. * unset, minify will sniff for an XHTML doctype.
* *
* @return null * @return null
*/ */
public function __construct($html, $options = array()) public function __construct($html, $options = array())
@@ -72,9 +78,12 @@ class Minify_HTML {
if (isset($options['jsMinifier'])) { if (isset($options['jsMinifier'])) {
$this->_jsMinifier = $options['jsMinifier']; $this->_jsMinifier = $options['jsMinifier'];
} }
if (isset($options['jsCleanComments'])) {
$this->_jsCleanComments = (bool)$options['jsCleanComments'];
}
} }
/** /**
* Minify the markeup given in the constructor * Minify the markeup given in the constructor
* *
@@ -213,17 +222,19 @@ class Minify_HTML {
// whitespace surrounding? preserve at least one space // whitespace surrounding? preserve at least one space
$ws1 = ($m[1] === '') ? '' : ' '; $ws1 = ($m[1] === '') ? '' : ' ';
$ws2 = ($m[4] === '') ? '' : ' '; $ws2 = ($m[4] === '') ? '' : ' ';
// remove HTML comments (and ending "//" if present) // remove HTML comments (and ending "//" if present)
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js); if ($this->_jsCleanComments) {
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
}
// remove CDATA section markers // remove CDATA section markers
$js = $this->_removeCdata($js); $js = $this->_removeCdata($js);
// minify // minify
$minifier = $this->_jsMinifier $minifier = $this->_jsMinifier
? $this->_jsMinifier ? $this->_jsMinifier
: 'trim'; : 'trim';
$js = call_user_func($minifier, $js); $js = call_user_func($minifier, $js);
return $this->_reservePlace($this->_needsCdata($js) return $this->_reservePlace($this->_needsCdata($js)