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) {
text = text.substring(0, max - length) + " ...";
tnode.text(text); tnode.text(text);
skip = true; length[0] += text.length();
return FilterResult.SKIP_ENTIRELY;
} else
length[0] += text.length();
} }
} else { return FilterResult.CONTINUE;
if (skip)
tnode.text("");
} }
length += text.length(); @Override
} public FilterResult tail(Node node, int depth) {
return FilterResult.CONTINUE;
} }
}, d);
if (length >= max && !skip) Log.i("Message size=" + length[0]);
elm.remove(); return (length[0] > max);
}
Log.i("Message size=" + length + " images=" + images);
return (length >= max);
} }
static boolean contains(Document d, String[] texts) { static boolean contains(Document d, String[] texts) {