Skip to content
Draft
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
13 changes: 12 additions & 1 deletion src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,22 @@ public function __toString()
continue;
}

// Empty "alt" explicitly marks an image as decorative; empty "value" and "data-*"
// are valid. For everything else we don't want to render the attribute at all.
if (!in_array($key, ['alt', 'value']) && !str_starts_with($key, 'data') && (is_string($value) && trim($value) === '')) {
continue;
}

if ($value === true) {
$value = $key === 'x-data' || str_starts_with($key, 'wire:') ? '' : $key;
}

$string .= ' '.$key.'="'.str_replace('"', '\\"', trim($value)).'"';
// Same as above. We want everything but data and value to be trimmed
$value = !str_starts_with($key, 'data') && $key !== 'value'
? trim($value)
: $value;

$string .= ' '.$key.'="'.str_replace('"', '\\"', $value).'"';
}

return trim($string);
Expand Down
16 changes: 13 additions & 3 deletions tests/View/ViewComponentAttributeBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ public function testAttributeRetrieval()
'test-0' => 0,
'test-0-string' => '0',
'test-empty-string' => '',
'test-whitespace-string' => ' ',
'data-test-empty-string' => '',
'data-test-whitespace-string' => ' ',
'value' => ' ',
'alt' => '',
]);

$this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" test-empty-string=""', (string) $bag);
$this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" test-empty-string=""', (string) $bag->merge());
$this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" data-test-empty-string="" data-test-whitespace-string=" " value=" " alt=""', (string) $bag);
$this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" data-test-empty-string="" data-test-whitespace-string=" " value=" " alt=""', (string) $bag->merge());

$bag = (new ComponentAttributeBag)
->merge([
Expand All @@ -72,9 +77,14 @@ public function testAttributeRetrieval()
'test-0' => 0,
'test-0-string' => '0',
'test-empty-string' => '',
'test-whitespace-string' => ' ',
'data-test-empty-string' => '',
'data-test-whitespace-string' => ' ',
'value' => ' ',
'alt' => '',
]);

$this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" test-empty-string=""', (string) $bag);
$this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" data-test-empty-string="" data-test-whitespace-string=" " value=" " alt=""', (string) $bag);

$bag = (new ComponentAttributeBag)
->merge([
Expand Down
Loading