magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.47k stars 9.29k forks source link

Wouldn't it be better to use inline CSS instead of the 15 lines of internal CSS & JS? #36953

Open nurullah opened 1 year ago

nurullah commented 1 year ago

Summary

I always wondered about the logic of the internal CSS and JS code that come from the image_with_borders.phtml file when I saw the output code of the product list. I finally find a good time to review that the code and I realized that the code could be written simply.

I understand, inline CSS looks bad but I think 15 lines of internal CSS and JS code block looks worse than the 50 letters of inline CSS. Besides this code is in the loop. That means there will be 15 unnecessary lines for each product in the product list.

image

Examples

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php
/** @var $block \Magento\Catalog\Block\Product\Image */
/** @var $escaper \Magento\Framework\Escaper */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
$width = (int)$block->getWidth();
$paddingBottom = $block->getRatio() * 100;
?>
<span class="product-image-container product-image-container-<?= /* @noEscape */ $block->getProductId() ?>">
    <span class="product-image-wrapper">
        <img class="<?= $escaper->escapeHtmlAttr($block->getClass()) ?>"
            <?php foreach ($block->getCustomAttributes() as $name => $value): ?>
                <?= $escaper->escapeHtmlAttr($name) ?>="<?= $escaper->escapeHtml($value) ?>"
            <?php endforeach; ?>
            src="<?= $escaper->escapeUrl($block->getImageUrl()) ?>"
            loading="lazy"
            width="<?= $escaper->escapeHtmlAttr($block->getWidth()) ?>"
            height="<?= $escaper->escapeHtmlAttr($block->getHeight()) ?>"
            alt="<?= $escaper->escapeHtmlAttr($block->getLabel()) ?>"/></span>
</span>
<?php
$styles = <<<STYLE
.product-image-container-{$block->getProductId()} {
    width: {$width}px;
}
.product-image-container-{$block->getProductId()} span.product-image-wrapper {
    padding-bottom: {$paddingBottom}%;
}
STYLE;
//In case a script was using "style" attributes of these elements
$script = <<<SCRIPT
prodImageContainers = document.querySelectorAll(".product-image-container-{$block->getProductId()}");
for (var i = 0; i < prodImageContainers.length; i++) {
    prodImageContainers[i].style.width = "{$width}px";
}
prodImageContainersWrappers = document.querySelectorAll(
    ".product-image-container-{$block->getProductId()}  span.product-image-wrapper"
);
for (var i = 0; i < prodImageContainersWrappers.length; i++) {
    prodImageContainersWrappers[i].style.paddingBottom = "{$paddingBottom}%";
}
SCRIPT;

?>
<?= /* @noEscape */ $secureRenderer->renderTag('style', [], $styles, false) ?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', ['type' => 'text/javascript'], $script, false) ?>

Proposed solution

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php
/** @var $block \Magento\Catalog\Block\Product\Image */
/** @var $escaper \Magento\Framework\Escaper */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
$width = (int)$block->getWidth();
$paddingBottom = $block->getRatio() * 100;
?>
<span class="product-image-container product-image-container-<?= /* @noEscape */ $block->getProductId() ?>" style="width: <?= $width; ?>px">
    <span class="product-image-wrapper" style="padding-bottom: <?= $paddingBottom; ?>%">
        <img class="<?= $escaper->escapeHtmlAttr($block->getClass()) ?>"
            <?php foreach ($block->getCustomAttributes() as $name => $value): ?>
                <?= $escaper->escapeHtmlAttr($name) ?>="<?= $escaper->escapeHtml($value) ?>"
            <?php endforeach; ?>
            src="<?= $escaper->escapeUrl($block->getImageUrl()) ?>"
            loading="lazy"
            width="<?= $escaper->escapeHtmlAttr($block->getWidth()) ?>"
            height="<?= $escaper->escapeHtmlAttr($block->getHeight()) ?>"
            alt="<?= $escaper->escapeHtmlAttr($block->getLabel()) ?>"/>
    </span>
</span>

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @nurullah. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

m2-assistant[bot] commented 1 year ago

Hi @nurullah! :wave: Thank you for collaboration. Only members of Community Contributors Team are allowed to be assigned to the issue. Please use @magento add to contributors team command to join Contributors team.

nurullah commented 1 year ago

@magento add to contributors team

m2-assistant[bot] commented 1 year ago

Hi @nurullah! :wave: Thank you for joining. Please accept team invitation :point_right: here :point_left: and add your comment one more time.

nurullah commented 1 year ago

@magento give me 2.4-develop instance

m2-assistant[bot] commented 1 year ago

Hi @engcom-November. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-November commented 1 year ago

Hi @nurullah , Thank you for reporting and collaboration. Verified the behavior on Magento 2.4-develop instance and is reproducible. We are considering this as feature request as this can be a good enhancement / improvement. Thank you. image