@@ -117,6 +117,7 @@ export function findAndReplace(tree, find, replace, options) {
117
117
const replace = pairs [ pairIndex ] [ 1 ]
118
118
let start = 0
119
119
const index = parent . children . indexOf ( node )
120
+ let change = false
120
121
/** @type {Array<Content> } */
121
122
let nodes = [ ]
122
123
/** @type {number|undefined } */
@@ -142,9 +143,7 @@ export function findAndReplace(tree, find, replace, options) {
142
143
}
143
144
144
145
// It wasn’t a match after all.
145
- if ( value === false ) {
146
- position = undefined
147
- } else {
146
+ if ( value !== false ) {
148
147
if ( start !== position ) {
149
148
nodes . push ( { type : 'text' , value : node . value . slice ( start , position ) } )
150
149
}
@@ -156,6 +155,7 @@ export function findAndReplace(tree, find, replace, options) {
156
155
}
157
156
158
157
start = position + match [ 0 ] . length
158
+ change = true
159
159
}
160
160
161
161
if ( ! find . global ) {
@@ -165,14 +165,14 @@ export function findAndReplace(tree, find, replace, options) {
165
165
match = find . exec ( node . value )
166
166
}
167
167
168
- if ( position === undefined ) {
169
- nodes = [ node ]
170
- } else {
168
+ if ( change ) {
171
169
if ( start < node . value . length ) {
172
170
nodes . push ( { type : 'text' , value : node . value . slice ( start ) } )
173
171
}
174
172
175
173
parent . children . splice ( index , 1 , ...nodes )
174
+ } else {
175
+ nodes = [ node ]
176
176
}
177
177
178
178
return index + nodes . length
0 commit comments