@@ -541,31 +541,34 @@ private function searchDepthPrev($path, $anchor, $depth = null, $ignoreRole = fa
541
541
$ childNames = array_reverse ($ childNames );
542
542
$ key = array_search ($ node ->getName (), $ childNames );
543
543
$ childNames = array_slice ($ childNames , $ key + 1 );
544
- }
545
544
546
- // traverse the previous siblings down the tree
547
- $ result = $ this ->traversePrevDepth ($ depth , PathHelper::getPathDepth ($ anchor ), $ childNames , $ parentPath , $ ignoreRole , $ class );
548
- if ($ result ) {
549
- return $ result ;
550
- }
545
+ if (!empty ($ childNames )) {
546
+ // traverse the previous siblings down the tree
547
+ $ result = $ this ->traversePrevDepth ($ depth , PathHelper::getPathDepth ($ anchor ), $ childNames , $ parentPath , $ ignoreRole , $ class );
548
+ if ($ result ) {
549
+ return $ result ;
550
+ }
551
551
552
- // check siblings
553
- $ result = $ this ->checkChildren ($ childNames , $ parentPath , $ ignoreRole , $ class );
554
- if ($ result ) {
555
- return $ result ;
552
+ // check siblings
553
+ $ result = $ this ->checkChildren ($ childNames , $ parentPath , $ ignoreRole , $ class );
554
+ if ($ result ) {
555
+ return $ result ;
556
+ }
557
+ }
556
558
}
557
559
558
560
// check parents
559
- // TODO do we need to traverse towards the anchor?
560
561
if (0 === strpos ($ parentPath , $ anchor )) {
561
562
$ parent = $ parent ->getParent ();
562
563
$ childNames = $ parent ->getNodeNames ()->getArrayCopy ();
563
564
$ key = array_search (PathHelper::getNodeName ($ parentPath ), $ childNames );
564
565
$ childNames = array_slice ($ childNames , 0 , $ key + 1 );
565
566
$ childNames = array_reverse ($ childNames );
566
- $ result = $ this ->checkChildren ($ childNames , $ parent ->getPath (), $ ignoreRole , $ class );
567
- if ($ result ) {
568
- return $ result ;
567
+ if (!empty ($ childNames )) {
568
+ $ result = $ this ->checkChildren ($ childNames , $ parent ->getPath (), $ ignoreRole , $ class );
569
+ if ($ result ) {
570
+ return $ result ;
571
+ }
569
572
}
570
573
}
571
574
@@ -604,7 +607,6 @@ private function searchDepthNext($path, $anchor, $depth = null, $ignoreRole = fa
604
607
}
605
608
606
609
// take the first eligible child if there are any
607
- // TODO do we need to traverse away from the anchor up to the depth here?
608
610
if (null === $ depth || PathHelper::getPathDepth ($ path ) - PathHelper::getPathDepth ($ anchor ) < $ depth ) {
609
611
$ childNames = $ node ->getNodeNames ()->getArrayCopy ();
610
612
$ result = $ this ->checkChildren ($ childNames , $ path , $ ignoreRole , $ class );
@@ -694,7 +696,7 @@ private function search($path, $reverse = false, $ignoreRole = false, $class = n
694
696
public function getPrev ($ current , $ anchor = null , $ depth = null , $ ignoreRole = false , $ class = null )
695
697
{
696
698
if ($ anchor ) {
697
- return $ this ->searchDepthPrev ($ current , $ anchor , $ depth , true , $ ignoreRole , $ class );
699
+ return $ this ->searchDepthPrev ($ current , $ anchor , $ depth , $ ignoreRole , $ class );
698
700
}
699
701
700
702
return $ this ->search ($ current , true , $ ignoreRole , $ class );
0 commit comments