11'use strict'
22
33var visit = require ( 'unist-util-visit-parents' )
4- var is = require ( 'hast-util-is-element' )
4+ var convert = require ( 'hast-util-is-element/convert ' )
55var escape = require ( 'escape-string-regexp' )
66
77var defaultIgnore = [ 'title' , 'script' , 'style' , 'svg' , 'math' ]
@@ -117,7 +117,7 @@ function findAndReplace(tree, find, replace, options) {
117117}
118118
119119function search ( tree , options , handler ) {
120- var ignore = options . ignore || defaultIgnore
120+ var ignored = convert ( options . ignore || defaultIgnore )
121121 var result = [ ]
122122
123123 visit ( tree , 'text' , visitor )
@@ -126,15 +126,27 @@ function search(tree, options, handler) {
126126
127127 function visitor ( node , parents ) {
128128 var length = parents . length
129- var index = length
129+ var index = - 1
130+ var parent
131+ var grandparent
130132
131- while ( index -- ) {
132- if ( is ( parents [ index ] , ignore ) ) {
133+ while ( ++ index < length ) {
134+ parent = parents [ index ]
135+
136+ if (
137+ ignored (
138+ parent ,
139+ grandparent ? grandparent . children . indexOf ( parent ) : undefined ,
140+ grandparent
141+ )
142+ ) {
133143 return
134144 }
145+
146+ grandparent = parent
135147 }
136148
137- handler ( node , parents [ length - 1 ] )
149+ handler ( node , grandparent )
138150 }
139151}
140152
0 commit comments