From 8ed98d648b124095c797e075c495ae30286046e7 Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Tue, 20 May 2025 15:01:07 +0200 Subject: [PATCH] [TwigComponent] Fix `ComponentAttributes` rendering when using `StimulusAttributes` as default attributes --- src/TwigComponent/CHANGELOG.md | 4 ++++ src/TwigComponent/src/ComponentAttributes.php | 2 +- src/TwigComponent/tests/Unit/ComponentAttributesTest.php | 7 +++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/TwigComponent/CHANGELOG.md b/src/TwigComponent/CHANGELOG.md index 770a5e15aab..8ddd89fb4b6 100644 --- a/src/TwigComponent/CHANGELOG.md +++ b/src/TwigComponent/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 2.25.2 + +- Fix `ComponentAttributes` rendering when using `StimulusAttributes` as default attributes + ## 2.25.1 - [SECURITY] `ComponentAttributes` now requires a `Twig\Runtime\EscaperRuntime` diff --git a/src/TwigComponent/src/ComponentAttributes.php b/src/TwigComponent/src/ComponentAttributes.php index 140e39f8d74..82f9a5cdd41 100644 --- a/src/TwigComponent/src/ComponentAttributes.php +++ b/src/TwigComponent/src/ComponentAttributes.php @@ -144,7 +144,7 @@ public function all(): array public function defaults(iterable $attributes): self { if ($attributes instanceof StimulusAttributes) { - $attributes = $attributes->toEscapedArray(); + $attributes = $attributes->toArray(); } if ($attributes instanceof \Traversable) { diff --git a/src/TwigComponent/tests/Unit/ComponentAttributesTest.php b/src/TwigComponent/tests/Unit/ComponentAttributesTest.php index 4e48ab850e8..377822c3118 100644 --- a/src/TwigComponent/tests/Unit/ComponentAttributesTest.php +++ b/src/TwigComponent/tests/Unit/ComponentAttributesTest.php @@ -143,7 +143,7 @@ public function testCanAddStimulusControllerViaStimulusAttributes(): void ], new EscaperRuntime()); $stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); - $stimulusAttributes->addController('foo', ['name' => 'ryan', 'some_array' => ['a', 'b']]); + $stimulusAttributes->addController('foo', ['name' => 'ryan', 'some_array' => ['a', 'b'], 'some_array_with_keys' => ['key1' => 'value1', 'key2' => 'value2']]); $attributes = $attributes->defaults($stimulusAttributes); $this->assertEquals([ @@ -151,8 +151,10 @@ public function testCanAddStimulusControllerViaStimulusAttributes(): void 'data-controller' => 'foo live', 'data-live-data-value' => '{}', 'data-foo-name-value' => 'ryan', - 'data-foo-some-array-value' => '["a","b"]', + 'data-foo-some-array-value' => '["a","b"]', + 'data-foo-some-array-with-keys-value' => '{"key1":"value1","key2":"value2"}', ], $attributes->all()); + $this->assertSame(' data-controller="foo live" data-foo-name-value="ryan" data-foo-some-array-value="["a","b"]" data-foo-some-array-with-keys-value="{"key1":"value1","key2":"value2"}" class="foo" data-live-data-value="{}"', (string) $attributes); } public function testCanAddStimulusActionViaStimulusAttributes(): void @@ -175,6 +177,7 @@ public function testCanAddStimulusActionViaStimulusAttributes(): void 'class' => 'foo', 'data-action' => 'foo#barMethod live#foo', ], $attributes->all()); + $this->assertSame(' data-action="foo#barMethod live#foo" class="foo"', (string) $attributes); } public function testBooleanBehaviour(): void