]> Sergey Matveev's repositories - public-inbox.git/commitdiff
linkify: be stricter about matching RFC 3986
authorEric Wong <e@80x24.org>
Thu, 18 Aug 2016 02:02:50 +0000 (02:02 +0000)
committerEric Wong <e@80x24.org>
Thu, 18 Aug 2016 02:03:41 +0000 (02:03 +0000)
We're not to-the-letter about percent-encoding, but
we should allow all the characters.  This is mainly
so we can effectively use the link to some Wikipedia
pages with parentheses in them:

https://en.wikipedia.org/wiki/Atom_(standard)
https://en.wikipedia.org/wiki/Git_(software)

lib/PublicInbox/Linkify.pm
t/linkify.t

index d4df689e495ad8fd5babfec0989415c10a7e4ad3..ea7fd71f13ca6d7f8a0909b271be95a03be1b2aa 100644 (file)
@@ -17,7 +17,10 @@ use Digest::SHA qw/sha1_hex/;
 my $SALT = rand;
 my $LINK_RE = qr{\b((?:ftps?|https?|nntps?|gopher)://
                 [\@:\w\.-]+/
-                ?[!,:~\$\@\w\+\&\?\.\%\;/#=-]*)}x;
+                (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
+                (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
+                (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
+                )}xi;
 
 sub new { bless {}, shift }
 
index 586691ae6474bb4eb8424189040e896597f5105c..49cbbd64edf5d18ca1892f91abd111afed88407e 100644 (file)
@@ -23,4 +23,38 @@ use PublicInbox::Linkify;
        is($s, qq(<a\nhref="$u">$u</a>;), 'trailing semicolon not in URL');
 }
 
+{
+       my $l = PublicInbox::Linkify->new;
+       my $u = 'http://example.com/url-with-(parens)';
+       my $s = "hello $u world";
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       is($s, qq(hello <a\nhref="$u">$u</a> world), 'URL preserved');
+
+       $u .= "?query=a";
+       $s = "hello $u world";
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       is($s, qq(hello <a\nhref="$u">$u</a> world), 'query preserved');
+
+       $u .= "#fragment";
+       $s = "hello $u world";
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       is($s, qq(hello <a\nhref="$u">$u</a> world),
+         'query + fragment preserved');
+
+       $u = "http://example.com/";
+       $s = "hello $u world";
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       is($s, qq(hello <a\nhref="$u">$u</a> world), "root URL preserved");
+
+       $u = "http://example.com/#fragment";
+       $s = "hello $u world";
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       is($s, qq(hello <a\nhref="$u">$u</a> world), "root + fragment");
+}
+
 done_testing();