diff options
author | Nicolas Lœuillet <nicolas@loeuillet.org> | 2014-02-21 15:57:10 +0100 |
---|---|---|
committer | Nicolas Lœuillet <nicolas@loeuillet.org> | 2014-02-21 15:57:10 +0100 |
commit | 99679d06884120c57f43b44e55e03595f1f87bed (patch) | |
tree | a3f2a1aa1afdaeca1386d0c6e8a75344fd2241fb /inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/Linkify.php | |
parent | 655214ab30ee84884dc408488b85586f36263fcb (diff) | |
parent | d3b47e94705e17b3ba3529cbb1dc6efe69c5d2b7 (diff) | |
download | wallabag-1.5.2.tar.gz wallabag-1.5.2.tar.zst wallabag-1.5.2.zip |
Merge pull request #481 from wallabag/dev1.5.2
1.5.2
Diffstat (limited to 'inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/Linkify.php')
-rw-r--r-- | inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/Linkify.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/Linkify.php b/inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/Linkify.php new file mode 100644 index 00000000..5eed3c12 --- /dev/null +++ b/inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/Linkify.php | |||
@@ -0,0 +1,59 @@ | |||
1 | <?php | ||
2 | |||
3 | /** | ||
4 | * Injector that converts http, https and ftp text URLs to actual links. | ||
5 | */ | ||
6 | class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector | ||
7 | { | ||
8 | /** | ||
9 | * @type string | ||
10 | */ | ||
11 | public $name = 'Linkify'; | ||
12 | |||
13 | /** | ||
14 | * @type array | ||
15 | */ | ||
16 | public $needed = array('a' => array('href')); | ||
17 | |||
18 | /** | ||
19 | * @param HTMLPurifier_Token $token | ||
20 | */ | ||
21 | public function handleText(&$token) | ||
22 | { | ||
23 | if (!$this->allowsElement('a')) { | ||
24 | return; | ||
25 | } | ||
26 | |||
27 | if (strpos($token->data, '://') === false) { | ||
28 | // our really quick heuristic failed, abort | ||
29 | // this may not work so well if we want to match things like | ||
30 | // "google.com", but then again, most people don't | ||
31 | return; | ||
32 | } | ||
33 | |||
34 | // there is/are URL(s). Let's split the string: | ||
35 | // Note: this regex is extremely permissive | ||
36 | $bits = preg_split('#((?:https?|ftp)://[^\s\'",<>()]+)#Su', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE); | ||
37 | |||
38 | |||
39 | $token = array(); | ||
40 | |||
41 | // $i = index | ||
42 | // $c = count | ||
43 | // $l = is link | ||
44 | for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) { | ||
45 | if (!$l) { | ||
46 | if ($bits[$i] === '') { | ||
47 | continue; | ||
48 | } | ||
49 | $token[] = new HTMLPurifier_Token_Text($bits[$i]); | ||
50 | } else { | ||
51 | $token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i])); | ||
52 | $token[] = new HTMLPurifier_Token_Text($bits[$i]); | ||
53 | $token[] = new HTMLPurifier_Token_End('a'); | ||
54 | } | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
59 | // vim: et sw=4 sts=4 | ||