Skip to content

Commit 1fcf2ea

Browse files
committed
bug #2159 [StimulusBundle] Normalize Stimulus controller name in event name (7-zete-7)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [StimulusBundle] Normalize Stimulus controller name in event name | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #2151 | License | MIT Adds the ability to use non-normalized Stimulus controller names when describing actions on events from other Stimulus controllers. Commits ------- a09e9af [StimulusBundle] Normalize Stimulus controller name in event name
2 parents 028a4e6 + a09e9af commit 1fcf2ea

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

src/StimulusBundle/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.20.1
4+
5+
- Normalize Stimulus controller name in event name
6+
37
## 2.14.2
48

59
- Fix bug with finding UX Packages with non-standard project structure

src/StimulusBundle/src/Dto/StimulusAttributes.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function addAction(string $controllerName, string $actionName, ?string $e
7575
$this->actions[] = [
7676
'controllerName' => $controllerName,
7777
'actionName' => $actionName,
78-
'eventName' => $eventName,
78+
'eventName' => null !== $eventName ? $this->normalizeEventName($eventName) : null,
7979
];
8080

8181
foreach ($parameters as $name => $value) {
@@ -218,6 +218,14 @@ private function normalizeControllerName(string $controllerName): string
218218
return preg_replace('/^@/', '', str_replace('_', '-', str_replace('/', '--', $controllerName)));
219219
}
220220

221+
/**
222+
* @see https://stimulus.hotwired.dev/reference/actions
223+
*/
224+
private function normalizeEventName(string $eventName): string
225+
{
226+
return preg_replace_callback('/^.+(?=:)/', fn (array $matches): string => $this->normalizeControllerName($matches[0]), $eventName);
227+
}
228+
221229
/**
222230
* Normalize a Stimulus Value API key into its HTML equivalent ("kebab case").
223231
* Backport features from symfony/string.

src/StimulusBundle/tests/Dto/StimulusAttributesTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,91 @@ public function testAddAttribute()
148148
$this->assertSame('foo="bar baz"', (string) $this->stimulusAttributes);
149149
$this->assertSame(['foo' => 'bar baz'], $this->stimulusAttributes->toArray());
150150
}
151+
152+
/**
153+
* @dataProvider provideAddComplexActionData
154+
*/
155+
public function testAddComplexAction(string $controllerName, string $actionName, ?string $eventName, string $expectedAction): void
156+
{
157+
$this->stimulusAttributes->addAction($controllerName, $actionName, $eventName);
158+
$attributesHtml = (string) $this->stimulusAttributes;
159+
self::assertSame(\sprintf('data-action="%s"', $expectedAction), $attributesHtml);
160+
}
161+
162+
/**
163+
* @return iterable<array{
164+
* controllerName: string,
165+
* actionName: string,
166+
* eventName: ?string,
167+
* expectedAction: string,
168+
* }>
169+
*/
170+
public static function provideAddComplexActionData(): iterable
171+
{
172+
// basic datasets
173+
yield 'foo#bar' => [
174+
'controllerName' => 'foo',
175+
'actionName' => 'bar',
176+
'eventName' => null,
177+
'expectedAction' => 'foo#bar',
178+
];
179+
yield 'baz->foo#bar' => [
180+
'controllerName' => 'foo',
181+
'actionName' => 'bar',
182+
'eventName' => 'baz',
183+
'expectedAction' => 'baz->foo#bar',
184+
];
185+
186+
// datasets from https://github.com/hotwired/stimulus
187+
yield 'keydown.esc@document->a#log' => [
188+
'controllerName' => 'a',
189+
'actionName' => 'log',
190+
'eventName' => 'keydown.esc@document',
191+
'expectedAction' => 'keydown.esc@document->a#log',
192+
];
193+
yield 'keydown.enter->a#log' => [
194+
'controllerName' => 'a',
195+
'actionName' => 'log',
196+
'eventName' => 'keydown.enter',
197+
'expectedAction' => 'keydown.enter->a#log',
198+
];
199+
yield 'keydown.shift+a->a#log' => [
200+
'controllerName' => 'a',
201+
'actionName' => 'log',
202+
'eventName' => 'keydown.shift+a',
203+
'expectedAction' => 'keydown.shift+a->a#log',
204+
];
205+
yield 'keydown@window->c#log' => [
206+
'controllerName' => 'c',
207+
'actionName' => 'log',
208+
'eventName' => 'keydown@window',
209+
'expectedAction' => 'keydown@window->c#log',
210+
];
211+
yield 'click->c#log:once' => [
212+
'controllerName' => 'c',
213+
'actionName' => 'log:once',
214+
'eventName' => 'click',
215+
'expectedAction' => 'click->c#log:once',
216+
];
217+
218+
// extended datasets
219+
yield 'vue:mount->foo#bar:passive' => [
220+
'controllerName' => 'foo',
221+
'actionName' => 'bar:passive',
222+
'eventName' => 'vue:mount',
223+
'expectedAction' => 'vue:mount->foo#bar:passive',
224+
];
225+
yield 'foo--controller-1:baz->bar--controller-2#log' => [
226+
'controllerName' => '@bar/controller_2',
227+
'actionName' => 'log',
228+
'eventName' => '@foo/controller_1:baz',
229+
'expectedAction' => 'foo--controller-1:baz->bar--controller-2#log',
230+
];
231+
yield 'foo--controller-1:baz@document->bar--controller-2#log:capture' => [
232+
'controllerName' => '@bar/controller_2',
233+
'actionName' => 'log:capture',
234+
'eventName' => '@foo/controller_1:baz@document',
235+
'expectedAction' => 'foo--controller-1:baz@document->bar--controller-2#log:capture',
236+
];
237+
}
151238
}

0 commit comments

Comments
 (0)