@@ -57,7 +57,7 @@ private function handleNode( DOMNode $node, array $data ) {
5757 if ( !$ this ->isRemovedFromTheDom ( $ node ) ) {
5858 if ( !$ this ->handleComponent ( $ node , $ data ) ) {
5959 $ this ->handleAttributeBinding ( $ node , $ data );
60- $ this ->handleIf ( $ node ->childNodes , $ data );
60+ $ this ->handleConditionalNodes ( $ node ->childNodes , $ data );
6161
6262 foreach ( iterator_to_array ( $ node ->childNodes ) as $ childNode ) {
6363 $ this ->handleNode ( $ childNode , $ data );
@@ -186,33 +186,64 @@ private function handleAttributeBinding( DOMElement $node, array $data ) {
186186 }
187187 }
188188
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+
189226 /**
190227 * @param DOMNodeList $nodes
191228 * @param array $data
192229 */
193- private function handleIf ( DOMNodeList $ nodes , array $ data ) {
230+ private function handleConditionalNodes ( DOMNodeList $ nodes , array $ data ) {
194231 // Iteration of iterator breaks if we try to remove items while iterating, so defer node
195232 // removing until finished iterating.
196233 $ nodesToRemove = [];
234+ $ previousIfCondition = false ;
197235 foreach ( $ nodes as $ node ) {
198236 if ( $ this ->isTextNode ( $ node ) ) {
199237 continue ;
200238 }
201239
202240 /** @var DOMElement $node */
203241 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 );
213245 } elseif ( $ node ->hasAttribute ( 'v-else ' ) ) {
214246 $ node ->removeAttribute ( 'v-else ' );
215-
216247 if ( $ previousIfCondition ) {
217248 $ nodesToRemove [] = $ node ;
218249 }
0 commit comments