File tree Expand file tree Collapse file tree 3 files changed +61
-4
lines changed Expand file tree Collapse file tree 3 files changed +61
-4
lines changed Original file line number Diff line number Diff line change @@ -41,7 +41,8 @@ Create with options.
41
41
$options = array(
42
42
'strip_scheme' => false,
43
43
'text_limit' => false,
44
- 'auto_title' => false
44
+ 'auto_title' => false,
45
+ 'escape' => true
45
46
);
46
47
47
48
$schemes = array('http', 'https', 'skype', 'itunes');
@@ -188,6 +189,22 @@ Output
188
189
<a href =" http://www.google.com.tw" >www.google.com.tw</a >
189
190
```
190
191
192
+ ### ` escape `
193
+
194
+ Strip Scheme on link text:
195
+
196
+ ``` php
197
+ $auitolink->autoEscape(true);
198
+
199
+ $text = $autolink->convert($text);
200
+ ```
201
+
202
+ Output
203
+
204
+ ``` html
205
+ <a href =" http://www.google.com.tw?foo=bar&yoo=baz" >http://www.google.com.tw?foo=bar&yoo=baz</a >
206
+ ```
207
+
191
208
## Scheme
192
209
193
210
You can add new scheme to convert URL begin with it, for example: ` vnc://example.com `
Original file line number Diff line number Diff line change @@ -25,7 +25,8 @@ class Autolink
25
25
public $ options = array (
26
26
'strip_scheme ' => false ,
27
27
'text_limit ' => false ,
28
- 'auto_title ' => false
28
+ 'auto_title ' => false ,
29
+ 'escape ' => true
29
30
);
30
31
31
32
/**
@@ -109,7 +110,9 @@ function ($matches) use ($self, $attribs) {
109
110
preg_match ('/[a-zA-Z]*\=\"(.*)/ ' , $ matches [0 ], $ inElements );
110
111
111
112
if (!$ inElements ) {
112
- $ attribs ['href ' ] = 'mailto: ' . htmlspecialchars ($ matches [0 ]);
113
+ $ email = $ self ->autoEscape () ? htmlspecialchars ($ matches [0 ]) : $ matches [0 ];
114
+
115
+ $ attribs ['href ' ] = 'mailto: ' . $ email ;
113
116
114
117
return $ self ->buildLink ($ matches [0 ], $ attribs );
115
118
}
@@ -146,7 +149,7 @@ public function link($url, $attribs = array())
146
149
}
147
150
}
148
151
149
- $ attribs ['href ' ] = htmlspecialchars ($ url );
152
+ $ attribs ['href ' ] = $ this -> autoEscape () ? htmlspecialchars ($ url ) : $ url ;
150
153
151
154
if ($ this ->autoTitle ()) {
152
155
$ attribs ['title ' ] = htmlspecialchars ($ url );
@@ -205,6 +208,20 @@ public function stripScheme($value = null)
205
208
return $ this ->optionAccess ('strip_scheme ' , $ value );
206
209
}
207
210
211
+ /**
212
+ * autoEscape
213
+ *
214
+ * @param mixed $value
215
+ *
216
+ * @return mixed|static
217
+ *
218
+ * @since __DEPLOY_VERSION__
219
+ */
220
+ public function autoEscape ($ value = null )
221
+ {
222
+ return $ this ->optionAccess ('escape ' , $ value );
223
+ }
224
+
208
225
/**
209
226
* textLimit
210
227
*
Original file line number Diff line number Diff line change @@ -243,6 +243,29 @@ public function testGetAndSetScheme()
243
243
$ this ->assertEquals (array (), $ autolink ->getSchemes ());
244
244
}
245
245
246
+ public function testAutoEscape ()
247
+ {
248
+ $ autolink = new Autolink ();
249
+
250
+ $ url = 'https://example.com/?foo=bar&yoo=baz ' ;
251
+
252
+ $ this ->assertEquals ('<a href=" ' . htmlspecialchars ($ url ) . '"> ' . htmlspecialchars ($ url ) . '</a> ' , $ autolink ->convert ($ url ));
253
+
254
+ $ autolink ->autoEscape (false );
255
+
256
+ $ this ->assertEquals ('<a href=" ' . $ url . '"> ' . htmlspecialchars ($ url ) . '</a> ' , $ autolink ->convert ($ url ));
257
+
258
+ $ url = 'hello+admin&test@example.org ' ;
259
+
260
+ $ autolink ->autoEscape (true );
261
+
262
+ $ this ->assertEquals ('<a href="mailto: ' . htmlspecialchars ($ url ) . '"> ' . htmlspecialchars ($ url ) . '</a> ' , $ autolink ->convertEmail ($ url ));
263
+
264
+ $ autolink ->autoEscape (false );
265
+
266
+ $ this ->assertEquals ('<a href="mailto: ' . $ url . '"> ' . htmlspecialchars ($ url ) . '</a> ' , $ autolink ->convertEmail ($ url ));
267
+ }
268
+
246
269
public function testConvertEmail ()
247
270
{
248
271
$ text = <<<TEXT
You can’t perform that action at this time.
0 commit comments