Skip to content

Commit 3e39150

Browse files
authored
Merge pull request #44 from wmde/feat/support-v-else-if-20250702
Add support for v-else-if attributes
2 parents 691c841 + 14b675c commit 3e39150

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

src/Component.php

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/php/TemplatingTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,36 @@ public function testTemplateWithIfElseBlockAndNontruthfulCondition_ElseIsDisplay
192192
$this->assertSame( '<p><a>else</a></p>', $result );
193193
}
194194

195+
public function testTemplateWithElseIfBlockAndTruthfulCondition_IfAndElseRemoved() {
196+
$result = $this->createAndRender(
197+
'<p><a v-if="variable">if</a><a v-else-if="other_variable">else if</a>' .
198+
'<a v-else>else</a></p>',
199+
[ 'variable' => false, 'other_variable' => true ]
200+
);
201+
202+
$this->assertSame( '<p><a>else if</a></p>', $result );
203+
}
204+
205+
public function testTemplateWithElseIfBlockAndTruthfulCondition_OnlyFirstElseIfShows() {
206+
$result = $this->createAndRender(
207+
'<p><a v-if="variable">if</a><a v-else-if="other_variable">else if</a>' .
208+
'<a v-else-if="still_another">another else</a><a v-else>else</a></p>',
209+
[ 'variable' => false, 'other_variable' => true, 'still_another' => true ]
210+
);
211+
212+
$this->assertSame( '<p><a>else if</a></p>', $result );
213+
}
214+
215+
public function testTemplateWithElseIfBlockAndTruthfulIfAndElseIfCondition_IfElseRemoved() {
216+
$result = $this->createAndRender(
217+
'<p><a v-if="variable">if</a><a v-else-if="other_variable">else if</a>' .
218+
'<a v-else>else</a></p>',
219+
[ 'variable' => true, 'other_variable' => true ]
220+
);
221+
222+
$this->assertSame( '<p><a>if</a></p>', $result );
223+
}
224+
195225
public function testTemplateWithForLoopAndEmptyArrayToIterate_NotRendered() {
196226
$result = $this->createAndRender( '<p><a v-for="item in list"></a></p>', [ 'list' => [] ] );
197227

0 commit comments

Comments
 (0)