Skip to content

Commit 952e6e7

Browse files
committed
bug #2757 [TwigComponent] Fix ComponentAttributes rendering when using StimulusAttributes as default attributes (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- [TwigComponent] Fix `ComponentAttributes` rendering when using `StimulusAttributes` as default attributes | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Docs? | yes <!-- required for new features --> | Issues | Fix #2756 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> We missed the case where we can inject `StimulusAttributes` when using `attributes.defaults()`, as documented in https://symfony.com/bundles/ux-twig-component/current/index.html#component-attributes It looks like our tests weren't solid enough, since we asserted on `all()` instead of the rendered string. Commits ------- 8ed98d6 [TwigComponent] Fix `ComponentAttributes` rendering when using `StimulusAttributes` as default attributes
2 parents b63eb02 + 8ed98d6 commit 952e6e7

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.25.2
4+
5+
- Fix `ComponentAttributes` rendering when using `StimulusAttributes` as default attributes
6+
37
## 2.25.1
48

59
- [SECURITY] `ComponentAttributes` now requires a `Twig\Runtime\EscaperRuntime`

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function all(): array
144144
public function defaults(iterable $attributes): self
145145
{
146146
if ($attributes instanceof StimulusAttributes) {
147-
$attributes = $attributes->toEscapedArray();
147+
$attributes = $attributes->toArray();
148148
}
149149

150150
if ($attributes instanceof \Traversable) {

src/TwigComponent/tests/Unit/ComponentAttributesTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,18 @@ public function testCanAddStimulusControllerViaStimulusAttributes(): void
143143
], new EscaperRuntime());
144144

145145
$stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));
146-
$stimulusAttributes->addController('foo', ['name' => 'ryan', 'some_array' => ['a', 'b']]);
146+
$stimulusAttributes->addController('foo', ['name' => 'ryan', 'some_array' => ['a', 'b'], 'some_array_with_keys' => ['key1' => 'value1', 'key2' => 'value2']]);
147147
$attributes = $attributes->defaults($stimulusAttributes);
148148

149149
$this->assertEquals([
150150
'class' => 'foo',
151151
'data-controller' => 'foo live',
152152
'data-live-data-value' => '{}',
153153
'data-foo-name-value' => 'ryan',
154-
'data-foo-some-array-value' => '[&quot;a&quot;,&quot;b&quot;]',
154+
'data-foo-some-array-value' => '["a","b"]',
155+
'data-foo-some-array-with-keys-value' => '{"key1":"value1","key2":"value2"}',
155156
], $attributes->all());
157+
$this->assertSame(' data-controller="foo live" data-foo-name-value="ryan" data-foo-some-array-value="[&quot;a&quot;,&quot;b&quot;]" data-foo-some-array-with-keys-value="{&quot;key1&quot;:&quot;value1&quot;,&quot;key2&quot;:&quot;value2&quot;}" class="foo" data-live-data-value="{}"', (string) $attributes);
156158
}
157159

158160
public function testCanAddStimulusActionViaStimulusAttributes(): void
@@ -175,6 +177,7 @@ public function testCanAddStimulusActionViaStimulusAttributes(): void
175177
'class' => 'foo',
176178
'data-action' => 'foo#barMethod live#foo',
177179
], $attributes->all());
180+
$this->assertSame(' data-action="foo#barMethod live#foo" class="foo"', (string) $attributes);
178181
}
179182

180183
public function testBooleanBehaviour(): void

0 commit comments

Comments
 (0)