Truncate viewed text only

This commit is contained in:
M66B 2020-03-25 20:25:06 +01:00
parent 5a730c246b
commit 20fe6314ad
5 changed files with 36 additions and 27 deletions

View File

@ -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());
}

View File

@ -1858,7 +1858,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
return document.html();
} else {
// Cleanup message
document = HtmlHelper.sanitize(context, document, show_images, true, true);
document = HtmlHelper.sanitizeView(context, document, show_images);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
args.putParcelable("actions", getConversationActions(message, document));

View File

@ -78,7 +78,7 @@ public class EditTextCompose extends AppCompatEditText {
return false;
html = "<div>" + HtmlHelper.formatPre(text.toString()) + "</div>";
}
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);

View File

@ -880,7 +880,7 @@ public class FragmentCompose extends FragmentBase {
p.html(TextUtils.join("<br>", 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

View File

@ -237,14 +237,10 @@ 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 sanitize(Context context, Document parsed, boolean show_images, boolean autolink, boolean more) {
static Document sanitizeCompose(Context context, String html, boolean show_images) {
try {
return _sanitize(context, parsed, show_images, autolink, more);
Document parsed = JsoupEx.parse(html);
return sanitize(context, parsed, false, show_images);
} catch (Throwable ex) {
// OutOfMemoryError
Log.e(ex);
@ -256,7 +252,21 @@ public class HtmlHelper {
}
}
private 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, true, 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;
}
}
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());