Skip to content

Commit

Permalink
Check for null parent Nodes in the case of orphaned branches
Browse files Browse the repository at this point in the history
  • Loading branch information
ttk authored and fabpot committed Feb 14, 2025
1 parent fd07959 commit 19073e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public function closest(string $selector): ?self

$domNode = $this->getNode(0);

while (\XML_ELEMENT_NODE === $domNode->nodeType) {
while (null !== $domNode && \XML_ELEMENT_NODE === $domNode->nodeType) {
$node = $this->createSubCrawler($domNode);
if ($node->matches($selector)) {
return $node;
Expand Down
23 changes: 23 additions & 0 deletions Tests/AbstractCrawlerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,29 @@ public function testClosest()
$this->assertNull($notFound);
}

public function testClosestWithOrphanedNode()
{
$html = <<<'HTML'
<html lang="en">
<body>
<div id="foo" class="newFoo ok">
<div class="lorem1 ko"></div>
</div>
</body>
</html>
HTML;

$crawler = $this->createCrawler($this->getDoctype().$html);
$foo = $crawler->filter('#foo');

$fooNode = $foo->getNode(0);

$fooNode->parentNode->replaceChild($fooNode->ownerDocument->createElement('ol'), $fooNode);

$body = $foo->closest('body');
$this->assertNull($body);
}

public function testOuterHtml()
{
$html = <<<'HTML'
Expand Down

0 comments on commit 19073e3

Please sign in to comment.