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
4 changes: 4 additions & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
2 changes: 1 addition & 1 deletion src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,18 @@ 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([
'class' => 'foo',
'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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix:

Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-' 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="{}"'
+' 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="{}"'

}

public function testCanAddStimulusActionViaStimulusAttributes(): void
Expand All @@ -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
Expand Down
Loading