Skip to content

Commit 84d33fa

Browse files
committed
wip
1 parent 11b37da commit 84d33fa

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

packages/view/src/Elements/ViewComponentElement.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public function __construct(
2727
private readonly TempestViewCompiler $compiler,
2828
private readonly ViewComponent $viewComponent,
2929
array $attributes,
30-
)
31-
{
30+
) {
3231
$this->attributes = $attributes;
3332
$this->dataAttributes = arr($attributes)
3433
->filter(fn ($_, $key) => ! str_starts_with($key, ':'))
@@ -81,10 +80,6 @@ public function getSlot(string $name = 'slot'): ?Element
8180
$elements[] = $child;
8281
}
8382

84-
if ($elements === []) {
85-
return null;
86-
}
87-
8883
return new CollectionElement($elements);
8984
}
9085

@@ -142,7 +137,7 @@ public function compile(): string
142137

143138
$compiled = $compiled
144139
->prepend(
145-
// Add attributes to the current scope
140+
// Add attributes to the current scope
146141
'<?php $_previousAttributes = $attributes ?? null; ?>',
147142
sprintf('<?php $attributes = \Tempest\Support\arr(%s); ?>', var_export($this->dataAttributes, true)), // @mago-expect best-practices/no-debug-symbols Set the new value of $attributes for this view component
148143

@@ -151,7 +146,7 @@ public function compile(): string
151146
sprintf('<?php $slots = \Tempest\Support\arr(%s); ?>', var_export($slots, true)), // @mago-expect best-practices/no-debug-symbols Set the new value of $slots for this view component
152147
)
153148
->append(
154-
// Restore previous slots
149+
// Restore previous slots
155150
'<?php unset($slots); ?>',
156151
'<?php $slots = $_previousSlots ?? null; ?>',
157152
'<?php unset($_previousSlots); ?>',
@@ -161,25 +156,30 @@ public function compile(): string
161156
'<?php $attributes = $_previousAttributes ?? null; ?>',
162157
'<?php unset($_previousAttributes); ?>',
163158
)
164-
165159
// Compile slots
166160
->replaceRegex(
167-
regex: '/<x-slot\s*(name="(?<name>[\w-]+)")?/',
161+
regex: '/<x-slot\s*(name="(?<name>[\w-]+)")?((\s*\/>)|>(?<default>(.|\n)*?)<\/x-slot>)/',
168162
replace: function ($matches) {
169-
$name = $matches['name'] ?? 'slot';
163+
$name = $matches['name'] ?: 'slot';
170164

171165
$slot = $this->getSlot($name);
172166

173-
if ($slot === null && $this->environment->isProduction()) {
174-
// A slot doesn't have any content, so we'll comment it out.
175-
// This is to prevent DOM parsing errors (slots in <head> tags is one example, see #937)
176-
return ('<!--' . $matches[0] . '-->');
177-
} elseif($slot === null) {
178-
$default = $slotElement->match('/<x-slot.*?>((.|\n)*?)<\/x-slot>/') ?? '';
167+
$default = $matches['default'];
179168

169+
if ($slot === null) {
180170
if ($default) {
181-
$slot = new RawElement(tag: null, content: $default);
171+
return $default;
182172
}
173+
174+
// A slot doesn't have any content, so we'll comment it out.
175+
// This is to prevent DOM parsing errors (slots in <head> tags is one example, see #937)
176+
return $this->environment->isProduction() ? '' : ('<!--' . $matches[0] . '-->');
177+
}
178+
179+
$compiled = $slot->compile();
180+
181+
if (trim($compiled) === '') {
182+
return $default;
183183
}
184184

185185
return $slot->compile();
@@ -188,4 +188,4 @@ public function compile(): string
188188

189189
return $this->compiler->compile($compiled->toString());
190190
}
191-
}
191+
}

tests/Integration/View/ViewComponentTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -916,19 +916,19 @@ public function test_default_slot_value(): void
916916
<x-slot name="b">Overwritten B</x-slot>
917917
</x-test>'));
918918

919-
// $this->assertSnippetsMatch(<<<'HTML'
920-
// Overwritten
921-
// Default A
922-
// Overwritten B
923-
// HTML, $this->render('<x-test>
924-
// Overwritten
925-
// <x-slot name="b">Overwritten B</x-slot>
926-
// </x-test>'));
927-
//
928-
// $this->assertSnippetsMatch(<<<'HTML'
929-
// Default
930-
// Default A
931-
// Default B
932-
// HTML, $this->render('<x-test></x-test>'));
919+
$this->assertSnippetsMatch(<<<'HTML'
920+
Overwritten
921+
Default A
922+
Overwritten B
923+
HTML, $this->render('<x-test>
924+
Overwritten
925+
<x-slot name="b">Overwritten B</x-slot>
926+
</x-test>'));
927+
928+
$this->assertSnippetsMatch(<<<'HTML'
929+
Default
930+
Default A
931+
Default B
932+
HTML, $this->render('<x-test></x-test>'));
933933
}
934934
}

0 commit comments

Comments
 (0)