how to ignore type="text/html" in <script>
Opened this issue · 2 comments
GoogleCodeExporter commented
$option = array('cssMinifier' => array('Minify_CSS', 'minify'), 'jsMinifier' =>
array('JSMin', 'minify'));
echo Minify_HTML::minify($html, $option);
in html have some javascript template
eg:
...
<script id="liTpl" type="text/html">
<section class="detail_list">
....
</section>
</script>
..
how to ignore this section???
Original issue reported on code.google.com by szdcboy@gmail.com
on 12 Nov 2013 at 4:15
GoogleCodeExporter commented
Minify should examine the SCRIPT type attribute if present, and not process the
contents if it's not a common JavaScript MIME type.
Original comment by mrclay....@gmail.com
on 12 Nov 2013 at 5:40
- Changed state: Accepted
GoogleCodeExporter commented
Hello,
Here is a proposed patch for this issue
perhaps we can add some values for the "javascript types allowed", and let the
trim ?
in /lib/Minify/HTML.php, near Line 217, patched function _removeScriptCB() (i
just added the $typeJS boolean)
protected function _removeScriptCB($m)
{
$openScript = "<script{$m[2]}";
$js = $m[3];
// whitespace surrounding? preserve at least one space
$ws1 = ($m[1] === '') ? '' : ' ';
$ws2 = ($m[4] === '') ? '' : ' ';
// check if the script has a type attribute, and check it
$typeJS = true; // no type means JS
if (preg_match('/type=(\'|")?([^ \'">]+)(\'|")?/i', $m[2], $matches)) {
$typeJS = in_array(strtolower($matches[2]), array( "text/javascript", "application/javascript" ));
}
if ($typeJS) {
// remove HTML comments (and ending "//" if present)
if ($this->_jsCleanComments) {
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
}
// remove CDATA section markers
$js = $this->_removeCdata($js);
// minify
$minifier = $this->_jsMinifier
? $this->_jsMinifier
: 'trim';
$js = call_user_func($minifier, $js);
}
return $this->_reservePlace($this->_needsCdata($js)
? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
: "{$ws1}{$openScript}{$js}</script>{$ws2}"
);
}
works for me and handlebars templates like
<script type="text/x-handlebars-template"> ... </script>
Original comment by j...@vitalyn.com
on 30 Dec 2014 at 3:54