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
2 changes: 2 additions & 0 deletions src/Illuminate/Support/Facades/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
* @method static string normalizeQueryString(string|null $qs)
* @method static void enableHttpMethodParameterOverride()
* @method static bool getHttpMethodParameterOverride()
* @method static void setAllowedHttpMethodOverride(array|null $methods)
* @method static array|null getAllowedHttpMethodOverride()
* @method static bool hasPreviousSession()
* @method static void setSession(\Symfony\Component\HttpFoundation\Session\SessionInterface $session)
* @method static array getClientIps()
Expand Down
17 changes: 16 additions & 1 deletion src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,26 @@ 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;

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

return trim($string);
Expand Down
19 changes: 14 additions & 5 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-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-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,10 +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-0="0" test-0-string="0" data-test-empty-string="" data-test-whitespace-string=" " value=" " alt=""', (string) $bag);
$bag = (new ComponentAttributeBag)
->merge([
'test-extract-1' => 'extracted-1',
Expand Down Expand Up @@ -151,7 +160,7 @@ public function testItMakesAnExceptionForAlpineXdata()
'x-data' => true,
]);

$this->assertSame('required="required" x-data=""', (string) $bag);
$this->assertSame('required x-data=""', (string) $bag);
}

public function testItMakesAnExceptionForLivewireWireAttributes()
Expand Down
Loading