1
1
'use strict'
2
2
3
3
var visit = require ( 'unist-util-visit-parents' )
4
- var is = require ( 'hast-util-is-element' )
4
+ var convert = require ( 'hast-util-is-element/convert ' )
5
5
var escape = require ( 'escape-string-regexp' )
6
6
7
7
var defaultIgnore = [ 'title' , 'script' , 'style' , 'svg' , 'math' ]
@@ -117,7 +117,7 @@ function findAndReplace(tree, find, replace, options) {
117
117
}
118
118
119
119
function search ( tree , options , handler ) {
120
- var ignore = options . ignore || defaultIgnore
120
+ var ignored = convert ( options . ignore || defaultIgnore )
121
121
var result = [ ]
122
122
123
123
visit ( tree , 'text' , visitor )
@@ -126,15 +126,27 @@ function search(tree, options, handler) {
126
126
127
127
function visitor ( node , parents ) {
128
128
var length = parents . length
129
- var index = length
129
+ var index = - 1
130
+ var parent
131
+ var grandparent
130
132
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
+ ) {
133
143
return
134
144
}
145
+
146
+ grandparent = parent
135
147
}
136
148
137
- handler ( node , parents [ length - 1 ] )
149
+ handler ( node , grandparent )
138
150
}
139
151
}
140
152
0 commit comments