@@ -107,54 +107,36 @@ public function addAttribute(string $name, string $value): void
107
107
108
108
public function __toString (): string
109
109
{
110
- $ controllers = array_map (function (string $ controllerName ): string {
111
- return $ this ->escapeAsHtmlAttr ($ controllerName );
112
- }, $ this ->controllers );
113
-
114
- // done separately so we can escape, but avoid escaping ->
115
- $ actions = array_map (function (array $ actionData ): string {
116
- $ controllerName = $ this ->escapeAsHtmlAttr ($ actionData ['controllerName ' ]);
117
- $ actionName = $ this ->escapeAsHtmlAttr ($ actionData ['actionName ' ]);
118
- $ eventName = $ actionData ['eventName ' ];
119
-
120
- $ action = $ controllerName .'# ' .$ actionName ;
121
- if (null !== $ eventName ) {
122
- $ action = $ this ->escapeAsHtmlAttr ($ eventName ).'-> ' .$ action ;
123
- }
124
-
125
- return $ action ;
126
- }, $ this ->actions );
110
+ $ attributes = [];
127
111
128
- $ targets = [];
129
- foreach ($ this ->targets as $ key => $ targetNamesString ) {
130
- $ targetNames = explode (' ' , $ targetNamesString );
131
- $ targets [$ key ] = implode (' ' , array_map (function (string $ targetName ): string {
132
- return $ this ->escapeAsHtmlAttr ($ targetName );
133
- }, $ targetNames ));
112
+ if ($ this ->controllers ) {
113
+ $ attributes [] = 'data-controller=" ' .$ this ->escape (implode (' ' , $ this ->controllers )).'" ' ;
134
114
}
135
115
136
- $ attributes = [];
116
+ if ($ this ->actions ) {
117
+ $ actions = [];
118
+ foreach ($ this ->actions as ['controllerName ' => $ controllerName , 'actionName ' => $ actionName , 'eventName ' => $ eventName ]) {
119
+ $ action = $ this ->escape ($ controllerName .'# ' .$ actionName );
120
+ if (null !== $ eventName ) {
121
+ // done separately so we can escape, but avoid escaping ->
122
+ $ action = $ this ->escape ($ eventName ).'-> ' .$ action ;
123
+ }
124
+
125
+ $ actions [] = $ action ;
126
+ }
137
127
138
- if ($ controllers ) {
139
- $ attributes [] = \sprintf ('data-controller="%s" ' , implode (' ' , $ controllers ));
128
+ $ attributes [] = 'data-action=" ' .implode (' ' , $ actions ).'" ' ;
140
129
}
141
130
142
- if ($ actions ) {
143
- $ attributes [] = \sprintf ( ' data-action="%s" ' , implode ( ' ' , $ actions )) ;
131
+ foreach ($ this -> targets as $ k => $ v ) {
132
+ $ attributes [] = $ this -> escape ( $ k , ' html_attr ' ). ' =" ' . $ this -> escape ( $ v ). ' " ' ;
144
133
}
145
134
146
- if ($ targets ) {
147
- $ attributes [] = implode (' ' , array_map (function (string $ key , string $ value ): string {
148
- return \sprintf ('%s="%s" ' , $ key , $ value );
149
- }, array_keys ($ targets ), $ targets ));
135
+ foreach ($ this ->attributes as $ k => $ v ) {
136
+ $ attributes [] = $ this ->escape ($ k , 'html_attr ' ).'=" ' .$ this ->escape ($ v ).'" ' ;
150
137
}
151
138
152
- return rtrim (implode (' ' , [
153
- ...$ attributes ,
154
- ...array_map (function (string $ attribute , string $ value ): string {
155
- return $ attribute .'=" ' .$ this ->escapeAsHtmlAttr ($ value ).'" ' ;
156
- }, array_keys ($ this ->attributes ), $ this ->attributes ),
157
- ]));
139
+ return implode (' ' , $ attributes );
158
140
}
159
141
160
142
public function toArray (): array
@@ -193,7 +175,7 @@ public function toEscapedArray(): array
193
175
{
194
176
$ escaped = [];
195
177
foreach ($ this ->toArray () as $ key => $ value ) {
196
- $ escaped [$ key ] = $ this ->escapeAsHtmlAttr ($ value );
178
+ $ escaped [$ key ] = $ this ->escape ($ value );
197
179
}
198
180
199
181
return $ escaped ;
@@ -212,18 +194,18 @@ private function getFormattedValue(mixed $value): string
212
194
return (string ) $ value ;
213
195
}
214
196
215
- private function escapeAsHtmlAttr (mixed $ value ): string
197
+ private function escape (mixed $ value, string $ strategy = ' html ' ): string
216
198
{
217
199
if (class_exists (EscaperRuntime::class)) {
218
- return $ this ->env ->getRuntime (EscaperRuntime::class)->escape ($ value , ' html_attr ' );
200
+ return $ this ->env ->getRuntime (EscaperRuntime::class)->escape ($ value , $ strategy );
219
201
}
220
202
221
203
if (method_exists (EscaperExtension::class, 'escape ' )) {
222
- return EscaperExtension::escape ($ this ->env , $ value , ' html_attr ' );
204
+ return EscaperExtension::escape ($ this ->env , $ value , $ strategy );
223
205
}
224
206
225
207
// since twig/twig 3.9.0: Using the internal "twig_escape_filter" function is deprecated.
226
- return (string ) twig_escape_filter ($ this ->env , $ value , ' html_attr ' );
208
+ return (string ) twig_escape_filter ($ this ->env , $ value , $ strategy );
227
209
}
228
210
229
211
/**
0 commit comments