@@ -851,15 +851,15 @@ export function find({
851851 expandAllMatchPaths = false ,
852852 expandFocusMatchPaths = true ,
853853} ) {
854- const matches = [ ] ;
855-
854+ let matchCount = 0 ;
856855 const trav = ( {
857856 isPseudoRoot = false ,
858857 node,
859858 currentIndex,
860859 path = [ ] ,
861860 } ) => {
862- let hasMatch = false ;
861+ let matches = [ ] ;
862+ let isSelfMatch = false ;
863863 let hasFocusMatch = false ;
864864 // The pseudo-root is not considered in the path
865865 const selfPath = isPseudoRoot ? [ ] : [
@@ -881,15 +881,22 @@ export function find({
881881
882882 // Examine the current node to see if it is a match
883883 if ( ! isPseudoRoot && searchMethod ( { ...extraInfo , node : newNode , searchQuery } ) ) {
884- hasMatch = true ;
885- if ( matches . length === searchFocusOffset ) {
884+ if ( matchCount === searchFocusOffset ) {
886885 hasFocusMatch = true ;
887886 if ( ( expandAllMatchPaths || expandFocusMatchPaths ) && hasChildren ) {
888887 newNode . expanded = true ;
889888 }
890889 }
891890
892- matches . push ( { ...extraInfo , node : newNode } ) ;
891+ // Keep track of the number of matching nodes, so we know when the searchFocusOffset
892+ // is reached
893+ matchCount ++ ;
894+
895+ // We cannot add this node to the matches right away, as it may be changed
896+ // during the search of the descendants. The entire node is used in
897+ // comparisons between nodes inside the `matches` and `treeData` results
898+ // of this method (`find`)
899+ isSelfMatch = true ;
893900 }
894901
895902 if ( hasChildren ) {
@@ -912,16 +919,15 @@ export function find({
912919 childIndex += 1 ;
913920 }
914921
915- if ( mapResult . hasMatch || mapResult . hasFocusMatch ) {
916- hasMatch = true ;
917-
922+ if ( mapResult . matches . length > 0 || mapResult . hasFocusMatch ) {
923+ matches = [ ...matches , ...mapResult . matches ] ;
918924 if ( mapResult . hasFocusMatch ) {
919925 hasFocusMatch = true ;
920926 }
921927
922928 // Expand the current node if it has descendants matching the search
923929 // and the settings are set to do so.
924- if ( ( expandAllMatchPaths && mapResult . hasMatch ) ||
930+ if ( ( expandAllMatchPaths && mapResult . matches . length > 0 ) ||
925931 ( ( expandAllMatchPaths || expandFocusMatchPaths ) && mapResult . hasFocusMatch )
926932 ) {
927933 newNode . expanded = true ;
@@ -930,13 +936,14 @@ export function find({
930936
931937 return mapResult . node ;
932938 } ) ;
933- } else {
934- childIndex += 1 ;
935939 }
936940
937941 return {
938- node : hasMatch ? newNode : node ,
939- hasMatch,
942+ node : matches . length > 0 ? newNode : node ,
943+ matches : ! isSelfMatch ? matches : [
944+ { ...extraInfo , node : newNode } ,
945+ ...matches ,
946+ ] ,
940947 hasFocusMatch,
941948 treeIndex : childIndex ,
942949 } ;
@@ -949,7 +956,7 @@ export function find({
949956 } ) ;
950957
951958 return {
952- matches,
959+ matches : result . matches ,
953960 treeData : result . node . children ,
954961 } ;
955962}
0 commit comments