Skip to content

Commit

Permalink
[7.x] Fix merging boolean or null attributes in Blade components (#32245
Browse files Browse the repository at this point in the history
)

* Fix merging boolean or null attributes in Blade components

* fix style

* fix style again
  • Loading branch information
sileence authored Apr 6, 2020
1 parent 79b433a commit ff1a13e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public function merge(array $attributeDefaults = [])
$attributes = [];

$attributeDefaults = array_map(function ($value) {
if (is_null($value) || is_bool($value)) {
return $value;
}

return e($value);
}, $attributeDefaults);

Expand Down
20 changes: 20 additions & 0 deletions tests/View/ViewComponentAttributeBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,25 @@ public function testAttributeRetrieval()

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

$bag = (new ComponentAttributeBag)
->merge([
'test-escaped' => '<tag attr="attr">',
]);

$this->assertSame('test-escaped="&lt;tag attr=&quot;attr&quot;&gt;"', (string) $bag);

$bag = (new ComponentAttributeBag)
->merge([
'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);
}
}

0 comments on commit ff1a13e

Please sign in to comment.