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
10 changes: 8 additions & 2 deletions packages/view/src/Attributes/ExpressionAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,20 @@ public static function render(string $name, mixed $value): string
return str($name)->kebab()->toString();
}

if (! $value) {
if ($value === false || $value === null) {
return '';
}

$resolvedValue = self::resolveValue($value);

if ($resolvedValue === '') {
return '';
}

return sprintf(
'%s="%s"',
str($name)->kebab(),
ExpressionAttribute::resolveValue($value),
$resolvedValue,
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/view/src/Elements/ViewComponentElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(
$this->expressionAttributes = arr($attributes)
->filter(fn (string $_, string $key) => str_starts_with($key, ':'))
->filter(fn (string $_, string $key) => ! in_array($key, [':if', ':else', ':elseif', ':foreach', ':forelse'], strict: true))
->mapWithKeys(fn (string $value, string $key) => yield str($key)->camel()->ltrim(':')->toString() => $value ?: 'true');
->mapWithKeys(fn (string $value, string $key) => yield str($key)->camel()->ltrim(':')->toString() => $value === '' ? 'true' : $value);

$this->scopedVariables = arr();
}
Expand Down Expand Up @@ -448,7 +448,7 @@ private function exportAttributesArray(): string
$isExpression = isset($this->expressionAttributes[$camelKey]);

$entries[] = $isExpression
? sprintf("'%s' => %s", $key, $value ?: 'true')
? sprintf("'%s' => %s", $key, $value === '' ? 'true' : $value)
: sprintf("'%s' => %s", $key, ViewObjectExporter::exportValue($value));
}

Expand Down
35 changes: 34 additions & 1 deletion tests/Integration/View/TempestViewRendererDataPassingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ public function test_expression_attribute_on_view_component(): void
);
}

public function test_zero_expression_attribute_literal_on_view_component(): void
{
$this->view->registerViewComponent(
'x-link',
<<<'HTML'
<a :href="$href" :data-start-percent="$attributes->get('data-start-percent')"><x-slot/></a>
HTML,
);

$this->assertSame(
'<a href="0" data-start-percent="0">a</a>',
$this->view->render(
<<<'HTML'
<x-link :href="0" :data-start-percent="0">a</x-link>
HTML,
),
);
}

public function test_normal_attribute_on_view_component(): void
{
// <x-button href="http://…" /> 💯 always pass as variable, never set directly as attribute
Expand Down Expand Up @@ -270,7 +289,7 @@ public function test_boolean_attributes(): void

#[TestWith(['false'])]
#[TestWith(['null'])]
#[TestWith(['0'])]
#[TestWith(["''"])]
#[TestWith(['$show'])]
public function test_falsy_bool_attribute(mixed $value): void
{
Expand All @@ -283,6 +302,20 @@ public function test_falsy_bool_attribute(mixed $value): void
HTML, $html);
}

#[TestWith([0])]
#[TestWith([0.0])]
#[TestWith(['0'])]
public function test_zero_expression_attribute_value_is_rendered(mixed $value): void
{
$html = $this->view->render(<<<'HTML'
<div :data-start-percent="$value"></div>
HTML, value: $value);

$this->assertStringEqualsStringIgnoringLineEndings(<<<'HTML'
<div data-start-percent="0"></div>
HTML, $html);
}

#[TestWith(['true'])]
#[TestWith(['$show'])]
public function test_truthy_bool_attribute(mixed $value): void
Expand Down
Loading