Apply spans reversed

This commit is contained in:
M66B 2020-04-24 18:05:50 +02:00
parent 5adee09a3f
commit 7628909dd0
1 changed files with 31 additions and 12 deletions

View File

@ -1767,6 +1767,7 @@ public class HtmlHelper {
if (experiments) { if (experiments) {
// https://developer.android.com/guide/topics/text/spans // https://developer.android.com/guide/topics/text/spans
SpannableStringBuilder ssb = new SpannableStringBuilder(); SpannableStringBuilder ssb = new SpannableStringBuilder();
List<SpanHolder> holders = new ArrayList<>();
NodeTraversor.traverse(new NodeVisitor() { NodeTraversor.traverse(new NodeVisitor() {
@Override @Override
@ -1781,7 +1782,7 @@ public class HtmlHelper {
? context.getDrawable(R.drawable.baseline_broken_image_24) ? context.getDrawable(R.drawable.baseline_broken_image_24)
: imageGetter.getDrawable(src)); : imageGetter.getDrawable(src));
ssb.append("\uFFFC"); // Object replacement character ssb.append("\uFFFC"); // Object replacement character
ssb.setSpan(new ImageSpan(d, src), ssb.length() - 1, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new ImageSpan(d, src), ssb.length() - 1, ssb.length()));
break; break;
} }
} else if (node instanceof TextNode) { } else if (node instanceof TextNode) {
@ -1798,22 +1799,22 @@ public class HtmlHelper {
switch (element.tagName()) { switch (element.tagName()) {
case "a": case "a":
String href = element.attr("href"); String href = element.attr("href");
ssb.setSpan(new URLSpan(href), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new URLSpan(href), start, ssb.length()));
break; break;
case "body": case "body":
// Do nothing // Do nothing
break; break;
case "big": case "big":
ssb.setSpan(new RelativeSizeSpan(FONT_LARGE), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new RelativeSizeSpan(FONT_LARGE), start, ssb.length()));
break; break;
case "blockquote": case "blockquote":
ssb.setSpan(new QuoteSpan(), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new QuoteSpan(), start, ssb.length()));
break; break;
case "br": case "br":
ssb.append("\n"); ssb.append("\n");
break; break;
case "em": case "em":
ssb.setSpan(new StyleSpan(Typeface.ITALIC), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new StyleSpan(Typeface.ITALIC), start, ssb.length()));
break; break;
case "h1": case "h1":
case "h2": case "h2":
@ -1822,23 +1823,23 @@ public class HtmlHelper {
case "h5": case "h5":
case "h6": case "h6":
int level = element.tagName().charAt(1) - '1'; int level = element.tagName().charAt(1) - '1';
ssb.setSpan(new RelativeSizeSpan(HEADING_SIZES[level]), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new RelativeSizeSpan(HEADING_SIZES[level]), start, ssb.length()));
ssb.setSpan(new StyleSpan(Typeface.BOLD), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new StyleSpan(Typeface.BOLD), start, ssb.length()));
break; break;
case "img": case "img":
// Do nothing // Do nothing
break; break;
case "small": case "small":
ssb.setSpan(new RelativeSizeSpan(FONT_SMALL), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new RelativeSizeSpan(FONT_SMALL), start, ssb.length()));
break; break;
case "span": case "span":
// Do nothing // Do nothing
break; break;
case "strong": case "strong":
ssb.setSpan(new StyleSpan(Typeface.BOLD), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new StyleSpan(Typeface.BOLD), start, ssb.length()));
break; break;
case "u": case "u":
ssb.setSpan(new UnderlineSpan(), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new UnderlineSpan(), start, ssb.length()));
break; break;
default: default:
Log.e("Unknown tag=" + element.tagName()); Log.e("Unknown tag=" + element.tagName());
@ -1854,11 +1855,11 @@ public class HtmlHelper {
switch (key) { switch (key) {
case "color": case "color":
int color = Integer.parseInt(value.substring(1), 16) | 0xFF000000; int color = Integer.parseInt(value.substring(1), 16) | 0xFF000000;
ssb.setSpan(new ForegroundColorSpan(color), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new ForegroundColorSpan(color), start, ssb.length()));
break; break;
case "text-decoration": case "text-decoration":
if ("line-through".equals(value)) if ("line-through".equals(value))
ssb.setSpan(new StrikethroughSpan(), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holders.add(new SpanHolder(new StrikethroughSpan(), start, ssb.length()));
break; break;
} }
} }
@ -1867,6 +1868,10 @@ public class HtmlHelper {
} }
}, document.body()); }, document.body());
Collections.reverse(holders);
for (SpanHolder holder : holders)
ssb.setSpan(holder.span, holder.start, holder.end, holder.flags);
return ssb; return ssb;
} else } else
return fromHtml(document.html(), imageGetter, null); return fromHtml(document.html(), imageGetter, null);
@ -1925,4 +1930,18 @@ public class HtmlHelper {
spanned.getSpanFlags(spans[i])); spanned.getSpanFlags(spans[i]));
return reverse; return reverse;
} }
private static class SpanHolder {
Object span;
int start;
int end;
int flags;
SpanHolder(Object span, int start, int end) {
this.span = span;
this.start = start;
this.end = end;
this.flags = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
}
}
} }