From c6c452e582492db89653a6b0b3a5451b1df04705 Mon Sep 17 00:00:00 2001 From: Lars Gebhardt-Kusche Date: Sun, 7 Dec 2025 01:57:25 +0100 Subject: [PATCH] up --- src/ApiKernel.php | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/ApiKernel.php b/src/ApiKernel.php index 40a0346..1958035 100644 --- a/src/ApiKernel.php +++ b/src/ApiKernel.php @@ -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 = '
' . $html . '
'; + $loaded = @$fragmentDoc->loadHTML('' . $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)); + } }