@@ -66,19 +66,22 @@ function getValueFromContext(exp, context) {
66
66
function extractElseif ( ifExps , sourcesInsideBlock ) {
67
67
var exps = [ ifExps ] ;
68
68
var sourcesInsideIf = [ ] ;
69
-
69
+ var otherIfCount = 0 ;
70
70
var start = 0 ;
71
- var i , len , source ;
72
-
73
- for ( i = 0 , len = sourcesInsideBlock . length ; i < len ; i += 1 ) {
74
- source = sourcesInsideBlock [ i ] ;
75
71
76
- if ( source . indexOf ( 'elseif' ) > - 1 || source === 'else' ) {
72
+ // eslint-disable-next-line complexity
73
+ forEach ( sourcesInsideBlock , function ( source , index ) {
74
+ if ( source . indexOf ( 'if' ) === 0 ) {
75
+ otherIfCount += 1 ;
76
+ } else if ( source === '/if' ) {
77
+ otherIfCount -= 1 ;
78
+ } else if ( ! otherIfCount && ( source . indexOf ( 'elseif' ) === 0 || source === 'else' ) ) {
77
79
exps . push ( source === 'else' ? [ 'true' ] : source . split ( ' ' ) . slice ( 1 ) ) ;
78
- sourcesInsideIf . push ( sourcesInsideBlock . slice ( start , i ) ) ;
79
- start = i + 1 ;
80
+ sourcesInsideIf . push ( sourcesInsideBlock . slice ( start , index ) ) ;
81
+ start = index + 1 ;
80
82
}
81
- }
83
+ } ) ;
84
+
82
85
sourcesInsideIf . push ( sourcesInsideBlock . slice ( start ) ) ;
83
86
84
87
return {
@@ -129,9 +132,9 @@ function handleEach(exps, sourcesInsideBlock, context) {
129
132
forEach ( collection , function ( item , key ) {
130
133
additionalContext [ additionalKey ] = key ;
131
134
additionalContext [ '@this' ] = item ;
132
- extend ( additionalContext , context ) ;
135
+ extend ( context , additionalContext ) ;
133
136
134
- result += compile ( sourcesInsideBlock . slice ( ) , additionalContext ) ;
137
+ result += compile ( sourcesInsideBlock . slice ( ) , context ) ;
135
138
} ) ;
136
139
137
140
return result ;
@@ -153,7 +156,7 @@ function handleWith(exps, sourcesInsideBlock, context) {
153
156
var additionalContext = { } ;
154
157
additionalContext [ alias ] = result ;
155
158
156
- return compile ( sourcesInsideBlock , extend ( additionalContext , context ) ) || '' ;
159
+ return compile ( sourcesInsideBlock , extend ( context , additionalContext ) ) || '' ;
157
160
}
158
161
159
162
/**
@@ -171,25 +174,6 @@ function extractSourcesInsideBlock(sources, start, end) {
171
174
return sourcesInsideBlock ;
172
175
}
173
176
174
- /**
175
- * Concatenate the strings between previous and next of the base string in place.
176
- * @param {Array.<string> } sources - array of sources
177
- * @param {number } index - index of base string
178
- * @private
179
- */
180
- function concatPrevAndNextString ( source , index ) {
181
- var start = Math . max ( index - 1 , 0 ) ;
182
- var end = Math . min ( index + 1 , source . length - 1 ) ;
183
- var deletedCount = end - start + 1 ;
184
- var result = source . splice ( start , deletedCount ) . join ( '' ) ;
185
-
186
- if ( deletedCount < 3 ) {
187
- source . splice ( start , 0 , '' , result ) ;
188
- } else {
189
- source . splice ( start , 0 , result ) ;
190
- }
191
- }
192
-
193
177
/**
194
178
* Handle block helper function
195
179
* @param {string } helperKeyword - helper keyword (ex. if, each, with)
@@ -200,37 +184,34 @@ function concatPrevAndNextString(source, index) {
200
184
*/
201
185
function handleBlockHelper ( helperKeyword , sourcesToEnd , context ) {
202
186
var executeBlockHelper = BLOCK_HELPERS [ helperKeyword ] ;
203
- var startBlockIndices = [ ] ;
204
- var helperCount = 0 ;
205
- var index = 0 ;
187
+ var helperCount = 1 ;
188
+ var startBlockIndex = 0 ;
189
+ var endBlockIndex ;
190
+ var index = startBlockIndex + EXPRESSION_INTERVAL ;
206
191
var expression = sourcesToEnd [ index ] ;
207
- var startBlockIndex ;
208
192
209
- do {
193
+ while ( helperCount && isString ( expression ) ) {
210
194
if ( expression . indexOf ( helperKeyword ) === 0 ) {
211
195
helperCount += 1 ;
212
- startBlockIndices . push ( index ) ;
213
196
} else if ( expression . indexOf ( '/' + helperKeyword ) === 0 ) {
214
197
helperCount -= 1 ;
215
- startBlockIndex = startBlockIndices . pop ( ) ;
216
-
217
- sourcesToEnd [ startBlockIndex ] = executeBlockHelper (
218
- sourcesToEnd [ startBlockIndex ] . split ( ' ' ) . slice ( 1 ) ,
219
- extractSourcesInsideBlock ( sourcesToEnd , startBlockIndex , index ) ,
220
- context
221
- ) ;
222
- concatPrevAndNextString ( sourcesToEnd , startBlockIndex ) ;
223
- index = startBlockIndex - EXPRESSION_INTERVAL ;
198
+ endBlockIndex = index ;
224
199
}
225
200
226
201
index += EXPRESSION_INTERVAL ;
227
202
expression = sourcesToEnd [ index ] ;
228
- } while ( helperCount && isString ( expression ) ) ;
203
+ }
229
204
230
205
if ( helperCount ) {
231
206
throw Error ( helperKeyword + ' needs {{/' + helperKeyword + '}} expression.' ) ;
232
207
}
233
208
209
+ sourcesToEnd [ startBlockIndex ] = executeBlockHelper (
210
+ sourcesToEnd [ startBlockIndex ] . split ( ' ' ) . slice ( 1 ) ,
211
+ extractSourcesInsideBlock ( sourcesToEnd , startBlockIndex , endBlockIndex ) ,
212
+ context
213
+ ) ;
214
+
234
215
return sourcesToEnd ;
235
216
}
236
217
0 commit comments