1212namespace Symfony \UX \TwigComponent ;
1313
1414use Symfony \UX \StimulusBundle \Dto \StimulusAttributes ;
15- use Symfony \UX \TwigComponent \Escaper \HtmlAttributeEscaperInterface ;
1615use Symfony \WebpackEncoreBundle \Dto \AbstractStimulusDto ;
16+ use Twig \Runtime \EscaperRuntime ;
1717
1818/**
1919 * @author Kevin Bond <kevinbond@gmail.com>
@@ -29,19 +29,13 @@ final class ComponentAttributes implements \Stringable, \IteratorAggregate, \Cou
2929 /** @var array<string,true> */
3030 private array $ rendered = [];
3131
32- private readonly ?HtmlAttributeEscaperInterface $ escaper ;
33-
3432 /**
3533 * @param array<string, string|bool> $attributes
3634 */
3735 public function __construct (
3836 private array $ attributes ,
39- ? HtmlAttributeEscaperInterface $ escaper = null ,
37+ private readonly EscaperRuntime $ escaper ,
4038 ) {
41- // Third argument used as internal flag to prevent multiple deprecations
42- if ((null === $ this ->escaper = $ escaper ) && 3 > func_num_args ()) {
43- trigger_deprecation ('symfony/ux-twig-component ' , '2.24 ' , 'Not passing an "%s" to "%s" is deprecated and will throw in 3.0. ' , HtmlAttributeEscaperInterface::class, self ::class);
44- }
4539 }
4640
4741 public function __toString (): string
@@ -87,13 +81,17 @@ public function __toString(): string
8781 // - special syntax names (Vue.js, Svelte, Alpine.js, ...)
8882 // v-*, x-*, @*, :*
8983 if (!ctype_alpha (str_replace (['- ' , '_ ' , ': ' , '@ ' , '. ' ], '' , $ key ))) {
90- $ key = $ this ->escaper ?->escapeName ($ key) ?? $ key ;
84+ $ key = ( string ) $ this ->escaper -> escape ($ key, ' html_attr ' ) ;
9185 }
9286
9387 if (true === $ value ) {
9488 $ attributes .= ' ' .$ key ;
9589 } else {
96- $ attributes .= ' ' .\sprintf ('%s="%s" ' , $ key , $ this ->escaper ?->escapeValue($ value ) ?? $ value );
90+ if (!ctype_alnum (str_replace (['- ' , '_ ' ], '' , $ value ))) {
91+ $ value = $ this ->escaper ->escape ($ value , 'html ' );
92+ }
93+
94+ $ attributes .= ' ' .\sprintf ('%s="%s" ' , $ key , $ value );
9795 }
9896 }
9997
@@ -167,7 +165,7 @@ public function defaults(iterable $attributes): self
167165 unset($ attributes [$ attribute ]);
168166 }
169167
170- return new self ($ attributes , $ this ->escaper , true );
168+ return new self ($ attributes , $ this ->escaper );
171169 }
172170
173171 /**
@@ -183,7 +181,7 @@ public function only(string ...$keys): self
183181 }
184182 }
185183
186- return new self ($ attributes , $ this ->escaper , true );
184+ return new self ($ attributes , $ this ->escaper );
187185 }
188186
189187 /**
@@ -221,7 +219,7 @@ public function add($stimulusDto): self
221219 )));
222220 unset($ controllersAttributes ['data-controller ' ]);
223221
224- $ clone = new self ($ attributes , $ this ->escaper , true );
222+ $ clone = new self ($ attributes , $ this ->escaper );
225223
226224 // add the remaining attributes for values/classes
227225 return $ clone ->defaults ($ controllersAttributes );
@@ -233,7 +231,7 @@ public function remove($key): self
233231
234232 unset($ attributes [$ key ]);
235233
236- return new self ($ attributes , $ this ->escaper , true );
234+ return new self ($ attributes , $ this ->escaper );
237235 }
238236
239237 public function nested (string $ namespace ): self
@@ -249,7 +247,7 @@ public function nested(string $namespace): self
249247 }
250248 }
251249
252- return new self ($ attributes , $ this ->escaper , true );
250+ return new self ($ attributes , $ this ->escaper );
253251 }
254252
255253 public function getIterator (): \Traversable
0 commit comments