Skip to content

Commit 9a0d50a

Browse files
committed
Merge branch 'ACP2E-3326' of https://github.com/adobe-commerce-tier-4/magento2-page-builder into PR-10-01-2024
2 parents 1969544 + effacba commit 9a0d50a

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

app/code/Magento/PageBuilder/Model/Stage/HtmlFilter.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,43 @@ public function filterHtml(string $content): string
7878
);
7979
foreach ($htmlContentTypes as $htmlContentType) {
8080
/* @var \DOMElement $htmlContentType */
81-
$innerHTML = '';
82-
$children = $htmlContentType->childNodes;
83-
foreach ($children as $child) {
84-
$innerHTML .= $child->ownerDocument->saveXML($child);
85-
}
8681
$htmlContentType->setAttribute(
8782
"class",
8883
$htmlContentType->getAttribute("class") . " placeholder-html-code"
8984
);
85+
86+
$innerHTML = $this->getChildrenInnerHtml($htmlContentType);
87+
9088
$htmlContentType->nodeValue = htmlentities($innerHTML);
9189
}
9290
return substr(trim($dom->saveHTML()), 5, -6);
9391
}
92+
93+
/**
94+
* Get inner HTML of element's children
95+
*
96+
* @param \DOMElement $element
97+
* @return string
98+
*/
99+
private function getChildrenInnerHtml(\DOMElement $element): string
100+
{
101+
$innerHTML = '';
102+
$childrenIterator = $element->childNodes->getIterator();
103+
while ($childrenIterator->valid()) {
104+
$child = $childrenIterator->current();
105+
try {
106+
$ownerDocument = $child->ownerDocument;
107+
} catch (\Error $error) {
108+
$ownerDocument = null;
109+
$this->loggerInterface->critical($error->getMessage());
110+
}
111+
if ($ownerDocument === null) {
112+
$childrenIterator->next();
113+
continue;
114+
}
115+
$innerHTML .= $ownerDocument->saveXML($child);
116+
$childrenIterator->next();
117+
}
118+
return $innerHTML;
119+
}
94120
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Test\Unit\Model\Stage;
9+
10+
use Magento\PageBuilder\Model\Stage\HtmlFilter;
11+
use PHPUnit\Framework\MockObject\MockObject;
12+
use PHPUnit\Framework\TestCase;
13+
use Psr\Log\LoggerInterface;
14+
15+
class HtmlFilterTest extends TestCase
16+
{
17+
/**
18+
* @var LoggerInterface|MockObject
19+
*/
20+
private $loggerMock;
21+
22+
/**
23+
* @var HtmlFilter
24+
*/
25+
private $htmlFilter;
26+
27+
protected function setUp(): void
28+
{
29+
$this->loggerMock = $this->createMock(LoggerInterface::class);
30+
$this->htmlFilter = new HtmlFilter($this->loggerMock);
31+
}
32+
33+
public function testFilterHtml()
34+
{
35+
//test for script tag
36+
$inputHtml = '<div><script type="text/x-magento-init">alert("test")</script><p>Content</p></div>';
37+
$expectedOutput = '<div><p>Content</p></div>';
38+
39+
$result = $this->htmlFilter->filterHtml($inputHtml);
40+
$this->assertEquals($expectedOutput, $result);
41+
42+
//test for PB placeholder
43+
$inputHtml = '
44+
<div data-content-type="html" data-appearance="default" data-element="main" class="test">
45+
<div class="block-123">Test</div>
46+
</div>';
47+
$expectedOutput = '
48+
<div data-content-type="html" data-appearance="default" data-element="main" class="test placeholder-html-code">
49+
&lt;div class="block-123"&gt;Test&lt;/div&gt;
50+
</div>';
51+
52+
$result = $this->htmlFilter->filterHtml($inputHtml);
53+
$this->assertEquals($expectedOutput, $result);
54+
}
55+
}

0 commit comments

Comments
 (0)