From dba197e453a2f3e90e38780f8faccc69dcf8cda8 Mon Sep 17 00:00:00 2001 From: John Koster Date: Wed, 30 Oct 2024 15:40:52 -0500 Subject: [PATCH] Parity with slot contents --- src/Tags/Partial.php | 15 +++++- src/View/Blade/Concerns/CompilesPartials.php | 2 +- .../AntlersComponents/PartialCompilerTest.php | 46 +++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/Tags/Partial.php b/src/Tags/Partial.php index 2732b92839..061ea26966 100644 --- a/src/Tags/Partial.php +++ b/src/Tags/Partial.php @@ -2,6 +2,8 @@ namespace Statamic\Tags; +use Illuminate\Support\HtmlString; + class Partial extends Tags { public function wildcard($tag) @@ -21,7 +23,7 @@ protected function render($partial) $variables = array_merge($this->context->all(), $this->params->all(), [ '__frontmatter' => $this->params->all(), - 'slot' => $this->isPair ? trim($this->parse()) : null, + 'slot' => $this->isPair ? $this->getSlotContent() : null, ]); return view($this->viewName($partial), $variables) @@ -29,6 +31,17 @@ protected function render($partial) ->render(); } + protected function getSlotContent() + { + $content = trim($this->parse()); + + if ($this->isAntlersBladeComponent()) { + return new HtmlString($content); + } + + return $content; + } + protected function shouldRender(): bool { if ($this->params->has('when')) { diff --git a/src/View/Blade/Concerns/CompilesPartials.php b/src/View/Blade/Concerns/CompilesPartials.php index a21407fbe3..c265501b50 100644 --- a/src/View/Blade/Concerns/CompilesPartials.php +++ b/src/View/Blade/Concerns/CompilesPartials.php @@ -89,7 +89,7 @@ protected function compilePartial(ComponentNode $component): string $injectedParam->setName($name); $injectedParam->type = ParameterType::DynamicVariable; - $injectedParam->value = '\Illuminate\Support\Facades\Blade::render($'.$hoistedVarName.', get_defined_vars())'; + $injectedParam->value = 'new \Illuminate\Support\HtmlString(\Illuminate\Support\Facades\Blade::render($'.$hoistedVarName.', get_defined_vars()))'; $hoistedSet .= Str::swap([ '$varName' => $hoistedVarName, diff --git a/tests/View/Blade/AntlersComponents/PartialCompilerTest.php b/tests/View/Blade/AntlersComponents/PartialCompilerTest.php index 6b0886970e..6e54d90025 100644 --- a/tests/View/Blade/AntlersComponents/PartialCompilerTest.php +++ b/tests/View/Blade/AntlersComponents/PartialCompilerTest.php @@ -333,4 +333,50 @@ public function it_compiles_nested_self_closing_partial_tags() Str::squish(Blade::render($template)) ); } + + #[Test] + public function slot_content_does_not_need_to_be_manually_escaped() + { + $this->withFakeViews(); + + $partial = <<<'BLADE' +Partial Start {{ $slot }} Partial End +BLADE; + + $this->viewShouldReturnRaw('the_partial', $partial, 'blade.php'); + + $template = <<<'BLADE' + +I am the slot content! + +BLADE; + + $this->assertSame( + 'Partial Start I am the slot content! Partial End', + Blade::render($template), + ); + $partial = <<<'BLADE' +Header Start {{ $header }} Header End +Partial Start {{ $slot }} Partial End +BLADE; + + $this->viewShouldReturnRaw('the_partial', $partial, 'blade.php'); + + $template = <<<'BLADE' + +I am the header! +I am the slot content! + +BLADE; + + $expected = <<<'EXPECTED' +Header Start I am the header! Header End +Partial Start I am the slot content! Partial End +EXPECTED; + + $this->assertSame( + $expected, + Blade::render($template), + ); + } }