Skip to content

Commit a3e543a

Browse files
[5.x] Correct issue with nested noparse and partials (#11801)
1 parent 4a797bb commit a3e543a

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/View/Antlers/Language/Runtime/RuntimeParser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ protected function isIgnitionInstalled()
314314
return class_exists(ViewException::class) || class_exists('Spatie\LaravelIgnition\Exceptions\ViewException');
315315
}
316316

317+
protected function shouldCacheRenderNodes($text)
318+
{
319+
return ! str_contains($text, '/noparse');
320+
}
321+
317322
/**
318323
* Parses and renders the input text, with the provided runtime data.
319324
*
@@ -350,7 +355,7 @@ protected function renderText($text, $data = [])
350355
$parseText = $this->sanitizePhp($text);
351356
$cacheSlug = md5($parseText);
352357

353-
if (! array_key_exists($cacheSlug, self::$standardRenderNodeCache)) {
358+
if (! array_key_exists($cacheSlug, self::$standardRenderNodeCache) || ! $this->shouldCacheRenderNodes($text)) {
354359
$this->documentParser->setIsVirtual($this->view == '');
355360

356361
if (strlen($this->view) > 0) {

tests/Antlers/Runtime/NoparseTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
namespace Tests\Antlers\Runtime;
44

5+
use Statamic\View\Antlers\Language\Runtime\GlobalRuntimeState;
6+
use Statamic\View\Antlers\Language\Runtime\NodeProcessor;
57
use Statamic\View\Antlers\Language\Utilities\StringUtilities;
68
use Tests\Antlers\ParserTestCase;
9+
use Tests\FakesViews;
710

811
class NoparseTest extends ParserTestCase
912
{
13+
use FakesViews;
14+
1015
public function test_noparse_ignores_braces_entirely()
1116
{
1217
$template = <<<'EOT'
@@ -142,4 +147,39 @@ public function test_multiple_noparse_regions()
142147

143148
$this->assertSame($expected, StringUtilities::normalizeLineEndings(trim($this->renderString($template, ['title' => 'the title']))));
144149
}
150+
151+
public function test_noparse_in_nested_partials_renders_correctly()
152+
{
153+
$template = <<<'EOT'
154+
{{ partial:partial_a }}
155+
{{ partial:partial_a }}
156+
{{ noparse }}inside noparse{{ /noparse }}
157+
{{ /partial:partial_a }}
158+
{{ /partial:partial_a }}
159+
160+
{{ partial:partial_a }}
161+
{{ partial:partial_a }}
162+
{{ noparse }}inside noparse{{ /noparse }}
163+
{{ /partial:partial_a }}
164+
{{ /partial:partial_a }}
165+
166+
{{ partial:partial_a }}
167+
{{ partial:partial_a }}
168+
{{ noparse }}inside noparse{{ /noparse }}
169+
{{ /partial:partial_a }}
170+
{{ /partial:partial_a }}
171+
EOT;
172+
173+
GlobalRuntimeState::$peekCallbacks[] = function ($processor, $nodes) {
174+
NodeProcessor::$break = true;
175+
};
176+
177+
$this->withFakeViews();
178+
$this->viewShouldReturnRaw('partial_a', '{{ slot }}');
179+
180+
$actual = StringUtilities::normalizeLineEndings(trim($this->renderString($template)));
181+
182+
$occurrences = substr_count($actual, 'inside noparse');
183+
$this->assertEquals(3, $occurrences, "Expected 'inside noparse' to appear exactly 3 times");
184+
}
145185
}

0 commit comments

Comments
 (0)