Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Folder/Foldable.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ protected function setupSlots(): void

foreach ($this->node->children as $child) {
if ($child instanceof SlotNode) {
if (! $this->hasActualContent($child->children)) {
continue;
}

$placeholder = 'BLAZE_PLACEHOLDER_' . $this->placeholderIndex++ . '_';

$this->slotByPlaceholder[$placeholder] = $child;
Expand All @@ -121,7 +125,7 @@ protected function setupSlots(): void
}

// Synthesize a default slot from loose content when there's not an explicit one
if ($looseContent && ! isset($slots['slot'])) {
if ($this->hasActualContent($looseContent) && ! isset($slots['slot'])) {
$placeholder = 'BLAZE_PLACEHOLDER_' . $this->placeholderIndex++ . '_';

$defaultSlot = new SlotNode(
Expand All @@ -146,6 +150,20 @@ protected function setupSlots(): void
$this->renderable->children = $slots;
}

/**
* Determine whether a list of nodes contains anything besides whitespace-only text.
*/
protected function hasActualContent(array $nodes): bool
{
foreach ($nodes as $node) {
if (! $node instanceof TextNode || trim($node->content) !== '') {
return true;
}
}

return false;
}

/**
* Convert [BLAZE_ATTR:...] markers into conditional PHP for dynamic attributes.
*/
Expand Down
34 changes: 34 additions & 0 deletions tests/Folder/FoldableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,37 @@
expect((new Foldable($node, '', app(BladeRenderer::class), app(BladeService::class)))->fold())
->toBe('<div></div>');
});

test('does not synthesize a default slot for whitespace-only loose content', function () {
$node = app(Parser::class)->parse('<x-card> </x-card>')[0];

mock(BladeRenderer::class)
->expects('render')
->once()->withArgs(function (ComponentNode $node) {
expect($node->render())->toBe('<x-card></x-card>');

return true;
})
->andReturn('<div></div>');

$output = (new Foldable($node, '', app(BladeRenderer::class), app(BladeService::class)))->fold();

expect($output)->toBe('<div></div>');
});

test('does not synthesize a placeholder for a whitespace-only explicit slot', function () {
$node = app(Parser::class)->parse('<x-card><x-slot:footer> </x-slot></x-card>')[0];

mock(BladeRenderer::class)
->expects('render')
->once()->withArgs(function (ComponentNode $node) {
expect($node->render())->toBe('<x-card></x-card>');

return true;
})
->andReturn('<div></div>');

$output = (new Foldable($node, '', app(BladeRenderer::class), app(BladeService::class)))->fold();

expect($output)->toBe('<div></div>');
});
Loading