@@ -13,7 +13,8 @@ var single = {
13
13
}
14
14
15
15
// Pair delimiters.
16
- // From common sense, and WikiPedia: <https://en.wikipedia.org/wiki/Quotation_mark>.
16
+ // From common sense, and WikiPedia:
17
+ // <https://en.wikipedia.org/wiki/Quotation_mark>.
17
18
var pairs = {
18
19
',' : {
19
20
',' : true
@@ -102,9 +103,10 @@ function isLiteral(parent, index) {
102
103
}
103
104
104
105
if (
105
- ( ! hasWordsBefore ( parent , index ) && nextDelimiter ( parent , index , single ) ) ||
106
- ( ! hasWordsAfter ( parent , index ) &&
107
- previousDelimiter ( parent , index , single ) ) ||
106
+ ( ! containsWord ( parent , - 1 , index ) &&
107
+ siblingDelimiter ( parent , index , 1 , single ) ) ||
108
+ ( ! containsWord ( parent , index , parent . children . length ) &&
109
+ siblingDelimiter ( parent , index , - 1 , single ) ) ||
108
110
isWrapped ( parent , index , pairs )
109
111
) {
110
112
return true
@@ -116,92 +118,43 @@ function isLiteral(parent, index) {
116
118
// Check if the node in `parent` at `position` is enclosed by matching
117
119
// delimiters.
118
120
function isWrapped ( parent , position , delimiters ) {
119
- var previous = previousDelimiter ( parent , position , delimiters )
121
+ var previous = siblingDelimiter ( parent , position , - 1 , delimiters )
120
122
var next
121
123
122
124
if ( previous ) {
123
- next = nextDelimiter ( parent , position , delimiters [ toString ( previous ) ] )
125
+ next = siblingDelimiter ( parent , position , 1 , delimiters [ toString ( previous ) ] )
124
126
}
125
127
126
- return Boolean ( next )
128
+ return next
127
129
}
128
130
129
- // Find the previous delimiter before `position` in `parent`.
131
+ // Find the previous or next delimiter before or after `position` in `parent`.
130
132
// Returns the delimiter node when found.
131
- function previousDelimiter ( parent , position , delimiters ) {
132
- var siblings = parent . children
133
- var index = position
134
- var result
133
+ function siblingDelimiter ( parent , position , step , delimiters ) {
134
+ var index = position + step
135
+ var sibling
135
136
136
- while ( index -- ) {
137
- result = delimiterCheck ( siblings [ index ] , delimiters )
137
+ while ( index > - 1 && index < parent . children . length ) {
138
+ sibling = parent . children [ index ]
138
139
139
- if ( result === null ) {
140
- continue
140
+ if ( sibling . type === 'WordNode' || sibling . type === 'SourceNode' ) {
141
+ return
141
142
}
142
143
143
- return result
144
- }
145
-
146
- return null
147
- }
148
-
149
- // Find the next delimiter after `position` in `parent`.
150
- // Returns the delimiter node when found.
151
- function nextDelimiter ( parent , position , delimiters ) {
152
- var siblings = parent . children
153
- var index = position
154
- var length = siblings . length
155
- var result
156
-
157
- while ( ++ index < length ) {
158
- result = delimiterCheck ( siblings [ index ] , delimiters )
159
-
160
- if ( result === null ) {
161
- continue
144
+ if ( sibling . type !== 'WhiteSpaceNode' ) {
145
+ return toString ( sibling ) in delimiters && sibling
162
146
}
163
147
164
- return result
165
- }
166
-
167
- return null
168
- }
169
-
170
- // Check if `node` is in `delimiters`.
171
- function delimiterCheck ( node , delimiters ) {
172
- var type = node . type
173
-
174
- if ( type === 'WordNode' || type === 'SourceNode' ) {
175
- return false
176
- }
177
-
178
- if ( type === 'WhiteSpaceNode' ) {
179
- return null
148
+ index += step
180
149
}
181
-
182
- return toString ( node ) in delimiters ? node : false
183
- }
184
-
185
- // Check if there are word nodes before `position` in `parent`.
186
- function hasWordsBefore ( parent , position ) {
187
- return containsWord ( parent , 0 , position )
188
150
}
189
151
190
- // Check if there are word nodes before `position` in `parent`.
191
- function hasWordsAfter ( parent , position ) {
192
- return containsWord ( parent , position + 1 , parent . children . length )
193
- }
194
-
195
- // Check if parent contains word-nodes between `start` and `end`.
152
+ // Check if parent contains word-nodes between `start` and `end` (both
153
+ // excluding).
196
154
function containsWord ( parent , start , end ) {
197
- var siblings = parent . children
198
- var index = start - 1
199
-
200
- while ( ++ index < end ) {
201
- if ( siblings [ index ] . type === 'WordNode' ) {
155
+ while ( ++ start < end ) {
156
+ if ( parent . children [ start ] . type === 'WordNode' ) {
202
157
return true
203
158
}
204
159
}
205
-
206
- return false
207
160
}
0 commit comments