Do not linkify trailing dot or comma

When using urls in sentences, those urls are often followed by a dot,
or a comma. Like:

  You can find more information at http://example.com/foo.html.

While "http://example.com/foo.html." would be a perfectly fine url,
most of the time, the link is meant to not include trailing dot or
trailing comma (like in the above example, the link should only link

  http://example.com/foo.html

and not

  http://example.com/foo.html.

). As linkifying trailing dots and trailing commas does more harm than
good, we only treat dots and commas as being part of urls, if they are
neither followed by whitespace nor occur at the end of a string.

Change-Id: I9a7e42e53f8c7ac746af45967beac380705a2711
This commit is contained in:
Christian Aistleitner 2014-07-15 10:20:57 +02:00 committed by David Pursehouse
parent 77167754ef
commit e81515cd9f
2 changed files with 34 additions and 1 deletions

View File

@ -122,8 +122,9 @@ public abstract class SafeHtml
/** Convert bare http:// and https:// URLs into <a href> tags. */ /** Convert bare http:// and https:// URLs into <a href> tags. */
public SafeHtml linkify() { public SafeHtml linkify() {
final String part = "(?:" + final String part = "(?:" +
"[a-zA-Z0-9$_.+!*',%;:@=?#/~-]" + "[a-zA-Z0-9$_+!*'%;:@=?#/~-]" +
"|&(?!lt;|gt;)" + "|&(?!lt;|gt;)" +
"|[.,](?!(?:\\s|$))" +
")"; ")";
return replaceAll( return replaceAll(
"(https?://" + "(https?://" +

View File

@ -60,6 +60,38 @@ public class SafeHtml_LinkifyTest {
assertEquals("A &lt;<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>&gt; B", n.asString()); assertEquals("A &lt;<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>&gt; B", n.asString());
} }
@Test
public void testLinkify_TrailingPlainLetter() {
final SafeHtml o = html("A http://go.here/foo B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/foo\" target=\"_blank\">http://go.here/foo</a> B", n.asString());
}
@Test
public void testLinkify_TrailingDot() {
final SafeHtml o = html("A http://go.here/. B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>. B", n.asString());
}
@Test
public void testLinkify_TrailingComma() {
final SafeHtml o = html("A http://go.here/, B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>, B", n.asString());
}
@Test
public void testLinkify_TrailingDotDot() {
final SafeHtml o = html("A http://go.here/.. B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/.\" target=\"_blank\">http://go.here/.</a>. B", n.asString());
}
private static SafeHtml html(String text) { private static SafeHtml html(String text) {
return new SafeHtmlBuilder().append(text).toSafeHtml(); return new SafeHtmlBuilder().append(text).toSafeHtml();
} }