macray / htmlcompressor

Automatically exported from code.google.com/p/htmlcompressor
Apache License 2.0
0 stars 0 forks source link

PHP code in script block causes exception #56

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Grap attached success.php and failure.php
2.java -jar htmlcompressor-1.5.1.jar --remove-quotes --compress-css 
--preserve-php --compress-js -o ~/Desktop/output/ failure.php
3.java -jar htmlcompressor-1.5.1.jar --remove-quotes --compress-css 
--preserve-php --compress-js -o ~/Desktop/output/ success.php

What is the expected output? What do you see instead?
Step 2 fails with the following output: 
[ERROR] HtmlCompressor: "syntax error" at line [7:6] during JavaScript 
compression:     %%%~COMPRESS~USER0~5~%%%
[ERROR] HtmlCompressor: "Compilation produced 1 syntax errors." at line [1:0] 
during JavaScript compression
Exception in thread "main" org.mozilla.javascript.EvaluatorException: 
Compilation produced 1 syntax errors.
    at com.googlecode.htmlcompressor.compressor.YuiJavaScriptCompressor$DefaultErrorReporter.runtimeError(YuiJavaScriptCompressor.java:93)
    at org.mozilla.javascript.Parser.parse(Parser.java:392)
    at org.mozilla.javascript.Parser.parse(Parser.java:337)
    at com.yahoo.platform.yui.compressor.JavaScriptCompressor.parse(JavaScriptCompressor.java:312)
    at com.yahoo.platform.yui.compressor.JavaScriptCompressor.<init>(JavaScriptCompressor.java:533)
    at com.googlecode.htmlcompressor.compressor.YuiJavaScriptCompressor.compress(YuiJavaScriptCompressor.java:53)
    at com.googlecode.htmlcompressor.compressor.HtmlCompressor.compressJavaScript(HtmlCompressor.java:894)
    at com.googlecode.htmlcompressor.compressor.HtmlCompressor.processScriptBlocks(HtmlCompressor.java:829)
    at com.googlecode.htmlcompressor.compressor.HtmlCompressor.processPreservedBlocks(HtmlCompressor.java:719)
    at com.googlecode.htmlcompressor.compressor.HtmlCompressor.compress(HtmlCompressor.java:197)
    at com.googlecode.htmlcompressor.CmdLineCompressor.init(CmdLineCompressor.java:161)
    at com.googlecode.htmlcompressor.CmdLineCompressor.main(CmdLineCompressor.java:105)

Step 3 is successful. The difference in the files is a php code segment in the 
final <script> tag. 
It is present in Step 2 and removed for step 3. The offending php is below.
<?php if($showMobile){ echo "$(function(){doMobileInit();});";}?>

What version of the product are you using? On what operating system?
htmlcompressor-1.5.1.jar on Ubuntu 11.04

Also occurred with htmlcompressor-1.4.3.jar packaged with HTML5 Boilerplate

Please provide any additional information below.

Original issue reported on code.google.com by jd200...@gmail.com on 15 Sep 2011 at 9:43

Attachments:

GoogleCodeExporter commented 9 years ago
This is a known issue, I probably need to warn about it in the documentation. 

I don't think I will be able to come up with a solution for this. 

When it preserves PHP blocks (or any other custom blocks) it replaces them with 
%%%~COMPRESS~USERX~X~%%% palceholder, which is incorrect javascript. I can 
change placeholder to something like /*%%%~COMPRESS~USERX~X~%%%*/, but it still 
could cause errors in many places:

var i = /*...*/; //syntax error

The only solution would be to not compress inline js if it is mixed with php 
blocks. You can skip this single piece of javascript if you wrap it with <!-- 
{{{ -->...<!-- }}} --> skip block.

Original comment by serg472@gmail.com on 15 Sep 2011 at 3:13

GoogleCodeExporter commented 9 years ago
Ahhh, fair enough.  Sorry for dragging it up. I had seen a similar issue from 
July so wasn't sure if it was an edge case that was missed or something.

That skip block solution will do me perfectly now I know what to look out for. 
Thanks for the responding so quickly and for this great package.

Original comment by jd200...@gmail.com on 15 Sep 2011 at 3:20

GoogleCodeExporter commented 9 years ago
Glad that you like the project.

Thanks :)

Original comment by serg472@gmail.com on 15 Sep 2011 at 3:34