This commit is contained in:
2025-12-07 01:57:25 +01:00
parent 610389a5e6
commit c6c452e582

View File

@@ -970,12 +970,7 @@ class ApiKernel
$node->removeChild($node->firstChild);
}
$fragment = $doc->createDocumentFragment();
if (@$fragment->appendXML($replacement)) {
$node->appendChild($fragment);
} else {
$node->appendChild($doc->createTextNode($replacement));
}
$this->appendHtmlToNode($doc, $node, $replacement);
}
}
@@ -988,4 +983,44 @@ class ApiKernel
}
return $output;
}
private function appendHtmlToNode(DOMDocument $targetDoc, DOMElement $node, string $html): void
{
if (trim($html) === '') return;
$flags = 0;
if (defined('LIBXML_HTML_NOIMPLIED')) {
$flags |= LIBXML_HTML_NOIMPLIED;
}
if (defined('LIBXML_HTML_NODEFDTD')) {
$flags |= LIBXML_HTML_NODEFDTD;
}
$fragmentDoc = new DOMDocument('1.0', 'UTF-8');
libxml_use_internal_errors(true);
$wrapped = '<div id="fragment-wrapper">' . $html . '</div>';
$loaded = @$fragmentDoc->loadHTML('<?xml encoding="utf-8"?>' . $wrapped, $flags);
libxml_clear_errors();
if ($loaded) {
$wrapper = $fragmentDoc->getElementById('fragment-wrapper');
if ($wrapper) {
$children = [];
if ($wrapper->hasChildNodes()) {
foreach ($wrapper->childNodes as $child) {
$children[] = $child;
}
}
foreach ($children as $child) {
$imported = $targetDoc->importNode($child, true);
if ($imported) {
$node->appendChild($imported);
}
}
return;
}
}
$node->appendChild($targetDoc->createTextNode($html));
}
}