Skip to content

Commit c9091d0

Browse files
committed
MC-15022: HTML Content type does not render self closing html tags on storefront
Use Random class to generate unique a-z string and update xpath to fetch top-level ancestor html content type nodes only
1 parent 60d8c8a commit c9091d0

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

app/code/Magento/PageBuilder/Plugin/Filter/TemplatePlugin.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@ class TemplatePlugin
2929
*/
3030
private $domDocument;
3131

32+
/**
33+
* @var \Magento\Framework\Math\Random
34+
*/
35+
private $mathRandom;
36+
3237
/**
3338
* @param \Psr\Log\LoggerInterface $logger
3439
* @param \Magento\Framework\View\ConfigInterface $viewConfig
40+
* @param \Magento\Framework\Math\Random $mathRandom
3541
*/
3642
public function __construct(
3743
\Psr\Log\LoggerInterface $logger,
38-
\Magento\Framework\View\ConfigInterface $viewConfig
44+
\Magento\Framework\View\ConfigInterface $viewConfig,
45+
\Magento\Framework\Math\Random $mathRandom
3946
) {
4047
$this->logger = $logger;
4148
$this->viewConfig = $viewConfig;
49+
$this->mathRandom = $mathRandom;
4250
}
4351

4452
/**
@@ -150,16 +158,17 @@ private function convertEncodedHtmlContentTypesToPlaceholders(
150158
): void {
151159
$xpath = new \DOMXPath($document);
152160

161+
// construct xpath query to fetch top-level ancestor html content type nodes
153162
/** @var $htmlContentTypeNodes \DOMNode[] */
154-
$htmlContentTypeNodes = $xpath->query('//*[@data-content-type="html" and not(@data-decoded="true")]');
163+
$htmlContentTypeNodes = $xpath->query(
164+
'//*[@data-content-type="html" and not(@data-decoded="true")]' .
165+
'[not(ancestor::*[@data-content-type="html"])]'
166+
);
155167

156-
// Preliminarily set decoded attribute on all encoded html content types so we don't double decode;
157-
// this needs to be done in a separate loop as contents will change throughout the subsequent loop
158168
foreach ($htmlContentTypeNodes as $htmlContentTypeNode) {
169+
// Set decoded attribute on all encoded html content types so we don't double decode;
159170
$htmlContentTypeNode->setAttribute('data-decoded', 'true');
160-
}
161171

162-
foreach ($htmlContentTypeNodes as $htmlContentTypeNode) {
163172
// if nothing exists inside the node, continue
164173
if (!strlen(trim($htmlContentTypeNode->nodeValue))) {
165174
continue;
@@ -171,7 +180,7 @@ private function convertEncodedHtmlContentTypesToPlaceholders(
171180
// generate unique node name element to replace with decoded html contents at end of processing;
172181
// goal is to create a document as few times as possible to prevent inadvertent parsing of contents as html
173182
// by the dom library
174-
$uniqueNodeName = 'a' . md5(uniqid('', true));
183+
$uniqueNodeName = $this->mathRandom->getRandomString(32, $this->mathRandom::CHARS_LOWERS);
175184

176185
$uniqueNode = new \DOMElement($uniqueNodeName);
177186
$htmlContentTypeNode->parentNode->replaceChild($uniqueNode, $htmlContentTypeNode);
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<div data-content-type="html">
22
&lt;img src=&quot;http://example.com&quot;&gt;
33
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/owsfdh4gxyc&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;
4+
&amp;amp;
45
</div>
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<div data-content-type="html" data-decoded="true">
22
<img src="http://example.com">
33
<iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
4-
<div data-content-type="html" data-decoded="true">
4+
<div data-content-type="html">
55
<img src="http://example.com">
66
<iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
7+
&amp;
78
</div>
89
</div>

dev/tests/integration/testsuite/Magento/PageBuilder/_files/template_plugin/text_content_type_with_block_with_html_content_type_inside_post_filter.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<div data-content-type="html" data-decoded="true">
44
<img src="http://example.com">
55
<iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
6+
&amp;
67
</div>
78
</div>

0 commit comments

Comments
 (0)