Magento2: [2.1.0] HTML minification problem with php tag with a comment and no space at the end

Created on 28 Jun 2016  路  26Comments  路  Source: magento/magento2

Steps to reproduce

  1. Install Magento 2.1.0 using composer
  2. Create a theme and a template file which contains this:
<?php //if ($showDescription):?>
    <div class="product description product-item-description">
        <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
    </div>
<?php //endif; ?>
  1. Turn on html minification in the backend

    Expected result

  2. Page is rendered properly without errors

    Actual result

  3. Error message appears: Parse error: syntax error, unexpected '<'.

The minified php/html file was turned into:

<?php <div class="product description product-item-description"><?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?></div><?php ?>

Temporary solution

Add a space at the end and it works again, like this:

<?php //if ($showDescription): ?>
Frontend kiev-cd Fixed in 2.1.x Fixed in 2.2.x Fixed in 2.3.x Confirmed Format is not valid Ready for Work ISM eCompany Reproduced on 2.1.x Reproduced on 2.2.x Reproduced on 2.3.x bug report

All 26 comments

Any updates on this?

@guz-anton, @okorshenko, @piotrekkaminski: since you guys were involved in another html minification issue, I'm including you in this conversation.

Hi @hostep ,
We are going to find solution under MAGETWO-55356.

Experienced a similar issue when I installed https://github.com/algolia/algoliasearch-magento-2.

Except the problem line in my case was this line: https://github.com/algolia/algoliasearch-magento-2/blob/master/view/frontend/templates/internals/commonjs.phtml#L41

When html minification is turned on that line looks like
if ($path != '') { $path .= ' }

But I'd expect it to look like
if ($path != '') { $path .= ' /// '; }

Here's a gist of the entire file: https://gist.github.com/m0zziter/b7c2f00afd3684e0642060d19617ab2f

Same problem. 2.1.1.

This issue hasn't been closed yet and it doesn't seem like there has been a fix to the Minify HTML problem. @guz-anton @okorshenko @piotrekkaminski is there any update on if this issue has been fixed any of the 2.1 release? Thanks.

I'm using 2.1.6 and I'm having the same issues with html minimication

Does anyone has a solution?

Yes: add a space at the end, like I mention in my initial post under 'temporary solution' ;)

@hostep maybe that is the best way now,but my project is a secondary development, how can I find all of the End tag :'?>' and add a space... minification library is vendor/magento/framework/View/Template/Html/Minifier.php
right? can I rewrite it?

@hostep You have to be joking with your temporary solution. I just ran a regex to find all offending occurrencies in core phtml templates. It found 1109 cases within app/code/Magento.

@chenshanmin I guess it would be possible to try and do this with regular expressions search & replace.
I used the regex [^\s]\?> to search for any non-whitespace characters followed by a php closing tag. You could modify that to insert a space, for example with "sed" on bash.

@pantaoran: it's only for php tags with php code which is commented out in phtml files. I haven't searched the Magento core code if there are offending files like that, in our case it was a file in our custom theme.
I don't think your regex searches for commented out code?

Although I agree that all phtml files which contain {something}?> should be replaced with {something} ?> (including a space) to have everything consistent.

Apologies, I should have read it more carefully. Yes, my regex searches for any closing tags and does not consider the presence of comments, and it is therefore irrelevant here.

The following regex could work instead: \/\/.*[^\s]\?>
With this I found various occurrencies in third party modules and themes, but none in Magento core.

I repeated all php tags Ha ha ha

I used @pantaoran 's regex to find & fix a few template files, but the minified HTML is still broken.
Any other idea on how to debug this issue?

Any fix in Magento 2.2RC ?

@hostep, thank you for your report.
We've created internal ticket(s) MAGETWO-83074 to track progress on the issue.

I'm having a similar issue with version 2.1.9 and 2.2.2.

Magento mode: default

phtml code (with space at the end)

        </form>
    </div>
    <?php //echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('footer-links-block-4')->toHtml(); ?>
</div>

Error

Parse error: syntax error, unexpected '/'

Minify HTML in var/view_preprocessed/pub/...

 </form></div><?php </div>

But if I changed the code to

<?php //echo 'test comment'; ?>

NO error

Minify HTML in var/view_preprocessed/pub/...

</form></div><?php ?></div>

My workaround was to check the comment to /* ... */

<?php /* echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('footer-links-block-4')->toHtml(); */ ?>

When we have /// in the data:image/gif;base64, the minify html remove all the datas in the image.

Ex:
data:image/gif;base64,R0lGODlhEAAQAPQAAP///wAAAPDw8IqKiuDg4EZGRnp6egAAAFhYWCQkJKysrL6+vhQUFJycnAQEBDY2NmhoaAAAAAAAAA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kjIQAh+QQJCgAAACwAAAAAEAAQAAAFeSAgAgIpnOSoLgxxvqgKLEcCC65KEAByKK8cSpA4DAiHQ/DkKhGKh4ZCtCyZGo6F6iYYPAqFgYy02xkSaLEMV34tELyRYNEsCQyHlvWkGCzsPgMCEAY7Cg04Uk48LAsDhRA8MVQPEF0GAgqYYwSRlycNcWskCkApIyEAOwAAAAAAAAAAAA==

Become : data:image/gif;base64,R0lGODlhEAAQAPQAAP

We can exclude this case in the minify

Hi folks, any update on this? will this be fixed in 2.3 ?

I am working on this at #dmcdindia

@rahul-kachhadiya thank you for joining. Please accept team invitation here and self-assign the issue.

kiev-cd

Hi @hostep. Thank you for your report.
The issue has been fixed in magento/magento2#16332 by @lisovyievhenii in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.0 release.

Hi @hostep. Thank you for your report.
The issue has been fixed in magento/magento2#16917 by @ronak2ram in 2.1-develop branch
Related commit(s):

The fix will be available with the upcoming 2.1.15 release.

Hi @hostep. Thank you for your report.
The issue has been fixed in magento/magento2#16916 by @ronak2ram in 2.2-develop branch
Related commit(s):

The fix will be available with the upcoming 2.2.6 release.

Worked for me. Thanks @hostep

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tstamplis picture tstamplis  路  258Comments

sengaigibon picture sengaigibon  路  89Comments

Sharpy310 picture Sharpy310  路  87Comments

Krapulat picture Krapulat  路  119Comments

rbostan picture rbostan  路  105Comments