Skip to content

Commit 1d6c060

Browse files
committed
Result of clone() now functional, fixes #34
1 parent 87f6b29 commit 1d6c060

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Rct567/DomQuery/DomQuery.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,17 @@ public function filter($selector)
645645
*/
646646
public function clone()
647647
{
648-
return $this->createChildInstance($this->getClonedNodes());
648+
$new_instance = self::create($this->getClonedNodes());
649+
650+
if (\is_bool($this->xml_mode)) {
651+
$new_instance->xml_mode = $this->xml_mode;
652+
}
653+
654+
if (isset($this->document) && $this->dom_xpath instanceof \DOMXPath) {
655+
$new_instance->dom_xpath = $this->dom_xpath;
656+
}
657+
658+
return $new_instance;
649659
}
650660

651661
/**

tests/Rct567/DomQuery/Tests/DomQueryMiscellaneousElementMethodTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,22 @@ public function testEachIteration()
123123
$this->assertCount(2, $result);
124124
$this->assertInstanceOf(\DOMNode::class, $result[0]);
125125
}
126+
127+
/*
128+
* Test clone
129+
*/
130+
public function testClone()
131+
{
132+
$dom = new DomQuery('<div><p>My words</p></div>');
133+
$elm = $dom->find('p');
134+
$cloned_elm = $elm->clone();
135+
$this->assertEquals('<p>My words</p>', (string) $elm);
136+
$this->assertEquals('<p>My words</p>', (string) $cloned_elm);
137+
$this->assertFalse( $elm->get(0)->isSameNode( $cloned_elm->get(0)) );
138+
139+
$dom_clone = $dom->clone();
140+
$this->assertEquals('<div><p>My words</p></div>', $dom_clone);
141+
$this->assertEquals('<p>My words</p>', $dom_clone->find('p')->first());
142+
$this->assertTrue($dom_clone->find('p')->first()->is('p'));
143+
}
126144
}

0 commit comments

Comments
 (0)