diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 00951ccddb..e1e80b4c8a 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -111,11 +111,7 @@ public class HtmlHelper { public void head(Node node, int depth) { if (node instanceof TextNode) { String text = Html.escapeHtml(((TextNode) node).text()); - Matcher matcher = pattern.matcher(text); - while (matcher.find()) { - String ref = matcher.group(); - text = text.replace(ref, String.format("%s", ref, ref)); - } + text = autolink(text); node.before(text); ((TextNode) node).text(""); } @@ -129,6 +125,15 @@ public class HtmlHelper { return document.body().html(); } + static String autolink(String text) { + Matcher matcher = pattern.matcher(text); + while (matcher.find()) { + String ref = matcher.group(); + text = text.replace(ref, String.format("%s", ref, ref)); + } + return text; + } + static Drawable decodeImage(String source, Context context, long id, boolean show) { int px = Helper.dp2pixels(context, 48); diff --git a/app/src/test/java/eu/faircode/email/HtmlHelperTest.java b/app/src/test/java/eu/faircode/email/HtmlHelperTest.java new file mode 100644 index 0000000000..ee1f9d04b6 --- /dev/null +++ b/app/src/test/java/eu/faircode/email/HtmlHelperTest.java @@ -0,0 +1,56 @@ +package eu.faircode.email; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class HtmlHelperTest { + + @Test + void autolink() { + + testAutolink( + "To visit http://www.example.org, go to http://www.example.org.", + + "" + + // FIXME: The trailing comma must not be part of the URL. + "To visit http://www.example.org, " + + // FIXME: The trailing dot must not be part of the URL. + "go to http://www.example.org."); + + testAutolink( + "one hhhhh|spt://example.org three", + + // FIXME: "hhhhh|spt" is not a proper URL scheme. + "one hhhhh|spt://example.org three" + ); + + testAutolink( + "https://example.org/search?q=%C3%A4&hl=nl", + + // TODO: Strictly speaking, the & should be encoded as &. + // Most browsers can deal with this situation though. + "" + + "https://example.org/search?q=%C3%A4&hl=nl" + ); + + testAutolink( + "Go to \"http://example.org/\".", + + // FIXME: The quote must not end up as part of the URL. + "Go to \"http://example.org/\"." + ); + + testAutolink( + "Go to .", + + // FIXME: The < must be encoded as <. + // FIXME: THe > must not end up as part of the URL. + "Go to <.\">http://example.org/>." + ); + } + + private void testAutolink(String input, String expectedOutput) { + assertThat(HtmlHelper.autolink(input)).isEqualTo(expectedOutput); + } +}