From cdd7e8686c352002a3804d28f54e266ff9b2cfae Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 16 Feb 2020 10:08:46 +0100 Subject: [PATCH] Truncate large texts --- .../java/eu/faircode/email/HtmlHelper.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 71d303c70c..73483daa1a 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -1215,17 +1215,40 @@ public class HtmlHelper { } static boolean truncate(Document d, boolean reformat) { - int at = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE); + int max = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE); int length = 0; for (Element elm : d.select("*")) { - for (Node child : elm.childNodes()) - if (child instanceof TextNode) - length += ((TextNode) child).text().length(); - if (length > at) + boolean skip = false; + + for (Node child : elm.childNodes()) { + if (child instanceof TextNode) { + TextNode tnode = ((TextNode) child); + String text = tnode.getWholeText(); + + if (length < max) { + if (length + text.length() >= max) { + text = text.substring(0, max - length) + " ..."; + tnode.text(text); + skip = true; + } + } else { + if (skip) + child.remove(); + } + + length += text.length(); + } else { + if (skip) + child.remove(); + } + } + + if (length >= max && !skip) elm.remove(); } - return (length > at); + + return (length >= max); } static Spanned fromHtml(@NonNull String html) {