diff --git a/app/src/main/java/eu/faircode/email/ActivityEML.java b/app/src/main/java/eu/faircode/email/ActivityEML.java index 04f2ff3ec3..856036fdfa 100644 --- a/app/src/main/java/eu/faircode/email/ActivityEML.java +++ b/app/src/main/java/eu/faircode/email/ActivityEML.java @@ -177,7 +177,8 @@ public class ActivityEML extends ActivityBase { String html = result.parts.getHtml(context); if (html != null) { - Document document = HtmlHelper.sanitize(context, html, false, true); + Document parsed = JsoupEx.parse(html); + Document document = HtmlHelper.sanitizeView(context, parsed, false); result.body = HtmlHelper.fromHtml(document.html()); } diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index ee99175d54..1223a80e00 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -1858,7 +1858,7 @@ public class AdapterMessage extends RecyclerView.Adapter= Build.VERSION_CODES.Q) args.putParcelable("actions", getConversationActions(message, document)); diff --git a/app/src/main/java/eu/faircode/email/EditTextCompose.java b/app/src/main/java/eu/faircode/email/EditTextCompose.java index bab7e5d7b9..7c06cb7f61 100644 --- a/app/src/main/java/eu/faircode/email/EditTextCompose.java +++ b/app/src/main/java/eu/faircode/email/EditTextCompose.java @@ -78,7 +78,7 @@ public class EditTextCompose extends AppCompatEditText { return false; html = "
" + HtmlHelper.formatPre(text.toString()) + "
"; } - Document document = HtmlHelper.sanitize(context, html, false, false); + Document document = HtmlHelper.sanitizeCompose(context, html, false); Spanned paste = HtmlHelper.fromHtml(document.html()); int colorPrimary = Helper.resolveColor(context, R.attr.colorPrimary); diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 8fc32d8f5f..e130f6687b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -880,7 +880,7 @@ public class FragmentCompose extends FragmentBase { p.html(TextUtils.join("
", line)); document.body().appendChild(p); } else { - Document d = HtmlHelper.sanitize(context, ref.outerHtml(), true, false); + Document d = HtmlHelper.sanitizeCompose(context, ref.outerHtml(), true); Element b = d.body(); b.tagName("div"); document.body().appendChild(b); @@ -2983,7 +2983,7 @@ public class FragmentCompose extends FragmentBase { data.draft.subject = args.getString("subject", ""); String b = args.getString("body", ""); if (!TextUtils.isEmpty(b)) { - Document d = HtmlHelper.sanitize(context, b, false, false); + Document d = HtmlHelper.sanitizeCompose(context, b, false); Element e = d.body(); e.tagName("div"); document.body().appendChild(e); @@ -3105,7 +3105,7 @@ public class FragmentCompose extends FragmentBase { data.draft.subject = ref.subject; if (ref.content) { String html = Helper.readText(ref.getFile(context)); - Document d = HtmlHelper.sanitize(context, html, true, false); + Document d = HtmlHelper.sanitizeCompose(context, html, true); Element e = d.body(); e.tagName("div"); document.body().appendChild(e); @@ -3391,7 +3391,7 @@ public class FragmentCompose extends FragmentBase { refFile.delete(); } - Document document = HtmlHelper.sanitize(context, doc.html(), true, false); + Document document = HtmlHelper.sanitizeCompose(context, doc.html(), true); EntityIdentity identity = null; if (data.draft.identity != null) @@ -3789,7 +3789,7 @@ public class FragmentCompose extends FragmentBase { if (body == null) b = Document.createShell(""); else - b = HtmlHelper.sanitize(context, body, true, false); + b = HtmlHelper.sanitizeCompose(context, body, true); if (TextUtils.isEmpty(body) || !b.body().html().equals(doc.body().html()) || @@ -4230,7 +4230,7 @@ public class FragmentCompose extends FragmentBase { Spanned spannedRef = null; if (!ref.isEmpty()) { - Document quote = HtmlHelper.sanitize(context, ref.outerHtml(), show_images, false); + Document quote = HtmlHelper.sanitizeCompose(context, ref.outerHtml(), show_images); Spanned spannedQuote = HtmlHelper.fromHtml(quote.html(), 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 952371beb0..f3a9e94f36 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -237,14 +237,24 @@ public class HtmlHelper { x11ColorMap.put("yellowgreen", 0x9ACD32); } - static Document sanitize(Context context, String html, boolean show_images, boolean autolink) { - Document parsed = JsoupEx.parse(html); - return sanitize(context, parsed, show_images, autolink, false); + static Document sanitizeCompose(Context context, String html, boolean show_images) { + try { + Document parsed = JsoupEx.parse(html); + return sanitize(context, parsed, false, show_images); + } catch (Throwable ex) { + // OutOfMemoryError + Log.e(ex); + Document document = Document.createShell(""); + Element strong = document.createElement("strong"); + strong.text(Log.formatThrowable(ex)); + document.body().appendChild(strong); + return document; + } } - static Document sanitize(Context context, Document parsed, boolean show_images, boolean autolink, boolean more) { + static Document sanitizeView(Context context, Document parsed, boolean show_images) { try { - return _sanitize(context, parsed, show_images, autolink, more); + return sanitize(context, parsed, true, show_images); } catch (Throwable ex) { // OutOfMemoryError Log.e(ex); @@ -256,7 +266,7 @@ public class HtmlHelper { } } - private static Document _sanitize(Context context, Document parsed, boolean show_images, boolean autolink, boolean more) { + private static Document sanitize(Context context, Document parsed, boolean view, boolean show_images) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean text_color = prefs.getBoolean("text_color", true); boolean text_size = prefs.getBoolean("text_size", true); @@ -328,20 +338,18 @@ public class HtmlHelper { } // Limit length - if (truncate(parsed, true)) { + if (view && truncate(parsed, true)) { parsed.body() .appendElement("br") .appendElement("p") .appendElement("em") .text(context.getString(R.string.title_too_large)); - - if (more) - parsed.body() - .appendElement("p") - .appendElement("big") - .appendElement("a") - .attr("href", "full:") - .text(context.getString(R.string.title_show_full)); + parsed.body() + .appendElement("p") + .appendElement("big") + .appendElement("a") + .attr("href", "full:") + .text(context.getString(R.string.title_show_full)); } Whitelist whitelist = Whitelist.relaxed() @@ -688,7 +696,7 @@ public class HtmlHelper { } // Autolink - if (autolink) { + if (view) { final Pattern pattern = Pattern.compile( PatternsCompat.AUTOLINK_EMAIL_ADDRESS.pattern() + "|" + PatternsCompat.AUTOLINK_WEB_URL.pattern());