Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mrclay opened this issue Sep 4, 2015 · 7 comments
Open

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

mrclay opened this issue Sep 4, 2015 · 7 comments

Comments

@mrclay
Copy link
Owner

mrclay commented Sep 4, 2015

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
Copy link
Owner Author

mrclay commented Sep 4, 2015

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
Copy link
Owner Author

mrclay commented Sep 4, 2015

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 jc@vitalyn.com on 2014-12-30 15:54:35

@rafaelmoni
Copy link

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?

@mrclay
Copy link
Owner Author

mrclay commented Aug 29, 2016

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

@scott-thrillist
Copy link

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"

@mrclay
Copy link
Owner Author

mrclay commented May 6, 2017

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

@scott-thrillist
Copy link

Made here: #598

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants