Skip to content

Commit f75dcdb

Browse files
committed
MC-15022: HTML Content type does not render self closing html tags on storefront
Add comments and refactor
1 parent 8d27320 commit f75dcdb

6 files changed

+20
-12
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,28 @@ private function decodeHtmlContentTypes(\DOMDocument $document): void
143143
// Store a decoded attribute on the element so we don't double decode
144144
$htmlContentTypeNode->setAttribute('data-decoded', 'true');
145145

146+
// if nothing exists inside the node, continue
146147
if (!strlen(trim($htmlContentTypeNode->nodeValue))) {
147148
continue;
148149
}
149150

151+
// clone node and empty node value to prevent side-effects of modifying iterables in loop
150152
$clonedHtmlContentTypeNode = clone $htmlContentTypeNode;
151153
$clonedHtmlContentTypeNode->nodeValue = '';
152154

153155
foreach ($htmlContentTypeNode->childNodes as $childNode) {
156+
// if child node is not text node type, it does not need to be decoded; just append to cloned html node
154157
if ($childNode->nodeType !== XML_TEXT_NODE) {
155158
$clonedHtmlContentTypeNode->appendChild($childNode);
156159
continue;
157160
}
158161

159-
$fragDoc = new \DOMDocument();
160-
$result = $fragDoc->loadHTML('<fragment>' . $childNode->nodeValue . '</fragment>');
162+
// Load node value into dom document in an attempt to decode any encoded html within
163+
$fragDoc = $this->createDomDocument($childNode->nodeValue);
161164

162-
if (!$result) {
163-
continue;
164-
}
165+
$import = $fragDoc->getElementsByTagName('body')->item(0);
165166

166-
$import = $fragDoc->getElementsByTagName('fragment')->item(0);
167+
// Loop through decoded child nodes and import into the original document
167168
foreach ($import->childNodes as $importedChildNode) {
168169
$importedNode = $document->importNode($importedChildNode, true);
169170

@@ -175,6 +176,7 @@ private function decodeHtmlContentTypes(\DOMDocument $document): void
175176
}
176177
}
177178

179+
// replace original html content type node with cloned node we've been performing decoding operating on
178180
$htmlContentTypeNode->parentNode->replaceChild($clonedHtmlContentTypeNode, $htmlContentTypeNode);
179181
}
180182
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
<div data-content-type="html" data-decoded="true"><img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
1+
<div data-content-type="html" data-decoded="true">
2+
<img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
23
</div>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
<div data-content-type="html" data-decoded="true"><img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
2-
<div data-content-type="html" data-decoded="true"><img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
1+
<div data-content-type="html" data-decoded="true">
2+
<img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
3+
<div data-content-type="html" data-decoded="true">
4+
<img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
35
</div></div>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
<div data-content-type="html" data-decoded="true"><img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
1+
<div data-content-type="html" data-decoded="true">
2+
<img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
23
<div>
34
<img src="http://example.com/block"></div></div>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
<div data-content-type="html" data-decoded="true"><img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
1+
<div data-content-type="html" data-decoded="true">
2+
<img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
23
Hello world
34
</div>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div data-content-type="text">
22
Hello there
3-
<div data-content-type="html" data-decoded="true"><img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
3+
<div data-content-type="html" data-decoded="true">
4+
<img src="http://example.com"><iframe width="560" height="315" src="https://www.youtube.com/embed/owsfdh4gxyc" frameborder="0" allowfullscreen></iframe>
45
</div>
56
</div>

0 commit comments

Comments
 (0)