Skip to content

Commit 7f9d1dd

Browse files
committed
Fix DOMUtils::setInnerHtml() for not xml compliant html
1 parent 6613c94 commit 7f9d1dd

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/wcmf/lib/util/DOMUtils.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,18 @@ public static function getInnerHtml(\DOMElement $element) {
7979
* @param $html
8080
*/
8181
public static function setInnerHtml(\DOMElement $element, $html) {
82-
$fragment = $element->ownerDocument->createDocumentFragment();
83-
$fragment->appendXML($html);
84-
$element->appendChild($fragment);
82+
$doc = new \DOMDocument();
83+
$doc->loadHTML('<body>'.trim(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')).'</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
84+
$contentNode = $doc->getElementsByTagName('body')->item(0);
85+
$contentNode = $element->ownerDocument->importNode($contentNode, true);
86+
$oldChildren = $element->childNodes;
87+
foreach ($oldChildren as $child) {
88+
$element->removeChild($child);
89+
}
90+
$newChildren = $contentNode->childNodes;
91+
foreach ($newChildren as $child) {
92+
$element->appendChild($child->cloneNode(true));
93+
}
8594
}
8695

8796

0 commit comments

Comments
 (0)