Skip to content

Commit

Permalink
Corrects issue compiling nested self-closing components inside partials
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Oct 30, 2024
1 parent 5d6fa5d commit 02fe069
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/View/Blade/Concerns/CompilesComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ protected function extractNoResults(ComponentNode $componentNode): array
}

if ($node instanceof ComponentNode) {
if ($node->isClosedBy === null || $node->isSelfClosing) {
$newContent .= $node->content;
} else {
$newContent .= $node->outerDocumentContent;
}
$newContent .= $this->getComponentContent($node);
} elseif ($node instanceof LiteralNode) {
$newContent .= $node->unescapedContent;
}
Expand Down
2 changes: 1 addition & 1 deletion src/View/Blade/Concerns/CompilesPartials.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function extractSlots(ComponentNode $componentNode): array
}

if ($node instanceof ComponentNode) {
$newContent .= $node->outerDocumentContent;
$newContent .= $this->getComponentContent($node);
} elseif ($node instanceof LiteralNode) {
$newContent .= $node->unescapedContent;
}
Expand Down
9 changes: 9 additions & 0 deletions src/View/Blade/StatamicTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public function __construct()
});
}

protected function getComponentContent(ComponentNode $node): string
{
if ($node->isClosedBy === null || $node->isSelfClosing) {
return $node->content;
}

return $node->outerDocumentContent;
}

public static function adjustDynamicVariableName(string $variableName): string
{
return ltrim($variableName, '$');
Expand Down
52 changes: 52 additions & 0 deletions tests/View/Blade/AntlersComponents/PartialCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\View\Blade\AntlersComponents;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Str;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Tests\FakesViews;
Expand Down Expand Up @@ -281,4 +282,55 @@ public function frontmatter_populates_view_array()
Blade::render('<s:partial:the_partial name="A different name!" />')
);
}

#[Test]
public function it_compiles_nested_self_closing_partial_tags()
{
$this->withFakeViews();

$this->viewShouldReturnRaw('one', 'Just Some Text', 'blade.php');

$this->viewShouldReturnRaw('two', '{{ $slot }}', 'blade.php');

$template = <<<'BLADE'
<s:partial:one />
|
<s:partial:two>
Some More Text
|
<s:partial:one />
| After Nested Partial Call
</s:partial:two>
BLADE;

$this->assertSame(
'Just Some Text| Some More Text | Just Some Text | After Nested Partial Call',
Str::squish(Blade::render($template))
);

$template = <<<'BLADE'
<s:partial:one />
|
Partial Two Call One
<s:partial:two>
Some More Text
|
<s:partial:one />
| After Nested Partial Call
| Partial Two Call Two
|
<s:partial:two>
Some Even More Text
|
<s:partial:one />
| After Another Nested Partial Call
</s:partial:two>
</s:partial:two>
BLADE;

$this->assertSame(
'Just Some Text| Partial Two Call One Some More Text | Just Some Text | After Nested Partial Call | Partial Two Call Two | Some Even More Text | Just Some Text | After Another Nested Partial Call',
Str::squish(Blade::render($template))
);
}
}

0 comments on commit 02fe069

Please sign in to comment.