Skip to content
Snippets Groups Projects

#3412644 Whitespace HTML entities break DOM parsing in Drupal 10.2

Merged Eric Smith requested to merge issue/toc_api-3412644:3412644-original-approach into 8.x-1.x
1 file
+ 24
2
Compare changes
  • Side-by-side
  • Inline
+ 24
2
@@ -87,7 +87,17 @@ class TocBuilder implements TocBuilderInterface {
$fragment_node = $dom->createDocumentFragment();
$html = Html::normalize($this->renderer->render($build));
$fragment_node->appendXML($html);
// Trim to ensure the first child is a DOMElement.
$html = trim($html);
// Build a new DOMDocument for the HTML.
$fragment_dom = Html::load($html);
// Get the body element of the new document.
$fragment_body = $fragment_dom->getElementsByTagName('body')->item(0);
// Import the new (header) nodes onto the original DOMDocument.
foreach ($fragment_body->childNodes as $fragment_child_node) {
$fragment_child_node = $dom->importNode($fragment_child_node, true);
$fragment_node->appendChild($fragment_child_node);
}
// Replace the header node.
$replace = TRUE;
@@ -112,9 +122,21 @@ class TocBuilder implements TocBuilderInterface {
'#toc' => $toc,
'#item' => NULL,
];
$fragment_node = $dom->createDocumentFragment();
$html = Html::normalize($this->renderer->render($build));
$fragment_node->appendXML($html);
// Trim to ensure the first child is a DOMElement.
$html = trim($html);
// Build a new DOMDocument for the HTML.
$fragment_dom = Html::load($html);
// Get the body element of the new document.
$fragment_body = $fragment_dom->getElementsByTagName('body')->item(0);
// Import the new (header) nodes onto the original DOMDocument.
foreach ($fragment_body->childNodes as $fragment_child_node) {
$fragment_child_node = $dom->importNode($fragment_child_node, true);
$fragment_node->appendChild($fragment_child_node);
}
if (!empty($fragment_node->textContent)) {
$dom->getElementsByTagName('body')->item(0)->appendChild($fragment_node);
}
Loading