Skip to content

Commit

Permalink
Result of clone() now functional, fixes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
Rct567 committed Sep 14, 2023
1 parent 87f6b29 commit 1d6c060
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Rct567/DomQuery/DomQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,17 @@ public function filter($selector)
*/
public function clone()
{
return $this->createChildInstance($this->getClonedNodes());
$new_instance = self::create($this->getClonedNodes());

if (\is_bool($this->xml_mode)) {
$new_instance->xml_mode = $this->xml_mode;
}

if (isset($this->document) && $this->dom_xpath instanceof \DOMXPath) {
$new_instance->dom_xpath = $this->dom_xpath;
}

return $new_instance;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,22 @@ public function testEachIteration()
$this->assertCount(2, $result);
$this->assertInstanceOf(\DOMNode::class, $result[0]);
}

/*
* Test clone
*/
public function testClone()
{
$dom = new DomQuery('<div><p>My words</p></div>');
$elm = $dom->find('p');
$cloned_elm = $elm->clone();
$this->assertEquals('<p>My words</p>', (string) $elm);
$this->assertEquals('<p>My words</p>', (string) $cloned_elm);
$this->assertFalse( $elm->get(0)->isSameNode( $cloned_elm->get(0)) );

$dom_clone = $dom->clone();
$this->assertEquals('<div><p>My words</p></div>', $dom_clone);
$this->assertEquals('<p>My words</p>', $dom_clone->find('p')->first());
$this->assertTrue($dom_clone->find('p')->first()->is('p'));
}
}

0 comments on commit 1d6c060

Please sign in to comment.