Skip to content

Commit e87fc1f

Browse files
committedMar 31, 2025·
Fix parsing block parameters with extra whitespace
zordius/lightncandy#371
1 parent 128c46a commit e87fc1f

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed
 

‎src/Parser.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ protected static function advancedVariable(array $vars, Context $context, string
235235

236236
// handle |...|
237237
if (preg_match(SafeString::IS_BLOCKPARAM_SEARCH, $var, $matched)) {
238-
$ret[static::BLOCKPARAM] = explode(' ', $matched[1]);
238+
$ret[static::BLOCKPARAM] = preg_split('/\s+/', trim($matched[1]));
239239
continue;
240240
}
241241

@@ -366,8 +366,8 @@ protected static function analyze(string $token, Context $context): array
366366
if ($quote === 0 && $stack > 0 && preg_match('/(.+=)*(\\(+)/', $t, $m) && !$quotes) {
367367
$stack += strlen($m[2]);
368368
}
369-
// end an argument when end with expected character
370-
if (substr($t, -1, 1) === $expect) {
369+
// end an argument when token ends with expected character (ignoring whitespace + first instance)
370+
if (substr($t, -1, 1) === $expect && !preg_match('/^\s+' . preg_quote($expect) . '$/', $prev)) {
371371
if ($stack > 0 && !$quotes) {
372372
preg_match('/(\\)+)$/', $t, $matchedq);
373373
$stack -= isset($matchedq[0]) ? strlen($matchedq[0]) : 1;

‎tests/RegressionTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,32 @@ public static function issueProvider(): array
13981398
'expected' => '3',
13991399
],
14001400

1401+
[
1402+
'id' => 371,
1403+
'template' => <<<_tpl
1404+
{{#myeach '[{"a":"ayy", "b":"bee"},{"a":"zzz", "b":"ccc"}]' as | newContext index | }}
1405+
Foo {{newContext.a}} {{index}}
1406+
{{/myeach}}
1407+
_tpl,
1408+
'options' => new Options(
1409+
helpers: [
1410+
'myeach' => function ($context, HelperOptions $options) {
1411+
$theArray = json_decode($context, true);
1412+
if (!is_array($theArray)) {
1413+
return '';
1414+
}
1415+
1416+
$ret = '';
1417+
foreach ($theArray as $i => $value) {
1418+
$ret .= $options->fn([], ['blockParams' => [$value, $i]]);
1419+
}
1420+
return $ret;
1421+
},
1422+
],
1423+
),
1424+
'expected' => "Foo ayy 0\nFoo zzz 1\n",
1425+
],
1426+
14011427
[
14021428
'template' => '{{#each . as |v k|}}#{{k}}{{/each}}',
14031429
'data' => ['a' => [], 'c' => []],

0 commit comments

Comments
 (0)
Please sign in to comment.