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

PHP tag inside a pair of quotes in script tag under enabling minifyJS #679

Closed
desket opened this issue Jun 27, 2016 · 4 comments
Closed

Comments

@desket
Copy link

desket commented Jun 27, 2016

I found a bug when putting PHP tag <?php ?> inside a pair of single/double quotes in script tag <script>

Before compressed:

<script>
$('#element').myPlugin({
    key: '<?php ?>'
});
</script>
<script>
$('#element').myPlugin({
    key: "<?php ?>"
});
</script>

Actual (after compressed):

<script>$("#element").myPlugin({key:"\t\t"})</script>
<script>$("#element").myPlugin({key:"\t\t"})</script>

Expected:

<script>$("#element").myPlugin({key:""})</script>
<script>$("#element").myPlugin({key:""})</script>

I tried adding some regex in ignoreCustomFragments but it doesn't help.

I guess these lines may be related to my issue: 0960482

Or would there be any may that by-pass/escape this issue?

Thank you!

@outaTiME
Copy link

outaTiME commented Jun 28, 2016

Same here bro, im using:

  const htmlminOptions = {
    collapseWhitespace: true,
    // custom
    removeComments: true,
    minifyJS: true,
    ignoreCustomFragments: [ /<(WC@[\s\S]*?)>(.*?)<\/\1>/ ]
  };

on windows platform im getting:

<script type="text/javascript">var device_id="\t<WC@GPSID></WC@GPSID>\t"</script>

but if i remove the minifyJS flag:

<script type="text/javascript">var device_id = '<WC@GPSID></WC@GPSID>';</script>

@outaTiME
Copy link

outaTiME commented Jun 28, 2016

The problem comes with minifyJS flag on windows platform, how to solve this ?

@LeoLei
Copy link

LeoLei commented Jun 28, 2016

It's the same to me!

node express with ejs, before compressed

<script>
    var isLogin = "<%= is_login  %>";
</script>

result in:
<script>var isLogin="\t<%= is_login %>\t";</script>

This will make logic error. And also, if i remove /<%[\s\S]*?%>/ from ignoreCustomFragments list, it leads to an error, thrown by HTMLParser in src/htmlparser.js :

232       parseEndTag('</' + stackedTag + '>', stackedTag);
233     }
234
235     if (html === last) {
236       throw new Error('Parse Error: ' + html);
237     }

I found the code in the file of src/htmlminifier.js

 793   var customFragments = (options.ignoreCustomFragments || [
 794     /<%[\s\S]*?%>/,
 795     /<\?[\s\S]*?\?>/
 796   ]).map(function(re) {
 797     return re.source;
 798   });
 799   if (customFragments.length) {
 800     var reCustomIgnore = new RegExp('\\s*(?:' + customFragments.join('|') + ')+\\s*', 'g');
 801     // temporarily replace custom ignored fragments with unique attributes
 802     value = value.replace(reCustomIgnore, function(match) {
 803       if (!uidAttr) {
 804         uidAttr = uniqueId(value);
 805       }
 806       var token = uidAttr + ignoredCustomMarkupChunks.length;
 807       ignoredCustomMarkupChunks.push(match);
 808       return '\t' + token + '\t';
 809     });
 810   }

In the line of 808, the two '\t's are added. Removing them both, the compression is going correctly! I'm not sure what's them adding for.

@outaTiME
Copy link

But it happens only in windows platform ... in mac it works fine ...

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

No branches or pull requests

3 participants