@@ -541,31 +541,34 @@ private function searchDepthPrev($path, $anchor, $depth = null, $ignoreRole = fa
541541 $ childNames = array_reverse ($ childNames );
542542 $ key = array_search ($ node ->getName (), $ childNames );
543543 $ childNames = array_slice ($ childNames , $ key + 1 );
544- }
545544
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+ }
551551
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+ }
556558 }
557559
558560 // check parents
559- // TODO do we need to traverse towards the anchor?
560561 if (0 === strpos ($ parentPath , $ anchor )) {
561562 $ parent = $ parent ->getParent ();
562563 $ childNames = $ parent ->getNodeNames ()->getArrayCopy ();
563564 $ key = array_search (PathHelper::getNodeName ($ parentPath ), $ childNames );
564565 $ childNames = array_slice ($ childNames , 0 , $ key + 1 );
565566 $ 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+ }
569572 }
570573 }
571574
@@ -604,7 +607,6 @@ private function searchDepthNext($path, $anchor, $depth = null, $ignoreRole = fa
604607 }
605608
606609 // 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?
608610 if (null === $ depth || PathHelper::getPathDepth ($ path ) - PathHelper::getPathDepth ($ anchor ) < $ depth ) {
609611 $ childNames = $ node ->getNodeNames ()->getArrayCopy ();
610612 $ result = $ this ->checkChildren ($ childNames , $ path , $ ignoreRole , $ class );
@@ -694,7 +696,7 @@ private function search($path, $reverse = false, $ignoreRole = false, $class = n
694696 public function getPrev ($ current , $ anchor = null , $ depth = null , $ ignoreRole = false , $ class = null )
695697 {
696698 if ($ anchor ) {
697- return $ this ->searchDepthPrev ($ current , $ anchor , $ depth , true , $ ignoreRole , $ class );
699+ return $ this ->searchDepthPrev ($ current , $ anchor , $ depth , $ ignoreRole , $ class );
698700 }
699701
700702 return $ this ->search ($ current , true , $ ignoreRole , $ class );
0 commit comments