diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 124c946d45..c87ac0a3a7 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -71,6 +71,7 @@ public class HtmlHelper { private static final int PREVIEW_SIZE = 500; // characters private static final float MIN_LUMINANCE = 0.5f; + private static final int TAB_SIZE = 2; private static final int MAX_AUTO_LINK = 250; private static final int TRACKING_PIXEL_SURFACE = 25; // pixels @@ -266,31 +267,68 @@ public class HtmlHelper { // Pre formatted text for (Element pre : document.select("pre")) { - Element div = document.createElement("font"); - div.attr("face", "monospace"); - + int level = 0; + StringBuilder sb = new StringBuilder(); String[] lines = pre.wholeText().split("\\r?\\n"); for (String line : lines) { + // Opening quotes + int tlevel = 0; + while (line.startsWith(">")) { + tlevel++; + if (tlevel > level) + sb.append("
"); + + line = line.substring(1); // > + + if (line.startsWith(" ")) + line = line.substring(1); + } + + // Closing quotes + for (int i = 0; i < level - tlevel; i++) + sb.append("
"); + level = tlevel; + + // Tabs characters + StringBuilder l = new StringBuilder(); + for (int j = 0; j < line.length(); j++) { + char kar = line.charAt(j); + if (kar == '\t') { + l.append(' '); + while (l.length() % TAB_SIZE != 0) + l.append(' '); + } else + l.append(kar); + } + line = l.toString(); + + // Html characters line = Html.escapeHtml(line); - StringBuilder sb = new StringBuilder(); + // Space characters int len = line.length(); for (int j = 0; j < len; j++) { char kar = line.charAt(j); - if (kar == ' ' && - j + 1 < len && line.charAt(j + 1) == ' ') - sb.append(" "); - else + if (kar == ' ') { + // Prevent trimming start + // Keep one space for word wrapping + if (j == 0 || (j + 1 < len && line.charAt(j + 1) == ' ')) + sb.append(" "); + else + sb.append(' '); + } else sb.append(kar); } - Element span = document.createElement("span"); - span.html(sb.toString()); - div.appendChild(span); - div.appendElement("br"); + sb.append("
"); } - pre.replaceWith(div); + // Closing quotes + for (int i = 0; i < level; i++) + sb.append(""); + + pre.tagName("tt"); // monospace + pre.html(sb.toString()); } // Code diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index fbdf866760..164f2ff182 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -23,7 +23,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.net.MailTo; import android.net.Uri; -import android.text.Html; import android.text.TextUtils; import androidx.preference.PreferenceManager; @@ -1041,60 +1040,8 @@ public class MessageHelper { warnings.add(Helper.formatThrowable(ex, false)); } - // Prevent Jsoup throwing an exception - if (part == plain) { - StringBuilder sb = new StringBuilder(); - sb.append(""); - - int level = 0; - String[] lines = result.split("\\r?\\n"); - for (String line : lines) { - // Opening quotes - int tlevel = 0; - while (line.startsWith(">")) { - tlevel++; - if (tlevel > level) - sb.append("
"); - - line = line.substring(1); // > - - if (line.startsWith(" ")) - line = line.substring(1); - } - - // Closing quotes - for (int i = 0; i < level - tlevel; i++) - sb.append("
"); - level = tlevel; - - // Show as-is - line = Html.escapeHtml(line); - - // Non breaking spaces - boolean start = true; - int len = line.length(); - for (int j = 0; j < len; j++) { - char kar = line.charAt(j); - if (kar == ' ' && - (start || j + 1 < len && line.charAt(j + 1) == ' ')) - sb.append(" "); - else { - start = false; - sb.append(kar); - } - } - - sb.append("
"); - } - - // Closing quotes - for (int i = 0; i < level; i++) - sb.append(""); - - sb.append("
"); - - result = sb.toString(); - } + if (part == plain) + result = "
" + result + "
"; return result; }