@@ -124,7 +124,7 @@ public function escape($string, string $strategy = 'html', ?string $charset = nu
124124 }
125125
126126 $ string = (string ) $ string ;
127- } elseif (\in_array ($ strategy , ['html ' , 'js ' , 'css ' , 'html_attr ' , 'url ' ], true )) {
127+ } elseif (\in_array ($ strategy , ['html ' , 'js ' , 'css ' , 'html_attr ' , 'html_attr_relaxed ' , ' url ' ], true )) {
128128 // we return the input as is (which can be of any type)
129129 return $ string ;
130130 }
@@ -256,6 +256,7 @@ public function escape($string, string $strategy = 'html', ?string $charset = nu
256256 return $ string ;
257257
258258 case 'html_attr ' :
259+ case 'html_attr_relaxed ' :
259260 if ('UTF-8 ' !== $ charset ) {
260261 $ string = $ this ->convertEncoding ($ string , 'UTF-8 ' , $ charset );
261262 }
@@ -264,7 +265,12 @@ public function escape($string, string $strategy = 'html', ?string $charset = nu
264265 throw new RuntimeError ('The string to escape is not a valid UTF-8 string. ' );
265266 }
266267
267- $ string = preg_replace_callback ('#[^a-zA-Z0-9,\.\-_]#Su ' , static function ($ matches ) {
268+ $ regex = match ($ strategy ) {
269+ 'html_attr ' => '#[^a-zA-Z0-9,\.\-_]#Su ' ,
270+ 'html_attr_relaxed ' => '#[^a-zA-Z0-9,\.\-_:@\[\]]#Su ' ,
271+ };
272+
273+ $ string = preg_replace_callback ($ regex , static function ($ matches ) {
268274 /**
269275 * This function is adapted from code coming from Zend Framework.
270276 *
@@ -323,7 +329,7 @@ public function escape($string, string $strategy = 'html', ?string $charset = nu
323329 return $ this ->escapers [$ strategy ]($ string , $ charset );
324330 }
325331
326- $ validStrategies = implode ('", " ' , array_merge (['html ' , 'js ' , 'url ' , 'css ' , 'html_attr ' ], array_keys ($ this ->escapers )));
332+ $ validStrategies = implode ('", " ' , array_merge (['html ' , 'js ' , 'url ' , 'css ' , 'html_attr ' , ' html_attr_relaxed ' ], array_keys ($ this ->escapers )));
327333
328334 throw new RuntimeError (\sprintf ('Invalid escaping strategy "%s" (valid ones: "%s"). ' , $ strategy , $ validStrategies ));
329335 }
0 commit comments