@@ -59,6 +59,8 @@ class Autolink
59
59
*/
60
60
protected $ linkBuilder ;
61
61
62
+ protected ?\Closure $ escapeHandler = null ;
63
+
62
64
/**
63
65
* Class init.
64
66
*
@@ -137,7 +139,7 @@ function ($matches) use ($attribs) {
137
139
preg_match ('/[a-zA-Z]*\=\"(.*)/ ' , $ matches [0 ], $ inElements );
138
140
139
141
if (!$ inElements ) {
140
- $ email = $ this ->isAutoEscape () ? htmlspecialchars ($ matches [0 ]) : $ matches [0 ];
142
+ $ email = $ this ->isAutoEscape () ? $ this -> escape ($ matches [0 ]) : $ matches [0 ];
141
143
142
144
$ attribs ['href ' ] = 'mailto: ' . $ email ;
143
145
@@ -176,7 +178,7 @@ public function link(string $url, array $attribs = []): string
176
178
}
177
179
}
178
180
179
- $ attribs ['href ' ] = $ this ->isAutoEscape () ? htmlspecialchars ($ url ) : $ url ;
181
+ $ attribs ['href ' ] = $ this ->isAutoEscape () ? $ this -> escape ($ url ) : $ url ;
180
182
181
183
if (($ scheme = $ this ->getLinkNoScheme ()) && !str_contains ($ attribs ['href ' ], ':// ' )) {
182
184
$ scheme = is_string ($ scheme ) ? $ scheme : 'http ' ;
@@ -185,11 +187,7 @@ public function link(string $url, array $attribs = []): string
185
187
}
186
188
187
189
if ($ this ->isAutoTitle ()) {
188
- $ attribs ['title ' ] = htmlspecialchars (
189
- $ url ,
190
- // PHP 8.1 or higher will escape single quote
191
- ENT_QUOTES | ENT_SUBSTITUTE
192
- );
190
+ $ attribs ['title ' ] = $ this ->escape ($ url );
193
191
}
194
192
195
193
return $ this ->buildLink ($ content , $ attribs );
@@ -209,7 +207,7 @@ protected function buildLink(?string $url = null, array $attribs = []): string
209
207
return (string ) ($ this ->linkBuilder )($ url , $ attribs );
210
208
}
211
209
212
- return HtmlBuilder::create ('a ' , $ attribs , htmlspecialchars ($ url ));
210
+ return HtmlBuilder::create ('a ' , $ attribs , $ this -> escape ($ url ));
213
211
}
214
212
215
213
/**
@@ -486,4 +484,23 @@ public static function shortenUrl(string $url, int $lastPartLimit = 15, int $dot
486
484
487
485
return $ first . str_repeat ('. ' , $ dots ) . $ last ;
488
486
}
487
+
488
+ public function escape (string $ str ): string
489
+ {
490
+ return $ this ->getEscapeHandler ()($ str );
491
+ }
492
+
493
+ public function getEscapeHandler (): ?\Closure
494
+ {
495
+ return $ this ->escapeHandler
496
+ // PHP 8.1 or higher will escape single quite
497
+ ?? static fn ($ str ) => htmlspecialchars ($ str , ENT_QUOTES | ENT_SUBSTITUTE );
498
+ }
499
+
500
+ public function setEscapeHandler (?\Closure $ escapeHandler ): static
501
+ {
502
+ $ this ->escapeHandler = $ escapeHandler ;
503
+
504
+ return $ this ;
505
+ }
489
506
}
0 commit comments