Skip to content

Commit 0bba405

Browse files
committed
Fix passing index, parent to tests
1 parent c2d85e4 commit 0bba405

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

Diff for: index.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
var visit = require('unist-util-visit-parents')
4-
var is = require('hast-util-is-element')
4+
var convert = require('hast-util-is-element/convert')
55
var escape = require('escape-string-regexp')
66

77
var defaultIgnore = ['title', 'script', 'style', 'svg', 'math']
@@ -117,7 +117,7 @@ function findAndReplace(tree, find, replace, options) {
117117
}
118118

119119
function 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

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
],
2929
"dependencies": {
3030
"escape-string-regexp": "^4.0.0",
31-
"hast-util-is-element": "^1.0.0",
31+
"hast-util-is-element": "^1.1.0",
3232
"unist-util-visit-parents": "^3.0.0"
3333
},
3434
"devDependencies": {

Diff for: readme.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ Partial matches are not supported.
100100
`replace` (at `1`).
101101
When `Object`, each key is a `find` (in string form) and each value is a
102102
`replace`
103-
* `options.ignore` (`Array`, default: `['title', 'script', 'style', 'svg',
103+
* `options.ignore` (`Test`, default: `['title', 'script', 'style', 'svg',
104104
'math']`)
105-
Tag-names of elements *not* to search.
106-
This list can be accessed at `findAndReplace.ignore`
105+
Any [`hast-util-is-element`][test] compatible test.
106+
The default list can be accessed at `findAndReplace.ignore`
107107

108108
###### Returns
109109

@@ -205,3 +205,5 @@ abide by its terms.
205205
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
206206

207207
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
208+
209+
[test]: https://github.com/syntax-tree/hast-util-is-element#api

0 commit comments

Comments
 (0)