 src/DomHelperTrait.php                | 20 ++++++++--------
 tests/src/Unit/DomHelperTraitTest.php | 43 +++++++++++++++++++++++++++--------
 2 files changed, 44 insertions(+), 19 deletions(-)

diff --git a/src/DomHelperTrait.php b/src/DomHelperTrait.php
index de72fd6..6a673c7 100644
--- a/src/DomHelperTrait.php
+++ b/src/DomHelperTrait.php
@@ -88,22 +88,22 @@ trait DomHelperTrait {
    */
   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 content into a new DOMDocument and retrieve the DOM nodes.
+      $replacement_nodes = Html::load($content)->getElementsByTagName('body')
         ->item(0)
-        ->childNodes
-        ->item(0);
+        ->childNodes;
     }
     else {
-      $replacement_node = $node->ownerDocument->createTextNode('');
+      $replacement_nodes = [$node->ownerDocument->createTextNode('')];
     }
 
-    // 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) {
+      // Import the replacement node from the new DOMDocument into the original
+      // one, importing also the child nodes of the replacement node.
+      $replacement_node = $node->ownerDocument->importNode($replacement_node, TRUE);
+      $node->parentNode->insertBefore($replacement_node, $node);
+    }
     $node->parentNode->removeChild($node);
-    $node = $replacement_node;
   }
 
   /**
diff --git a/tests/src/Unit/DomHelperTraitTest.php b/tests/src/Unit/DomHelperTraitTest.php
index 17b75e3..f547abb 100644
--- a/tests/src/Unit/DomHelperTraitTest.php
+++ b/tests/src/Unit/DomHelperTraitTest.php
@@ -62,16 +62,41 @@ class DomHelperTraitTest extends UnitTestCase {
 
   /**
    * Test DomHelperTrait::replaceNodeContent().
+   *
+   * @dataProvider providerTestReplaceNodeContent
    */
-  public function testReplaceNodeContent() {
-    $this->replaceNodeContent($this->node, '<div><replacement replaced="true" /></div>');
-    $this->assertEquals(Html::serialize($this->document), '<outer><div><replacement replaced="true"></replacement></div></outer>');
-    // Test replacing with an empty value.
-    $this->replaceNodeContent($this->node, '');
-    $this->assertEquals(Html::serialize($this->document), '<outer></outer>');
-    // Test replacing again with a non-empty value.
-    $this->replaceNodeContent($this->node, '<div></div>');
-    $this->assertEquals(Html::serialize($this->document), '<outer><div></div></outer>');
+  public function testReplaceNodeContent($content, $expected_output) {
+    $this->replaceNodeContent($this->node, $content);
+    $this->assertEquals($expected_output, Html::serialize($this->document));
+  }
+
+  /**
+   * @return array
+   * @see ::testReplaceNodeContent()
+   */
+  public function providerTestReplaceNodeContent() {
+    return [
+      'empty' => [
+        '',
+        '<outer></outer>',
+      ],
+      'single node without children' => [
+        '<div></div>',
+        '<outer><div></div></outer>',
+      ],
+      'single node with children' => [
+        '<div><replacement replaced="true" /></div>',
+        '<outer><div><replacement replaced="true"></replacement></div></outer>',
+      ],
+      'multiple nodes' => [
+        '<p>first</p><p>second</p>',
+        '<outer><p>first</p><p>second</p></outer>',
+      ],
+      'multiple nodes, with a text node, comment node and element node' => [
+        'Second <!-- comment --> <p>third</p>',
+        '<outer>Second <!-- comment --> <p>third</p></outer>',
+      ]
+    ];
   }
 
   /**
