Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Allow any HTML as content for replaceNodeContent #13

Open
wants to merge 5 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/DomHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/src/Unit/DomHelperTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ public function testReplaceNodeContent() {
// Test replacing again with a non-empty value.
$this->replaceNodeContent($this->node, '<div></div>');
$this->assertEquals(Html::serialize($this->document), '<outer><div></div></outer>');
// Test replacing again with a value without root element.
$two_nodes = $this->replaceNodeContent($this->node, '<p>first</p><p>second</p>');
$this->assertEquals(Html::serialize($this->document), '<outer><p>first</p><p>second</p></outer>');
$this->assertEquals(count($two_nodes), 2);
// Test replacing again with a non-empty value.
$new_nodes = $this->replaceNodeContent($this->node, '<p>third</p>');
$this->assertEquals(Html::serialize($this->document), '<outer><p>first</p><p>third</p></outer>');
$this->assertEquals(count($new_nodes), 1);
// Test replacing of returned DOMNode objects.
$this->node = $two_nodes[0];
$new_nodes = $this->replaceNodeContent($this->node, 'Second <!-- comment --> ');
$this->assertEquals(Html::serialize($this->document), '<outer>Second <!-- comment --> <p>third</p></outer>');
$this->assertEquals(count($new_nodes), 3);
}

/**
Expand Down