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

Issue with Heredoc Syntax in .phtml Files When HTML Minification is Enabled #46

Open
underser opened this issue Sep 4, 2024 · 1 comment

Comments

@underser
Copy link

underser commented Sep 4, 2024

Description

Overview

The use of Heredoc syntax in the Magento 2 Friendly Captcha extension's .phtml files (introduced in version 3.x) is causing syntax errors when Magento's HTML minification setting is enabled. This issue affects environments where the Magento configuration under Stores -> Configuration -> Advanced -> Developer -> Template Settings -> Minify Html is set to Yes. When this setting is enabled, Magento minifies .phtml files, which leads to improperly processed Heredoc syntax causing ParseError: syntax error, unexpected token <<.

Affected Files

Expected Behavior

The .phtml template files should render without syntax errors, regardless of whether HTML minification is enabled or disabled in Magento's backend.

Actual Behavior

When HTML minification is enabled in production mode, the rendered .phtml files with Heredoc syntax cause a PHP parse error due to the compression of the file into a single line or other alterations that disrupt the proper end marking of the Heredoc block.

Steps to Reproduce

  1. Configure and Enable extension at Stores -> Configuration -> Security -> Friendly Captcha
  2. Set Magento to production mode.
  3. Enable HTML minification at Stores -> Configuration -> Advanced -> Developer -> Template Settings -> Minify Html or execute ./bin/magento config:set dev/template/minify_html 1 command.
  4. Clear cache.
  5. Navigate to a page where the Friendly Captcha is supposed to load. For example to Login page.
  6. Observe the system.log and page crash due to a syntax error.

Error from system.log

[2024-09-04T08:32:28.642497+00:00] main.CRITICAL: ParseError: syntax error, unexpected token "<<" in /Users/user/Sites/main/project/src/var/view_preprocessed/pub/static/vendor/imi/magento2-friendly-captcha/view/frontend/templates/imi_friendly_captcha.phtml:1

Minified content of var/view_preprocessed/pub/static/vendor/imi/magento2-friendly-captcha/view/frontend/templates/imi_friendly_captcha.phtml

<?php /** * Copyright © iMi digital GmbH, based on work by MageSpecialist * See LICENSE for license details. */ use Magento\Framework\View\Helper\SecureHtmlRenderer; /** @var $block IMI\FriendlyCaptcha\Block\Frontend\FriendlyCaptcha */ /** @var $escaper \Magento\Framework\Escaper */ /** @var $secureRenderer SecureHtmlRenderer */ $endpoint = $block->getPuzzleEndpoint(); $widgetId = $block->getWidgetId(); ?> <div id="<?= $widgetId ?>" class="frc-captcha" data-sitekey="<?= $block->getSiteKey() ?>" <?php if ($endpoint !== ''): ?>data-puzzle-endpoint="<?= $escaper->escapeUrl($endpoint) ?>"<?php endif; ?> data-lang="<?= $block->getLang() ?>" data-callback="captchaSolved_<?= $widgetId ?>" ></div><?php $scriptString = <<<SCRIPT 'use strict'; document.addEventListener('DOMContentLoaded', function () { function setButtonDisabled(disabled) { const button = Array.from(document.getElementById('{$widgetId}').closest('form').elements).find(el => el.type === 'submit'); if (!button) { return; } button.disabled = disabled; } setButtonDisabled(true); window.captchaSolved_{$widgetId} = function () { setButtonDisabled(false); } }); SCRIPT ?> <?= /** noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>

@amenk
Copy link
Member

amenk commented Sep 4, 2024

Thanks @underser for the elaborate and clear report. That is interesting, I did not know that this causes issues.

Can you make a pull request with a fix?

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

2 participants