Skip to content

Commit 66585b3

Browse files
committed
Add a params to close auto htmlspecialchars() #7
1 parent 2bf011d commit 66585b3

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Create with options.
4141
$options = array(
4242
'strip_scheme' => false,
4343
'text_limit' => false,
44-
'auto_title' => false
44+
'auto_title' => false,
45+
'escape' => true
4546
);
4647

4748
$schemes = array('http', 'https', 'skype', 'itunes');
@@ -188,6 +189,22 @@ Output
188189
<a href="http://www.google.com.tw" >www.google.com.tw</a>
189190
```
190191

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+
191208
## Scheme
192209

193210
You can add new scheme to convert URL begin with it, for example: `vnc://example.com`

src/Autolink.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class Autolink
2525
public $options = array(
2626
'strip_scheme' => false,
2727
'text_limit' => false,
28-
'auto_title' => false
28+
'auto_title' => false,
29+
'escape' => true
2930
);
3031

3132
/**
@@ -109,7 +110,9 @@ function ($matches) use ($self, $attribs) {
109110
preg_match('/[a-zA-Z]*\=\"(.*)/', $matches[0], $inElements);
110111

111112
if (!$inElements) {
112-
$attribs['href'] = 'mailto:' . htmlspecialchars($matches[0]);
113+
$email = $self->autoEscape() ? htmlspecialchars($matches[0]) : $matches[0];
114+
115+
$attribs['href'] = 'mailto:' . $email;
113116

114117
return $self->buildLink($matches[0], $attribs);
115118
}
@@ -146,7 +149,7 @@ public function link($url, $attribs = array())
146149
}
147150
}
148151

149-
$attribs['href'] = htmlspecialchars($url);
152+
$attribs['href'] = $this->autoEscape() ? htmlspecialchars($url) : $url;
150153

151154
if ($this->autoTitle()) {
152155
$attribs['title'] = htmlspecialchars($url);
@@ -205,6 +208,20 @@ public function stripScheme($value = null)
205208
return $this->optionAccess('strip_scheme', $value);
206209
}
207210

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+
208225
/**
209226
* textLimit
210227
*

test/AutolinkTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,29 @@ public function testGetAndSetScheme()
243243
$this->assertEquals(array(), $autolink->getSchemes());
244244
}
245245

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&[email protected]';
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+
246269
public function testConvertEmail()
247270
{
248271
$text = <<<TEXT

0 commit comments

Comments
 (0)