@@ -1764,23 +1764,6 @@ export function $splitNode(
17641764 return [ leftTree , rightTree ] ;
17651765}
17661766
1767- export function $findMatchingParent (
1768- startingNode : LexicalNode ,
1769- findFn : ( node : LexicalNode ) => boolean ,
1770- ) : LexicalNode | null {
1771- let curr : ElementNode | LexicalNode | null = startingNode ;
1772-
1773- while ( curr !== $getRoot ( ) && curr != null ) {
1774- if ( findFn ( curr ) ) {
1775- return curr ;
1776- }
1777-
1778- curr = curr . getParent ( ) ;
1779- }
1780-
1781- return null ;
1782- }
1783-
17841767/**
17851768 * @param x - The element being tested
17861769 * @returns Returns true if x is an HTML anchor tag, false otherwise
@@ -2141,3 +2124,37 @@ export function $create<T extends LexicalNode>(klass: Klass<T>): T {
21412124 ) ;
21422125 return new registeredNode . klass ( ) as T ;
21432126}
2127+
2128+ /**
2129+ * Starts with a node and moves up the tree (toward the root node) to find a matching node based on
2130+ * the search parameters of the findFn. (Consider JavaScripts' .find() function where a testing function must be
2131+ * passed as an argument. eg. if( (node) => node.__type === 'div') ) return true; otherwise return false
2132+ * @param startingNode - The node where the search starts.
2133+ * @param findFn - A testing function that returns true if the current node satisfies the testing parameters.
2134+ * @returns `startingNode` or one of its ancestors that matches the `findFn` predicate and is not the `RootNode`, or `null` if no match was found.
2135+ */
2136+ export const $findMatchingParent : {
2137+ < T extends LexicalNode > (
2138+ startingNode : LexicalNode ,
2139+ findFn : ( node : LexicalNode ) => node is T ,
2140+ ) : T | null ;
2141+ (
2142+ startingNode : LexicalNode ,
2143+ findFn : ( node : LexicalNode ) => boolean ,
2144+ ) : LexicalNode | null ;
2145+ } = (
2146+ startingNode : LexicalNode ,
2147+ findFn : ( node : LexicalNode ) => boolean ,
2148+ ) : LexicalNode | null => {
2149+ let curr : ElementNode | LexicalNode | null = startingNode ;
2150+
2151+ while ( curr != null && ! $isRootNode ( curr ) ) {
2152+ if ( findFn ( curr ) ) {
2153+ return curr ;
2154+ }
2155+
2156+ curr = curr . getParent ( ) ;
2157+ }
2158+
2159+ return null ;
2160+ } ;
0 commit comments