2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -411,7 +411,7 @@ public function closest(string $selector): ?self
411
411
412
412
$ domNode = $ this ->getNode (0 );
413
413
414
- while (\XML_ELEMENT_NODE === $ domNode ->nodeType ) {
414
+ while (null !== $ domNode && \XML_ELEMENT_NODE === $ domNode ->nodeType ) {
415
415
$ node = $ this ->createSubCrawler ($ domNode );
416
416
if ($ node ->matches ($ selector )) {
417
417
return $ node ;
Original file line number Diff line number Diff line change @@ -1031,6 +1031,29 @@ public function testClosest()
1031
1031
$ this ->assertNull ($ notFound );
1032
1032
}
1033
1033
1034
+ public function testClosestWithOrphanedNode ()
1035
+ {
1036
+ $ html = <<<'HTML'
1037
+ <html lang="en">
1038
+ <body>
1039
+ <div id="foo" class="newFoo ok">
1040
+ <div class="lorem1 ko"></div>
1041
+ </div>
1042
+ </body>
1043
+ </html>
1044
+ HTML;
1045
+
1046
+ $ crawler = $ this ->createCrawler ($ this ->getDoctype ().$ html );
1047
+ $ foo = $ crawler ->filter ('#foo ' );
1048
+
1049
+ $ fooNode = $ foo ->getNode (0 );
1050
+
1051
+ $ fooNode ->parentNode ->replaceChild ($ fooNode ->ownerDocument ->createElement ('ol ' ), $ fooNode );
1052
+
1053
+ $ body = $ foo ->closest ('body ' );
1054
+ $ this ->assertNull ($ body );
1055
+ }
1056
+
1034
1057
public function testOuterHtml ()
1035
1058
{
1036
1059
$ html = <<<'HTML'
0 commit comments