From 695200978210040a97b601f4310dfbe8e64b0fdb Mon Sep 17 00:00:00 2001 From: Roland Illig Date: Sat, 16 Feb 2019 20:45:13 +0100 Subject: [PATCH] Fixed explosion of generated autolink text --- .../java/eu/faircode/email/HtmlHelper.java | 6 ++- .../eu/faircode/email/HtmlHelperTest.java | 44 ++----------------- 2 files changed, 8 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index cc7f533974..7d436bb3f0 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -127,11 +127,13 @@ public class HtmlHelper { static String autolink(String text) { Matcher matcher = pattern.matcher(text); + StringBuffer sb = new StringBuffer(); while (matcher.find()) { String ref = matcher.group(); - text = text.replace(ref, String.format("%s", ref, ref)); + matcher.appendReplacement(sb, String.format("%s", ref, ref)); } - return text; + matcher.appendTail(sb); + return sb.toString(); } static Drawable decodeImage(String source, Context context, long id, boolean show) { diff --git a/app/src/test/java/eu/faircode/email/HtmlHelperTest.java b/app/src/test/java/eu/faircode/email/HtmlHelperTest.java index 172fd181bd..21f804b400 100644 --- a/app/src/test/java/eu/faircode/email/HtmlHelperTest.java +++ b/app/src/test/java/eu/faircode/email/HtmlHelperTest.java @@ -50,59 +50,23 @@ class HtmlHelperTest { testAutolink( "http://example.org/ and http://example.org/subdir/", - // FIXME: Each URL must be linked to its exact address, not just to a prefix. + // Each URL must be linked to its exact address, not just to a prefix. "" + "http://example.org/ and " + - "http://example.org/subdir/" + "http://example.org/subdir/" ); testAutolink( "http://example.org/ and http://example.org/ and http://example.org/", - // FIXME: Even when the same URL is mentioned multiple times, + // When the same URL is mentioned multiple times, // each of the URLs must only appear a single time. "" + - "http://example.org/" + - "\">" + - "http://example.org/" + - "" + - "\">" + - "http://example.org/" + - "\">" + - "http://example.org/" + - "" + - "" + " and " + - "http://example.org/" + - "\">" + - "http://example.org/" + - "" + - "\">" + - "http://example.org/" + - "\">" + - "http://example.org/" + - "" + - "" + " and " + - "http://example.org/" + - "\">" + - "http://example.org/" + - "" + - "\">" + - "http://example.org/" + - "\">" + - "http://example.org/" + - "" + - "" + "http://example.org/" ); }