Skip to content

Commit 8b2348f

Browse files
committed
Fix inline partials leaking out of an else block
1 parent 5526b37 commit 8b2348f

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

src/HelperOptions.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,7 @@ public function fn(mixed $context = Scope::Use, mixed $data = null): string
7171
}
7272
return '';
7373
}
74-
$cx = $this->cx;
75-
// Save inlinePartials so that any {{#* inline}} partials registered inside the block body
76-
// don't leak out after fn() returns. The spec requires inline partials to be
77-
// block-scoped. PHP copy-on-write makes this assignment cheap when no inline partials are registered.
78-
$savedInlinePartials = $cx->inlinePartials;
79-
$ret = $this->invokeBlock($this->cb, $context, $data);
80-
$cx->inlinePartials = $savedInlinePartials;
81-
return $ret;
74+
return $this->invokeBlock($this->cb, $context, $data);
8275
}
8376

8477
public function inverse(mixed $context = Scope::Use, mixed $data = null): string
@@ -95,6 +88,10 @@ public function inverse(mixed $context = Scope::Use, mixed $data = null): string
9588
private function invokeBlock(\Closure $closure, mixed $context, mixed $data): string
9689
{
9790
$cx = $this->cx;
91+
// Save inlinePartials so that any {{#* inline}} partials registered inside the block body
92+
// don't leak out after it returns. The spec requires inline partials to be
93+
// block-scoped. PHP copy-on-write makes this assignment cheap when no inline partials are registered.
94+
$savedInlinePartials = $cx->inlinePartials;
9895
$scope = $this->scope;
9996
// Skip depths push when the caller explicitly passes the current scope (equivalent to
10097
// HBS.js options.fn(this) / options.inverse(this)), since the scope level isn't changing.
@@ -129,6 +126,7 @@ private function invokeBlock(\Closure $closure, mixed $context, mixed $data): st
129126
if ($savedFrame !== null) {
130127
$cx->frame = $savedFrame;
131128
}
129+
$cx->inlinePartials = $savedInlinePartials;
132130
return $ret;
133131
}
134132

tests/RegressionTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ public static function helperProvider(): array
240240

241241
[
242242
'desc' => 'LNC#114 - inverse block helpers',
243-
'template' => '{{^myeach .}}OK:{{.}},{{else}}NOT GOOD{{/myeach}}',
243+
'template' => '{{^myeach .}}bad:{{.}} {{else}}OK:{{.}} {{/myeach}}',
244244
'helpers' => ['myeach' => $myEach],
245-
'data' => [1, 'foo', 3, 'bar'],
246-
'expected' => 'NOT GOODNOT GOODNOT GOODNOT GOOD',
245+
'data' => [1, 3.5, 'foo', true, false, null],
246+
'expected' => 'OK:1 OK:3.5 OK:foo OK:true OK:false OK: ',
247247
],
248248

249249
[
@@ -2073,6 +2073,11 @@ public static function sectionProvider(): array
20732073
'data' => ['section' => ['x' => 1]],
20742074
'expected' => 'INSIDEBEFORE',
20752075
],
2076+
[
2077+
'desc' => 'inline partials registered inside an else block do not leak out after the section ends',
2078+
'template' => '{{#* inline "p"}}BEFORE{{/inline}}{{#foo}}{{else}}{{#* inline "p"}}INSIDE{{/inline}}{{> p}}{{/foo}}{{> p}}',
2079+
'expected' => 'INSIDEBEFORE',
2080+
],
20762081
];
20772082
}
20782083

0 commit comments

Comments
 (0)