Skip to content

Commit e179b29

Browse files
committed
Fix indentation of nested partials
ProAI/laravel-handlebars#33
1 parent 4d1710e commit e179b29

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

src/Compiler.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,10 @@ public static function partial(Context $context, array $vars): string
267267
} else {
268268
$p = "'$p[0]'";
269269
}
270-
$sp = $context->tokens['partialind'] ? ", '{$context->tokens['partialind']}'" : '';
271-
return $context->ops['separator'] . static::getFuncName($context, 'p', $tag) . "\$cx, $p, $v[0],$pid$sp){$context->ops['separator']}";
270+
$sp = "(\$sp ?? '') . '{$context->tokens['partialind']}'";
271+
return $context->ops['separator'] .
272+
static::getFuncName($context, 'p', $tag) . "\$cx, $p, $v[0], $pid, $sp)" .
273+
$context->ops['separator'];
272274
}
273275

274276
/**

src/Partial.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
class Partial
66
{
7-
public static string $TMP_JS_FUNCTION_STR = "!!\aFuNcTiOn\a!!";
8-
97
/**
108
* Include all partials when using dynamic partials
119
*/
@@ -90,19 +88,16 @@ public static function compile(Context $context, string $template, string $name)
9088
$tmpContext->partialBlock = [];
9189
$tmpContext->partialStack[] = $name;
9290

93-
$code = Compiler::compileTemplate($tmpContext, str_replace('function', static::$TMP_JS_FUNCTION_STR, $template));
91+
$code = Compiler::compileTemplate($tmpContext, $template);
9492
$context->merge($tmpContext);
9593

9694
if (!$context->options->preventIndent) {
97-
$sp = ', $sp';
9895
$code = preg_replace('/^/m', "'{$context->ops['separator']}\$sp{$context->ops['separator']}'", $code);
99-
// callbacks inside partial should be aware of $sp
100-
$code = preg_replace('/\bfunction\s*\(([^\(]*?)\)\s*{/', 'function(\\1)use($sp){', $code);
101-
$code = preg_replace('/function\(\$cx, \$in, \$sp\)use\(\$sp\){/', 'function($cx, $in)use($sp){', $code);
102-
} else {
103-
$sp = '';
96+
// remove extra spaces before partial
97+
$code = preg_replace('/^\'\\.\\$sp\\.\'(\'\\.LR::p\\()/m', '$1', $code, 1);
98+
// add spaces after partial
99+
$code = preg_replace('/^(\'\\.LR::p\\(.+\\)\\.)(\'.+)/m', '$1\$sp.$2', $code, 1);
104100
}
105-
$code = str_replace(static::$TMP_JS_FUNCTION_STR, 'function', $code);
106-
return "function (\$cx, \$in{$sp}) {{$context->ops['op_start']}'$code'{$context->ops['op_end']}}";
101+
return "function (\$cx, \$in, \$sp) {{$context->ops['op_start']}'$code'{$context->ops['op_end']}}";
107102
}
108103
}

src/Runtime.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,8 @@ public static function merge($a, $b)
305305
*
306306
* @param string $p partial name
307307
* @param array<array|string|int>|string|int|null $v value to be the new context
308-
*
309308
*/
310-
public static function p(RuntimeContext $cx, string $p, $v, int $pid, $sp = ''): string
309+
public static function p(RuntimeContext $cx, string $p, $v, int $pid, string $sp): string
311310
{
312311
$pp = ($p === '@partial-block') ? $p . ($pid > 0 ? $pid : $cx->partialId) : $p;
313312

src/Validator.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,12 @@ protected static function token(array &$token, Context $context): string|array|n
532532
[$raw, $vars] = Parser::parse($token, $context);
533533

534534
// Handle spacing (standalone tags, partial indent)
535-
static::spacing($token, $context, (($token[Token::POS_OP] === '') || ($token[Token::POS_OP] === '&')) && (!isset($vars[0][0]) || ($vars[0][0] !== 'else')) || $context->options->ignoreStandalone);
535+
static::spacing($token, $context, ($token[Token::POS_OP] === '' || $token[Token::POS_OP] === '&') && (!isset($vars[0][0]) || $vars[0][0] !== 'else') || $context->options->ignoreStandalone);
536536

537-
$inlinepartial = static::inlinePartial($context, $vars);
538-
$partialblock = static::partialBlock($context, $vars);
537+
$inlinePartial = static::inlinePartial($context, $vars);
538+
$partialBlock = static::partialBlock($context, $vars);
539539

540-
if ($partialblock || $inlinepartial) {
540+
if ($partialBlock || $inlinePartial) {
541541
$context->stack = array_slice($context->stack, 0, -4);
542542
static::pushPartial($context, $context->currentToken[Token::POS_LOTHER] . $context->currentToken[Token::POS_LSPACE] . Token::toString($context->currentToken));
543543
$context->currentToken[Token::POS_LOTHER] = '';
@@ -765,13 +765,14 @@ protected static function spacing(array &$token, Context $context, bool $nost =
765765
|| ($lsp && !$token[Token::POS_ROTHER]) // final line
766766
)) {
767767
// handle partial
768+
$leftSpace = isset($lmatch[2]) ? ($lmatch[1] . $lmatch[2]) : '';
768769
if ($token[Token::POS_OP] === '>') {
769770
if (!$context->options->preventIndent) {
770771
$context->tokens['partialind'] = $token[Token::POS_LSPACECTL] ? '' : $ind;
771-
$token[Token::POS_LSPACE] = (isset($lmatch[2]) ? ($lmatch[1] . $lmatch[2]) : '');
772+
$token[Token::POS_LSPACE] = $leftSpace;
772773
}
773774
} else {
774-
$token[Token::POS_LSPACE] = (isset($lmatch[2]) ? ($lmatch[1] . $lmatch[2]) : '');
775+
$token[Token::POS_LSPACE] = $leftSpace;
775776
}
776777
$token[Token::POS_RSPACE] = $rmatch[3] ?? '';
777778
}

tests/RegressionTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,34 @@ public static function issueProvider(): array
18701870
'expected' => "a: A\nb: BOY!\nDONE",
18711871
],
18721872

1873+
[
1874+
'template' => <<<_tpl
1875+
<div>
1876+
{{> partialA}}
1877+
{{> partialB}}
1878+
</div>
1879+
_tpl,
1880+
'options' => new Options(
1881+
partials: [
1882+
'partialA' => "<div>\n Partial A\n {{> partialB}}\n</div>\n",
1883+
'partialB' => "<p>\n Partial B\n</p>\n",
1884+
],
1885+
),
1886+
'expected' => <<<_result
1887+
<div>
1888+
<div>
1889+
Partial A
1890+
<p>
1891+
Partial B
1892+
</p>
1893+
</div>
1894+
<p>
1895+
Partial B
1896+
</p>
1897+
</div>
1898+
_result,
1899+
],
1900+
18731901
[
18741902
'template' => "{{>test1}}\n {{>test1}}\nDONE\n",
18751903
'options' => new Options(

0 commit comments

Comments
 (0)