minify icon indicating copy to clipboard operation
minify copied to clipboard

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

Open mrclay opened this issue 9 years ago • 7 comments

Originally reported on Google Code with ID 302


$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???

Reported by szdcboy on 2013-11-12 16:15:38

mrclay avatar Sep 04 '15 12:09 mrclay

Minify should examine the SCRIPT type attribute if present, and not process the contents
if it's not a common JavaScript MIME type.

Reported by mrclay.org on 2013-11-12 17:40:26

  • Status changed: Accepted

mrclay avatar Sep 04 '15 12:09 mrclay

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>

Reported by [email protected] on 2014-12-30 15:54:35

mrclay avatar Sep 04 '15 12:09 mrclay

I'm having a similar problem with schema script tag "application/ld+json", the minify just wipe out the entire JSON inside the tag, how should I proceed?

rafaelmoni avatar Aug 11 '16 16:08 rafaelmoni

You could try to fix it. I haven't seriously worked on this in years.

mrclay avatar Aug 29 '16 22:08 mrclay

Thanks for this patch Steve, but this doesn't work out of the box. Need to do this on the return:

return $this->_reservePlace($typeJS && $this->_needsCdata($js)

Otherwise it reinserts the CDATA for "text/template"

scott-thrillist avatar May 05 '17 15:05 scott-thrillist

If it works for you, how about sending a PR?

mrclay avatar May 06 '17 12:05 mrclay

Made here: https://github.com/mrclay/minify/pull/598

scott-thrillist avatar May 09 '17 17:05 scott-thrillist