@@ -107,54 +107,36 @@ public function addAttribute(string $name, string $value): void
107107
108108 public function __toString (): string
109109 {
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 = [];
127111
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 )).'" ' ;
134114 }
135115
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+ }
137127
138- if ($ controllers ) {
139- $ attributes [] = \sprintf ('data-controller="%s" ' , implode (' ' , $ controllers ));
128+ $ attributes [] = 'data-action=" ' .implode (' ' , $ actions ).'" ' ;
140129 }
141130
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 ). ' " ' ;
144133 }
145134
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 ).'" ' ;
150137 }
151138
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 );
158140 }
159141
160142 public function toArray (): array
@@ -193,7 +175,7 @@ public function toEscapedArray(): array
193175 {
194176 $ escaped = [];
195177 foreach ($ this ->toArray () as $ key => $ value ) {
196- $ escaped [$ key ] = $ this ->escapeAsHtmlAttr ($ value );
178+ $ escaped [$ key ] = $ this ->escape ($ value );
197179 }
198180
199181 return $ escaped ;
@@ -212,18 +194,18 @@ private function getFormattedValue(mixed $value): string
212194 return (string ) $ value ;
213195 }
214196
215- private function escapeAsHtmlAttr (mixed $ value ): string
197+ private function escape (mixed $ value, string $ strategy = ' html ' ): string
216198 {
217199 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 );
219201 }
220202
221203 if (method_exists (EscaperExtension::class, 'escape ' )) {
222- return EscaperExtension::escape ($ this ->env , $ value , ' html_attr ' );
204+ return EscaperExtension::escape ($ this ->env , $ value , $ strategy );
223205 }
224206
225207 // 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 );
227209 }
228210
229211 /**
0 commit comments