Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Properly Inherit Parent Attributes #32576

Merged
merged 3 commits into from
Apr 28, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\View\Compilers\Concerns;

use Illuminate\Support\Str;
use Illuminate\View\ComponentAttributeBag;

trait CompilesComponents
{
Expand Down Expand Up @@ -167,7 +168,7 @@ protected function compileProps($expression)
public static function sanitizeComponentAttribute($value)
{
return is_string($value) ||
(is_object($value) && method_exists($value, '__toString'))
(is_object($value) && ! $value instanceof ComponentAttributeBag && method_exists($value, '__toString'))
? e($value)
: $value;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ public function merge(array $attributeDefaults = [])
*/
public function setAttributes(array $attributes)
{
if (isset($attributes['attributes']) &&
$attributes['attributes'] instanceof self) {
$parentBag = $attributes['attributes'];

unset($attributes['attributes']);

$attributes = $parentBag->merge($attributes);
}

$this->attributes = $attributes;
}

Expand Down
1 change: 0 additions & 1 deletion tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Compilers\ComponentTagCompiler;
use Illuminate\View\Component;
Expand Down
10 changes: 10 additions & 0 deletions tests/View/ViewComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\View;

use Illuminate\View\Component;
use Illuminate\View\ComponentAttributeBag;
use PHPUnit\Framework\TestCase;

class ViewComponentTest extends TestCase
Expand All @@ -18,6 +19,15 @@ public function testDataExposure()
$this->assertSame('taylor', $variables['hello']('taylor'));
}

public function testAttributeParentInheritance()
{
$component = new TestViewComponent;

$component->withAttributes(['class' => 'foo', 'attributes' => new ComponentAttributeBag(['class' => 'bar', 'type' => 'button'])]);

$this->assertEquals('class="foo bar" type="button"', (string) $component->attributes);
}

public function testPublicMethodsWithNoArgsAreConvertedToStringableCallablesInvokedAndNotCached()
{
$component = new TestSampleViewComponent;
Expand Down