Skip to content

Commit ff199aa

Browse files
committed
Fix nested else if validation
zordius/lightncandy#313
1 parent d357604 commit ff199aa

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Validator.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,15 @@ protected static function doElse(Context $context, array $vars)
579579
}
580580

581581
if (isset($vars[1][0])) {
582-
$token = $context->currentToken;
582+
$if = $vars[1][0];
583+
$innerTag = $context->currentToken[Token::POS_INNERTAG];
584+
$condition = preg_replace("/^\\s*else\\s+{$if}\\s*/", '', $innerTag);
583585
$context->currentToken[Token::POS_INNERTAG] = 'else';
584-
$context->currentToken[Token::POS_RSPACE] = "{{#{$vars[1][0]} " . preg_replace('/^\\s*else\\s+' . $vars[1][0] . '\\s*/', '', $token[Token::POS_INNERTAG]) . '}}' . $context->currentToken[Token::POS_RSPACE];
585-
array_unshift($context->elseLvl[0], $vars[1][0]);
586+
$context->currentToken[Token::POS_RSPACE] = "{{#$if $condition}}" . $context->currentToken[Token::POS_RSPACE];
587+
if (!$context->elseLvl) {
588+
array_unshift($context->elseLvl, []);
589+
}
590+
array_unshift($context->elseLvl[0], $if);
586591
$context->elseChain = true;
587592
}
588593

tests/RegressionTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,27 @@ public static function issueProvider(): array
13661366
'expected' => 'SOME-TEXT-foobar...',
13671367
],
13681368

1369+
[
1370+
'id' => 313,
1371+
'template' => <<<_tpl
1372+
{{#if conditionA}}
1373+
{{#if conditionA1}}
1374+
Do something then do more stuff conditionally
1375+
{{#if conditionA1.x}}
1376+
Do something here
1377+
{{else if conditionA1.y}}
1378+
Do something else here
1379+
{{/if}}
1380+
{{/if}}
1381+
{{else if conditionB}}
1382+
Do something else
1383+
{{else}}
1384+
Finally, do this last thing if all else fails
1385+
{{/if}}
1386+
_tpl,
1387+
'expected' => " Finally, do this last thing if all else fails\n",
1388+
],
1389+
13691390
[
13701391
'id' => 315,
13711392
'template' => '{{#each foo}}#{{@key}}({{@index}})={{.}}-{{moo}}-{{@irr}}{{/each}}',

0 commit comments

Comments
 (0)