Skip to content

Commit

Permalink
Fixed #65 Cannot replace root elements in html-fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWeinert committed Mar 3, 2017
1 parent ebf5c45 commit 7172b4b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/FluentDOM/Node/ChildNode/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public function remove() {
public function before($nodes) {
/** @var \DOMNode|Implementation $this */
if (
$this->parentNode instanceof \DOMElement &&
(
$this->parentNode instanceof \DOMElement ||
$this->parentNode instanceof \DOMDocument
) &&
($nodes = MutationMacro::expand($this->ownerDocument, $nodes))
) {
$this->parentNode->insertBefore($nodes, $this);
Expand All @@ -42,7 +45,10 @@ public function before($nodes) {
public function after($nodes) {
/** @var \DOMNode|Implementation $this */
if (
$this->parentNode instanceof \DOMElement &&
(
$this->parentNode instanceof \DOMElement ||
$this->parentNode instanceof \DOMDocument
) &&
($nodes = MutationMacro::expand($this->ownerDocument, $nodes))
) {
if ($this->nextSibling instanceof \DOMNode) {
Expand Down
14 changes: 14 additions & 0 deletions tests/FluentDOM/Node/ChildNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,19 @@ public function testReplaceWithElementNode() {
$dom->saveXML()
);
}

/**
* @covers \FluentDOM\Node\ChildNode\Implementation
*/
public function testReplaceWithDocumentElement() {
$dom = new Document();
$dom->loadXML('<foo><bar/></foo>');
$newNode = $dom->createElement('replaced');
$dom('/foo')->item(0)->replace($newNode);
$this->assertXmlStringEqualsXmlString(
'<replaced/>',
$dom->saveXML()
);
}
}
}

0 comments on commit 7172b4b

Please sign in to comment.