1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-26 09:47:13 +00:00

Fixed encoding plain text messages

This commit is contained in:
M66B 2019-11-19 09:57:55 +01:00
parent d5a3a18722
commit 482c7eb92c
2 changed files with 67 additions and 66 deletions

View file

@ -135,7 +135,6 @@ public class HtmlHelper {
Whitelist whitelist = Whitelist.relaxed() Whitelist whitelist = Whitelist.relaxed()
.addTags("hr", "abbr", "big", "font") .addTags("hr", "abbr", "big", "font")
.addAttributes("pre", "plain")
.removeTags("col", "colgroup", "thead", "tbody") .removeTags("col", "colgroup", "thead", "tbody")
.removeAttributes("table", "width") .removeAttributes("table", "width")
.removeAttributes("td", "colspan", "rowspan", "width") .removeAttributes("td", "colspan", "rowspan", "width")
@ -268,70 +267,8 @@ public class HtmlHelper {
// Pre formatted text // Pre formatted text
for (Element pre : document.select("pre")) { for (Element pre : document.select("pre")) {
int level = 0; pre.html(formatPre(pre.wholeText()));
StringBuilder sb = new StringBuilder(); pre.tagName("tt");
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("<blockquote>");
line = line.substring(1); // >
if (line.startsWith(" "))
line = line.substring(1);
}
// Closing quotes
for (int i = 0; i < level - tlevel; i++)
sb.append("</blockquote>");
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);
// Space characters
int len = line.length();
for (int j = 0; j < len; j++) {
char kar = line.charAt(j);
if (kar == ' ') {
// Prevent trimming start
// Keep one space for word wrapping
if (j == 0 || (j + 1 < len && line.charAt(j + 1) == ' '))
sb.append("&nbsp;");
else
sb.append(' ');
} else
sb.append(kar);
}
sb.append("<br>");
}
// Closing quotes
for (int i = 0; i < level; i++)
sb.append("</blockquote>");
String plain = pre.attr("plain");
pre.tagName(Boolean.parseBoolean(plain) ? "div" : "tt");
pre.html(sb.toString());
} }
// Code // Code
@ -552,6 +489,70 @@ public class HtmlHelper {
return false; return false;
} }
static String formatPre(String text) {
int level = 0;
StringBuilder sb = new StringBuilder();
String[] lines = text.split("\\r?\\n");
for (String line : lines) {
// Opening quotes
int tlevel = 0;
while (line.startsWith(">")) {
tlevel++;
if (tlevel > level)
sb.append("<blockquote>");
line = line.substring(1); // >
if (line.startsWith(" "))
line = line.substring(1);
}
// Closing quotes
for (int i = 0; i < level - tlevel; i++)
sb.append("</blockquote>");
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);
// Space characters
int len = line.length();
for (int j = 0; j < len; j++) {
char kar = line.charAt(j);
if (kar == ' ') {
// Prevent trimming start
// Keep one space for word wrapping
if (j == 0 || (j + 1 < len && line.charAt(j + 1) == ' '))
sb.append("&nbsp;");
else
sb.append(' ');
} else
sb.append(kar);
}
sb.append("<br>");
}
// Closing quotes
for (int i = 0; i < level; i++)
sb.append("</blockquote>");
return sb.toString();
}
static void removeTrackingPixels(Context context, Document document) { static void removeTrackingPixels(Context context, Document document) {
Drawable d = ContextCompat.getDrawable(context, R.drawable.baseline_my_location_24); Drawable d = ContextCompat.getDrawable(context, R.drawable.baseline_my_location_24);
d.setTint(Helper.resolveColor(context, R.attr.colorWarning)); d.setTint(Helper.resolveColor(context, R.attr.colorWarning));

View file

@ -1045,7 +1045,7 @@ public class MessageHelper {
} }
if (part == plain) if (part == plain)
result = "<pre plain=\"true\">" + result + "</pre>"; result = "<div>" + HtmlHelper.formatPre(result) + "</div>";
return result; return result;
} }