Skip to content

Commit a6303b8

Browse files
committed
MC-3578: Script tag removal
1 parent 9462e81 commit a6303b8

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

app/code/Magento/PageBuilder/Model/Stage/Renderer/CmsStaticBlock.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public function render(array $params): array
8888

8989
if ($block->isActive()) {
9090
$directiveResult = $this->widgetDirectiveRenderer->render($params);
91-
$result['content'] = $this->removeScriptTags($directiveResult['content']);
92-
$result['error'] = $directiveResult['error'];
91+
$result = $this->removeScriptTags($directiveResult);
9392
} else {
9493
$result['error'] = __('Block disabled');
9594
}
@@ -100,21 +99,26 @@ public function render(array $params): array
10099
/**
101100
* Remove script tag from html
102101
*
103-
* @param string $html
104-
* @return string
102+
* @param array $directiveResult
103+
* @return array
105104
*/
106-
private function removeScriptTags(string $html) : string
105+
private function removeScriptTags(array $directiveResult): array
107106
{
108-
$dom = new \DOMDocument();
109-
try {
110-
$dom->loadHTML($html);
111-
} catch (\Exception $e) {
112-
$this->loggerInterface->critical($e->getMessage());
113-
return $html;
114-
}
115-
foreach (iterator_to_array($dom->getElementsByTagName('script')) as $item) {
116-
$item->parentNode->removeChild($item);
117-
}
118-
return $dom->saveHTML();
107+
$dom = new \DOMDocument();
108+
try {
109+
$dom->loadHTML($directiveResult['content']);
110+
} catch (\Exception $e) {
111+
$this->loggerInterface->critical($e->getMessage());
112+
return [
113+
'error' => __('The block cannot be displayed because it contains errors.')
114+
];
115+
}
116+
foreach (iterator_to_array($dom->getElementsByTagName('script')) as $item) {
117+
$item->parentNode->removeChild($item);
118+
}
119+
return [
120+
'content' => $dom->saveHTML(),
121+
'error' => $directiveResult['error']
122+
];
119123
}
120124
}

0 commit comments

Comments
 (0)