From 44dd58d0d156f000ad0b1b7d57075f504c9775a8 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 15 Nov 2019 08:43:35 +0100 Subject: [PATCH] Disable autolink for composer --- .../java/eu/faircode/email/ActivityEML.java | 2 +- .../eu/faircode/email/AdapterMessage.java | 2 +- .../eu/faircode/email/EditTextCompose.java | 2 +- .../eu/faircode/email/FragmentCompose.java | 10 +- .../java/eu/faircode/email/HtmlHelper.java | 112 +++++++++--------- 5 files changed, 65 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityEML.java b/app/src/main/java/eu/faircode/email/ActivityEML.java index aac24354b2..8ed75a5420 100644 --- a/app/src/main/java/eu/faircode/email/ActivityEML.java +++ b/app/src/main/java/eu/faircode/email/ActivityEML.java @@ -139,7 +139,7 @@ public class ActivityEML extends ActivityBase { String html = result.parts.getHtml(context); if (html != null) - result.body = HtmlHelper.fromHtml(HtmlHelper.sanitize(context, html, false)); + result.body = HtmlHelper.fromHtml(HtmlHelper.sanitize(context, html, false, false)); return result; } diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 8a2564d4f5..c4b298c406 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -1449,7 +1449,7 @@ public class AdapterMessage extends RecyclerView.Adapter") + "

"; } else - html = HtmlHelper.sanitize(context, ref, true); + html = HtmlHelper.sanitize(context, ref, true, false); refFile.delete(); return body + html; @@ -2235,7 +2235,7 @@ public class FragmentCompose extends FragmentBase { data.draft.subject = args.getString("subject", ""); body = args.getString("body", ""); if (!TextUtils.isEmpty(body)) - body = HtmlHelper.sanitize(context, body, false); + body = HtmlHelper.sanitize(context, body, false, false); if (answer > 0) { EntityAnswer a = db.answer().getAnswer(answer); @@ -2309,7 +2309,7 @@ public class FragmentCompose extends FragmentBase { data.draft.subject = ref.subject; if (ref.content) { String html = Helper.readText(ref.getFile(context)); - body = HtmlHelper.sanitize(context, html, true); + body = HtmlHelper.sanitize(context, html, true, false); } } else if ("list".equals(action)) { data.draft.subject = ref.subject; @@ -2596,7 +2596,7 @@ public class FragmentCompose extends FragmentBase { if (data.draft.content) { File file = data.draft.getFile(context); String html = Helper.readText(file); - html = HtmlHelper.sanitize(context, html, true); + html = HtmlHelper.sanitize(context, html, true, false); Helper.writeText(file, html); } else { if (data.draft.uid == null) @@ -3316,7 +3316,7 @@ public class FragmentCompose extends FragmentBase { Spanned spannedRef = null; File refFile = draft.getRefFile(context); if (refFile.exists()) { - String quote = HtmlHelper.sanitize(context, Helper.readText(refFile), show_images); + String quote = HtmlHelper.sanitize(context, Helper.readText(refFile), show_images, false); Spanned spannedQuote = HtmlHelper.fromHtml(quote, new Html.ImageGetter() { @Override diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index c87ac0a3a7..a086c9e08d 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -80,9 +80,9 @@ public class HtmlHelper { private static final List tails = Collections.unmodifiableList(Arrays.asList( "h1", "h2", "h3", "h4", "h5", "h6", "p", "ol", "ul", "li")); - static String sanitize(Context context, String html, boolean show_images) { + static String sanitize(Context context, String html, boolean show_images, boolean autolink) { try { - return _sanitize(context, html, show_images); + return _sanitize(context, html, show_images, autolink); } catch (Throwable ex) { // OutOfMemoryError Log.e(ex); @@ -90,7 +90,7 @@ public class HtmlHelper { } } - private static String _sanitize(Context context, String html, boolean show_images) { + private static String _sanitize(Context context, String html, boolean show_images, boolean autolink) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean text_color = prefs.getBoolean("text_color", true); boolean display_hidden = prefs.getBoolean("display_hidden", false); @@ -450,69 +450,71 @@ public class HtmlHelper { } // Autolink - final Pattern pattern = Pattern.compile( - PatternsCompat.AUTOLINK_EMAIL_ADDRESS.pattern() + "|" + - PatternsCompat.AUTOLINK_WEB_URL.pattern()); - - NodeTraversor.traverse(new NodeVisitor() { - private int links = 0; - - @Override - public void head(Node node, int depth) { - if (links < MAX_AUTO_LINK && node instanceof TextNode) { - TextNode tnode = (TextNode) node; - String text = tnode.getWholeText(); - - Matcher matcher = pattern.matcher(text); - if (matcher.find()) { - Element span = document.createElement("span"); - - int pos = 0; - do { - boolean linked = false; - Node parent = tnode.parent(); - while (parent != null) { - if ("a".equals(parent.nodeName())) { - linked = true; - break; + if (autolink) { + final Pattern pattern = Pattern.compile( + PatternsCompat.AUTOLINK_EMAIL_ADDRESS.pattern() + "|" + + PatternsCompat.AUTOLINK_WEB_URL.pattern()); + + NodeTraversor.traverse(new NodeVisitor() { + private int links = 0; + + @Override + public void head(Node node, int depth) { + if (links < MAX_AUTO_LINK && node instanceof TextNode) { + TextNode tnode = (TextNode) node; + String text = tnode.getWholeText(); + + Matcher matcher = pattern.matcher(text); + if (matcher.find()) { + Element span = document.createElement("span"); + + int pos = 0; + do { + boolean linked = false; + Node parent = tnode.parent(); + while (parent != null) { + if ("a".equals(parent.nodeName())) { + linked = true; + break; + } + parent = parent.parent(); } - parent = parent.parent(); - } - boolean email = matcher.group().contains("@") && !matcher.group().contains(":"); - if (BuildConfig.DEBUG) - Log.i("Web url=" + matcher.group() + - " " + matcher.start() + "..." + matcher.end() + "/" + text.length() + - " linked=" + linked + " email=" + email + " count=" + links); + boolean email = matcher.group().contains("@") && !matcher.group().contains(":"); + if (BuildConfig.DEBUG) + Log.i("Web url=" + matcher.group() + + " " + matcher.start() + "..." + matcher.end() + "/" + text.length() + + " linked=" + linked + " email=" + email + " count=" + links); - if (linked) - span.appendText(text.substring(pos, matcher.end())); - else { - span.appendText(text.substring(pos, matcher.start())); + if (linked) + span.appendText(text.substring(pos, matcher.end())); + else { + span.appendText(text.substring(pos, matcher.start())); - Element a = document.createElement("a"); - a.attr("href", (email ? "mailto:" : "") + matcher.group()); - a.text(matcher.group()); - span.appendChild(a); + Element a = document.createElement("a"); + a.attr("href", (email ? "mailto:" : "") + matcher.group()); + a.text(matcher.group()); + span.appendChild(a); - links++; - } + links++; + } - pos = matcher.end(); - } while (links < MAX_AUTO_LINK && matcher.find()); + pos = matcher.end(); + } while (links < MAX_AUTO_LINK && matcher.find()); - span.appendText(text.substring(pos)); + span.appendText(text.substring(pos)); - tnode.before(span); - tnode.text(""); + tnode.before(span); + tnode.text(""); + } } } - } - @Override - public void tail(Node node, int depth) { - } - }, document); + @Override + public void tail(Node node, int depth) { + } + }, document); + } // Selective new lines for (Element div : document.select("div"))