zhanjh / minify

Automatically exported from code.google.com/p/minify
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

how to ignore type="text/html" in <script> #302

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

$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 9 years ago
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

GoogleCodeExporter commented 9 years ago
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