@@ -57,7 +57,7 @@ private function handleNode( DOMNode $node, array $data ) {
57
57
if ( !$ this ->isRemovedFromTheDom ( $ node ) ) {
58
58
if ( !$ this ->handleComponent ( $ node , $ data ) ) {
59
59
$ this ->handleAttributeBinding ( $ node , $ data );
60
- $ this ->handleIf ( $ node ->childNodes , $ data );
60
+ $ this ->handleConditionalNodes ( $ node ->childNodes , $ data );
61
61
62
62
foreach ( iterator_to_array ( $ node ->childNodes ) as $ childNode ) {
63
63
$ this ->handleNode ( $ childNode , $ data );
@@ -186,33 +186,64 @@ private function handleAttributeBinding( DOMElement $node, array $data ) {
186
186
}
187
187
}
188
188
189
+ private function handleIf (
190
+ DOMNode $ node ,
191
+ array $ data ,
192
+ bool $ previousIfCondition ,
193
+ array &$ nodesToRemove
194
+ ): bool {
195
+ $ conditionString = $ node ->getAttribute ( 'v-if ' );
196
+ $ node ->removeAttribute ( 'v-if ' );
197
+ $ condition = $ this ->app ->evaluateExpression ( $ conditionString , $ data );
198
+
199
+ if ( !$ condition ) {
200
+ $ nodesToRemove [] = $ node ;
201
+ }
202
+
203
+ return $ condition ;
204
+ }
205
+
206
+ private function handleElseIf (
207
+ DOMNode $ node ,
208
+ array $ data ,
209
+ bool $ previousIfCondition ,
210
+ array &$ nodesToRemove
211
+ ): bool {
212
+ $ conditionString = $ node ->getAttribute ( 'v-else-if ' );
213
+ $ node ->removeAttribute ( 'v-else-if ' );
214
+ if ( !$ previousIfCondition ) {
215
+ $ condition = $ this ->app ->evaluateExpression ( $ conditionString , $ data );
216
+
217
+ if ( !$ condition ) {
218
+ $ nodesToRemove [] = $ node ;
219
+ }
220
+ return $ condition ;
221
+ }
222
+ $ nodesToRemove [] = $ node ;
223
+ return $ previousIfCondition ;
224
+ }
225
+
189
226
/**
190
227
* @param DOMNodeList $nodes
191
228
* @param array $data
192
229
*/
193
- private function handleIf ( DOMNodeList $ nodes , array $ data ) {
230
+ private function handleConditionalNodes ( DOMNodeList $ nodes , array $ data ) {
194
231
// Iteration of iterator breaks if we try to remove items while iterating, so defer node
195
232
// removing until finished iterating.
196
233
$ nodesToRemove = [];
234
+ $ previousIfCondition = false ;
197
235
foreach ( $ nodes as $ node ) {
198
236
if ( $ this ->isTextNode ( $ node ) ) {
199
237
continue ;
200
238
}
201
239
202
240
/** @var DOMElement $node */
203
241
if ( $ node ->hasAttribute ( 'v-if ' ) ) {
204
- $ conditionString = $ node ->getAttribute ( 'v-if ' );
205
- $ node ->removeAttribute ( 'v-if ' );
206
- $ condition = $ this ->app ->evaluateExpression ( $ conditionString , $ data );
207
-
208
- if ( !$ condition ) {
209
- $ nodesToRemove [] = $ node ;
210
- }
211
-
212
- $ previousIfCondition = $ condition ;
242
+ $ previousIfCondition = $ this ->handleIf ( $ node , $ data , $ previousIfCondition , $ nodesToRemove );
243
+ } elseif ( $ node ->hasAttribute ( 'v-else-if ' ) ) {
244
+ $ previousIfCondition = $ this ->handleElseIf ( $ node , $ data , $ previousIfCondition , $ nodesToRemove );
213
245
} elseif ( $ node ->hasAttribute ( 'v-else ' ) ) {
214
246
$ node ->removeAttribute ( 'v-else ' );
215
-
216
247
if ( $ previousIfCondition ) {
217
248
$ nodesToRemove [] = $ node ;
218
249
}
0 commit comments