Skip to content

Commit

Permalink
Fix bug in setInnerHtml: can't rewrite existing content
Browse files Browse the repository at this point in the history
  • Loading branch information
Imangazaliev committed May 15, 2017
1 parent dc05790 commit a00fe02
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/DiDom/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,19 @@ public function setInnerHtml($html)
}

// remove all child nodes
foreach ($this->node->childNodes as $node)

// we need to collect child nodes to array
// because removing nodes from the DOMNodeList on iterating is not working
$childNodes = [];

foreach ($this->node->childNodes as $childNode)
{
$childNodes[] = $childNode;
}

foreach ($childNodes as $childNode)
{
$this->node->removeChild($node);
$this->node->removeChild($childNode);
}

if ($html !== '') {
Expand Down
7 changes: 7 additions & 0 deletions tests/DiDom/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,13 @@ public function testSetInnerHtml()
$this->assertEquals($list, $list->setInnerHtml($html));
$this->assertEquals(['One', 'Two', 'Three'], $list->find('li::text'));

// check inner HTML rewrite works

$html = '<li>Foo</li><li>Bar</li><li>Baz</li>';

$this->assertEquals($list, $list->setInnerHtml($html));
$this->assertEquals(['Foo', 'Bar', 'Baz'], $list->find('li::text'));

$html = '<div id="root"></div>';
$innerHtml = ' Plain text <span>Lorem ipsum.</span><span>Lorem ipsum.</span>';

Expand Down

0 comments on commit a00fe02

Please sign in to comment.