File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -108,10 +108,10 @@ public function add(AbstractStimulusDto $stimulusDto): self
108
108
$ controllersAttributes = $ stimulusDto ->toArray ();
109
109
$ attributes = $ this ->attributes ;
110
110
111
- $ attributes ['data-controller ' ] = implode (' ' , array_merge (
112
- explode (' ' , $ attributes ['data-controller ' ]),
111
+ $ attributes ['data-controller ' ] = trim ( implode (' ' , array_merge (
112
+ explode (' ' , $ attributes ['data-controller ' ] ?? '' ),
113
113
explode (' ' , $ controllersAttributes ['data-controller ' ] ?? [])
114
- ));
114
+ ))) ;
115
115
unset($ controllersAttributes ['data-controller ' ]);
116
116
117
117
$ clone = new self ($ attributes );
Original file line number Diff line number Diff line change @@ -88,6 +88,29 @@ public function testCanAddStimulusController(): void
88
88
], $ attributes ->all ());
89
89
}
90
90
91
+ public function testCanAddStimulusControllerIfNoneAlreadyPresent (): void
92
+ {
93
+ $ attributes = new ComponentAttributes ([
94
+ 'class ' => 'foo ' ,
95
+ ]);
96
+
97
+ $ controllerDto = $ this ->createMock (AbstractStimulusDto::class);
98
+ $ controllerDto ->expects (self ::once ())
99
+ ->method ('toArray ' )
100
+ ->willReturn ([
101
+ 'data-controller ' => 'foo bar ' ,
102
+ 'data-foo-name-value ' => 'ryan ' ,
103
+ ]);
104
+
105
+ $ attributes = $ attributes ->add ($ controllerDto );
106
+
107
+ $ this ->assertEquals ([
108
+ 'class ' => 'foo ' ,
109
+ 'data-controller ' => 'foo bar ' ,
110
+ 'data-foo-name-value ' => 'ryan ' ,
111
+ ], $ attributes ->all ());
112
+ }
113
+
91
114
public function testBooleanBehaviour (): void
92
115
{
93
116
$ attributes = new ComponentAttributes (['disabled ' => true ]);
You can’t perform that action at this time.
0 commit comments