Skip to content

Commit e2ea984

Browse files
committed
refactor: improved boolean attribute rendering
1 parent 3aed587 commit e2ea984

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/Illuminate/View/ComponentAttributeBag.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,26 @@ public function __toString()
491491
continue;
492492
}
493493

494+
// Empty "alt" explicitly marks an image as decorative; empty "value" and "data-*"
495+
// are valid. For everything else we don't want to render the attribute at all.
496+
if (!in_array($key, ['alt', 'value']) && !str_starts_with($key, 'data') && (is_string($value) && trim($value) === '')) {
497+
continue;
498+
}
499+
494500
if ($value === true) {
495501
$value = $key === 'x-data' || str_starts_with($key, 'wire:') ? '' : $key;
496502
}
497503

498-
$string .= ' '.$key.'="'.str_replace('"', '\\"', trim($value)).'"';
504+
// Same as above. We want everything but data and value to be trimmed
505+
$value = !str_starts_with($key, 'data') && $key !== 'value'
506+
? trim($value)
507+
: $value;
508+
509+
if($key === $value) {
510+
$string .= ' '.$key;
511+
} else {
512+
$string .= ' '.$key.'="'.str_replace('"', '\\"', $value).'"';
513+
}
499514
}
500515

501516
return trim($string);

tests/View/ViewComponentAttributeBagTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ public function testAttributeRetrieval()
5151
'test-0' => 0,
5252
'test-0-string' => '0',
5353
'test-empty-string' => '',
54+
'test-whitespace-string' => ' ',
55+
'data-test-empty-string' => '',
56+
'data-test-whitespace-string' => ' ',
57+
'value' => ' ',
58+
'alt' => '',
5459
]);
5560

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

5964
$bag = (new ComponentAttributeBag)
6065
->merge([
@@ -72,10 +77,14 @@ public function testAttributeRetrieval()
7277
'test-0' => 0,
7378
'test-0-string' => '0',
7479
'test-empty-string' => '',
80+
'test-whitespace-string' => ' ',
81+
'data-test-empty-string' => '',
82+
'data-test-whitespace-string' => ' ',
83+
'value' => ' ',
84+
'alt' => '',
7585
]);
7686

77-
$this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" test-empty-string=""', (string) $bag);
78-
87+
$this->assertSame('test-string="ok" test-true test-0="0" test-0-string="0" data-test-empty-string="" data-test-whitespace-string=" " value=" " alt=""', (string) $bag);
7988
$bag = (new ComponentAttributeBag)
8089
->merge([
8190
'test-extract-1' => 'extracted-1',
@@ -151,7 +160,7 @@ public function testItMakesAnExceptionForAlpineXdata()
151160
'x-data' => true,
152161
]);
153162

154-
$this->assertSame('required="required" x-data=""', (string) $bag);
163+
$this->assertSame('required x-data=""', (string) $bag);
155164
}
156165

157166
public function testItMakesAnExceptionForLivewireWireAttributes()

0 commit comments

Comments
 (0)