Fixed/simplified message truncation

This commit is contained in:
M66B 2020-11-01 19:17:07 +01:00
parent 629ea43e5f
commit c65dedaed7
1 changed files with 23 additions and 29 deletions

View File

@ -1816,42 +1816,36 @@ public class HtmlHelper {
} }
static boolean truncate(Document d, boolean reformat) { static boolean truncate(Document d, boolean reformat) {
final int[] length = new int[1];
int max = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE); int max = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE);
int length = 0; NodeTraversor.filter(new NodeFilter() {
int images = 0; @Override
for (Element elm : d.select("*")) { public FilterResult head(Node node, int depth) {
if ("img".equals(elm.tagName())) if (length[0] >= max)
images++; return FilterResult.REMOVE;
else if (node instanceof TextNode) {
boolean skip = false; TextNode tnode = ((TextNode) node);
for (Node child : elm.childNodes()) {
if (child instanceof TextNode) {
TextNode tnode = ((TextNode) child);
String text = tnode.getWholeText(); String text = tnode.getWholeText();
if (length[0] + text.length() >= max) {
if (length < max) { text = text.substring(0, max - length[0]) + " ...";
if (length + text.length() >= max) { tnode.text(text);
text = text.substring(0, max - length) + " ..."; length[0] += text.length();
tnode.text(text); return FilterResult.SKIP_ENTIRELY;
skip = true; } else
} length[0] += text.length();
} else {
if (skip)
tnode.text("");
}
length += text.length();
} }
return FilterResult.CONTINUE;
} }
if (length >= max && !skip) @Override
elm.remove(); public FilterResult tail(Node node, int depth) {
} return FilterResult.CONTINUE;
}
}, d);
Log.i("Message size=" + length + " images=" + images); Log.i("Message size=" + length[0]);
return (length[0] > max);
return (length >= max);
} }
static boolean contains(Document d, String[] texts) { static boolean contains(Document d, String[] texts) {