@@ -6,6 +6,8 @@ var escape = require('escape-string-regexp')
6
6
7
7
var defaultIgnore = [ 'title' , 'script' , 'style' , 'svg' , 'math' ]
8
8
9
+ var splice = [ ] . splice
10
+
9
11
module . exports = findAndReplace
10
12
11
13
findAndReplace . ignore = defaultIgnore
@@ -36,82 +38,78 @@ function findAndReplace(tree, find, replace, options) {
36
38
return handler
37
39
38
40
function handler ( node , parent ) {
39
- var siblings = parent . children
40
- var pos = siblings . indexOf ( node )
41
- var value = node . value
42
41
var find = pair [ 0 ]
43
42
var replace = pair [ 1 ]
44
- var lastIndex = 0
45
43
var nodes = [ ]
46
- var subvalue
47
- var index
48
- var length
44
+ var start = 0
45
+ var index = parent . children . indexOf ( node )
46
+ var position
49
47
var match
50
48
var subhandler
49
+ var value
51
50
52
51
find . lastIndex = 0
53
52
54
- match = find . exec ( value )
53
+ match = find . exec ( node . value )
55
54
56
55
while ( match ) {
57
- index = match . index
58
- subvalue = value . slice ( lastIndex , index )
56
+ position = match . index
57
+ value = replace . apply (
58
+ null ,
59
+ [ ] . concat ( match , { index : match . index , input : match . input } )
60
+ )
59
61
60
- if ( subvalue ) {
61
- nodes . push ( { type : 'text' , value : subvalue } )
62
- }
62
+ if ( value !== false ) {
63
+ if ( start !== position ) {
64
+ nodes . push ( { type : 'text' , value : node . value . slice ( start , position ) } )
65
+ }
63
66
64
- subvalue = replace . apply ( null , match )
67
+ if ( typeof value === 'string' && value . length > 0 ) {
68
+ value = { type : 'text' , value : value }
69
+ }
65
70
66
- if ( subvalue ) {
67
- if ( typeof subvalue === 'string' ) {
68
- subvalue = { type : 'text' , value : subvalue }
71
+ if ( value ) {
72
+ nodes . push ( value )
69
73
}
70
74
71
- nodes . push ( subvalue )
75
+ start = position + match [ 0 ] . length
72
76
}
73
77
74
- lastIndex = index + match [ 0 ] . length
75
-
76
78
if ( ! find . global ) {
77
79
break
78
80
}
79
81
80
- match = find . exec ( value )
82
+ match = find . exec ( node . value )
81
83
}
82
84
83
- if ( index === undefined ) {
85
+ if ( position === undefined ) {
84
86
nodes = [ node ]
87
+ index --
85
88
} else {
86
- subvalue = value . slice ( lastIndex )
87
-
88
- if ( subvalue ) {
89
- nodes . push ( { type : 'text' , value : subvalue } )
89
+ if ( start < node . value . length ) {
90
+ nodes . push ( { type : 'text' , value : node . value . slice ( start ) } )
90
91
}
91
92
92
- parent . children = siblings
93
- . slice ( 0 , pos )
94
- . concat ( nodes )
95
- . concat ( siblings . slice ( pos + 1 ) )
96
- }
97
-
98
- if ( pairs . length <= 1 ) {
99
- return
93
+ nodes . unshift ( index , 1 )
94
+ splice . apply ( parent . children , nodes )
100
95
}
101
96
102
- length = nodes . length
103
- index = - 1
104
- subhandler = handlerFactory ( pairs . slice ( 1 ) )
97
+ if ( pairs . length > 1 ) {
98
+ subhandler = handlerFactory ( pairs . slice ( 1 ) )
99
+ position = - 1
105
100
106
- while ( ++ index < length ) {
107
- node = nodes [ index ]
101
+ while ( ++ position < nodes . length ) {
102
+ node = nodes [ position ]
108
103
109
- if ( node . type === 'text' ) {
110
- subhandler ( node , parent )
111
- } else {
112
- search ( node , settings , subhandler )
104
+ if ( node . type === 'text' ) {
105
+ subhandler ( node , parent )
106
+ } else {
107
+ search ( node , settings , subhandler )
108
+ }
113
109
}
114
110
}
111
+
112
+ return index + nodes . length + 1
115
113
}
116
114
}
117
115
}
@@ -125,12 +123,11 @@ function search(tree, options, handler) {
125
123
return result
126
124
127
125
function visitor ( node , parents ) {
128
- var length = parents . length
129
126
var index = - 1
130
127
var parent
131
128
var grandparent
132
129
133
- while ( ++ index < length ) {
130
+ while ( ++ index < parents . length ) {
134
131
parent = parents [ index ]
135
132
136
133
if (
@@ -146,25 +143,23 @@ function search(tree, options, handler) {
146
143
grandparent = parent
147
144
}
148
145
149
- handler ( node , grandparent )
146
+ return handler ( node , grandparent )
150
147
}
151
148
}
152
149
153
150
function toPairs ( schema ) {
154
151
var result = [ ]
155
152
var key
156
- var length
157
153
var index
158
154
159
155
if ( typeof schema !== 'object' ) {
160
156
throw new Error ( 'Expected array or object as schema' )
161
157
}
162
158
163
159
if ( 'length' in schema ) {
164
- length = schema . length
165
160
index = - 1
166
161
167
- while ( ++ index < length ) {
162
+ while ( ++ index < schema . length ) {
168
163
result . push ( [
169
164
toExpression ( schema [ index ] [ 0 ] ) ,
170
165
toFunction ( schema [ index ] [ 1 ] )
0 commit comments