Skip to content

Commit bead5a5

Browse files
authored
fix(view): hyphens in slot names (#1129)
1 parent dcc2401 commit bead5a5

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/Tempest/View/src/Elements/ViewComponentElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function compile(): string
155155
)
156156
// Compile slots
157157
->replaceRegex(
158-
regex: '/<x-slot\s*(name="(?<name>\w+)")?((\s*\/>)|><\/x-slot>)/',
158+
regex: '/<x-slot\s*(name="(?<name>[\w-]+)")?((\s*\/>)|><\/x-slot>)/',
159159
replace: function ($matches) {
160160
$name = $matches['name'] ?: 'slot';
161161

tests/Integration/View/ViewComponentTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,55 @@ public function test_renders_minified_html_with_void_elements(): void
703703
HTML, $html);
704704
}
705705

706+
public function test_multiple_instances_of_custom_component_using_slots(): void
707+
{
708+
$this->registerViewComponent('x-foo-bar', 'FOO-BAR');
709+
710+
$this->registerViewComponent('x-test', <<<'HTML'
711+
<div>
712+
<x-foo-bar />
713+
<x-slot name="test" />
714+
</div>
715+
HTML);
716+
717+
$html = $this->render(<<<'HTML'
718+
<x-test>
719+
<x-slot name="test">
720+
<x-foo-bar />
721+
</x-slot>
722+
</x-test>
723+
HTML);
724+
725+
$this->assertSnippetsMatch(<<<'HTML'
726+
<div>FOO-BAR
727+
FOO-BAR
728+
</div>
729+
HTML, $html);
730+
}
731+
732+
public function test_slots_with_hyphens(): void
733+
{
734+
$this->registerViewComponent('x-test', <<<'HTML'
735+
<div>
736+
<x-slot name="test-slot" />
737+
</div>
738+
HTML);
739+
740+
$html = $this->render(<<<'HTML'
741+
<x-test>
742+
<x-slot name="test-slot">
743+
Hi
744+
</x-slot>
745+
</x-test>
746+
HTML);
747+
748+
$this->assertSnippetsMatch(<<<'HTML'
749+
<div>
750+
Hi
751+
</div>
752+
HTML, $html);
753+
}
754+
706755
private function assertSnippetsMatch(string $expected, string $actual): void
707756
{
708757
$expected = str_replace([PHP_EOL, ' '], '', $expected);

0 commit comments

Comments
 (0)