diff --git a/src/DomHelperTrait.php b/src/DomHelperTrait.php index de72fd6..f053a96 100644 --- a/src/DomHelperTrait.php +++ b/src/DomHelperTrait.php @@ -85,25 +85,35 @@ protected function setNodeContent(\DOMNode $node, $content) { * A DOMNode object. * @param string $content * The text or HTML that will replace the contents of $node. + * + * @return array + * Array of DOMNode objects that replaced a node. */ protected function replaceNodeContent(\DOMNode &$node, $content) { if (strlen($content)) { - // Load the contents into a new DOMDocument and retrieve the element. - $replacement_node = Html::load($content)->getElementsByTagName('body') + // Load the contents into a new DOMDocument and retrieve elements. + $replacement_nodes = Html::load($content)->getElementsByTagName('body') ->item(0) - ->childNodes - ->item(0); + ->childNodes; } else { - $replacement_node = $node->ownerDocument->createTextNode(''); + $replacement_nodes = [$node->ownerDocument->createTextNode('')]; } + // We can't just return $replacement_nodes because it may be an array or + // DOMNodeList object. So collect replacement nodes into new array. + $replacement_nodes_array = []; // Import the updated DOMNode from the new DOMDocument into the original // one, importing also the child nodes of the replacement DOMNode. - $replacement_node = $node->ownerDocument->importNode($replacement_node, TRUE); - $node->parentNode->appendChild($replacement_node); + foreach ($replacement_nodes as $replacement_node) { + $replacement_node = $node->ownerDocument->importNode($replacement_node, TRUE); + $node->parentNode->insertBefore($replacement_node, $node); + $replacement_nodes_array[] = $replacement_node; + } $node->parentNode->removeChild($node); $node = $replacement_node; + + return $replacement_nodes_array; } /** diff --git a/tests/src/Unit/DomHelperTraitTest.php b/tests/src/Unit/DomHelperTraitTest.php index 17b75e3..c45ef6a 100644 --- a/tests/src/Unit/DomHelperTraitTest.php +++ b/tests/src/Unit/DomHelperTraitTest.php @@ -72,6 +72,19 @@ public function testReplaceNodeContent() { // Test replacing again with a non-empty value. $this->replaceNodeContent($this->node, '
'); $this->assertEquals(Html::serialize($this->document), 'first
second
'); + $this->assertEquals(Html::serialize($this->document), 'first
second
third
'); + $this->assertEquals(Html::serialize($this->document), 'first
third
third