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: 10 additions & 10 deletions src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ public function merge(array $attributeDefaults = [])
$attributes = [];

foreach ($this->attributes as $key => $value) {
if ($value === true) {
$attributes[$key] = $key;

continue;
}

if ($key !== 'class') {
$attributes[$key] = $value;

Expand All @@ -126,7 +120,7 @@ public function merge(array $attributeDefaults = [])
));
}

return new static(array_merge($attributeDefaults, array_filter($attributes)));
return new static(array_merge($attributeDefaults, $attributes));
}

/**
Expand Down Expand Up @@ -226,9 +220,15 @@ public function __toString()
$string = '';

foreach ($this->attributes as $key => $value) {
$string .= $value === true
? ' '.$key
: ' '.$key.'="'.str_replace('"', '\\"', trim($value)).'"';
if ($value === false || is_null($value)) {
continue;
}

if ($value === true) {
$value = $key;
}

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

return trim($string);
Expand Down
13 changes: 13 additions & 0 deletions tests/View/ViewComponentAttributeBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,18 @@ public function testAttributeRetrieval()
$bag = new ComponentAttributeBag([]);

$this->assertSame('class="mt-4"', (string) $bag->merge(['class' => 'mt-4']));

$bag = new ComponentAttributeBag([
'test-string' => 'ok',
'test-null' => null,
'test-false' => false,
'test-true' => true,
'test-0' => 0,
'test-0-string' => '0',
'test-empty-string' => '',
]);

$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());
}
}