HtmlEx: added debug logging

pull/209/head
M66B 3 years ago
parent 933ad6a991
commit 16fc4360c6

@ -141,36 +141,33 @@ public class HtmlEx {
int n1 = text.nextSpanTransition(i, end, QuoteSpan.class); int n1 = text.nextSpanTransition(i, end, QuoteSpan.class);
int n2 = text.nextSpanTransition(i, end, eu.faircode.email.IndentSpan.class); int n2 = text.nextSpanTransition(i, end, eu.faircode.email.IndentSpan.class);
next = Math.min(n1, n2); next = Math.min(n1, n2);
if (next > end) { try {
StringBuilder sb = new StringBuilder(); List<Object> spans = new ArrayList<>();
TextUtils.dumpSpans(text, new StringBuilderPrinter(sb), "withinDiv "); for (Object span : getSpans(text, i, next, LeadingMarginSpan.class))
sb.append(" next=").append(next); if (span instanceof QuoteSpan ||
sb.append(" start=").append(start); span instanceof eu.faircode.email.IndentSpan)
sb.append(" end=").append(end); spans.add(span);
eu.faircode.email.Log.e(sb.toString());
next = end; for (Object span : spans) {
} if (span instanceof QuoteSpan)
List<Object> spans = new ArrayList<>(); out.append("<blockquote style=\"")
for (Object span : getSpans(text, i, next, LeadingMarginSpan.class)) .append(eu.faircode.email.HtmlHelper.getQuoteStyle(text, next, end))
if (span instanceof QuoteSpan || .append("\">");
span instanceof eu.faircode.email.IndentSpan) else if (span instanceof eu.faircode.email.IndentSpan)
spans.add(span); out.append("<blockquote style=\"")
.append(eu.faircode.email.HtmlHelper.getIndentStyle(text, next, end))
for (Object span : spans) { .append("\">");
if (span instanceof QuoteSpan) }
out.append("<blockquote style=\"")
.append(eu.faircode.email.HtmlHelper.getQuoteStyle(text, next, end)) withinBlockquote(out, text, i, next, option);
.append("\">");
else if (span instanceof eu.faircode.email.IndentSpan) for (Object span : spans) {
out.append("<blockquote style=\"") out.append("</blockquote>\n");
.append(eu.faircode.email.HtmlHelper.getIndentStyle(text, next, end)) }
.append("\">"); } catch (Throwable ex) {
} Log.e("withinDiv " + start + "..." + end + "/" + text.length() +
" i=" + i + " n1=" + n1 + " n2=" + n2);
withinBlockquote(out, text, i, next, option); throw ex;
for (Object span : spans) {
out.append("</blockquote>\n");
} }
} }
} }
@ -248,85 +245,91 @@ public class HtmlEx {
int next; int next;
for (int i = start; i <= end; i = next) { for (int i = start; i <= end; i = next) {
next = TextUtils.indexOf(text, '\n', i, end); next = TextUtils.indexOf(text, '\n', i, end);
if (next < 0) { try {
next = end; if (next < 0) {
} next = end;
if (next == i) {
if (levels.size() > 0) {
// Current paragraph is no longer a list item; close the previously opened list
for (int l = levels.size() - 1; l >= 0; l--)
out.append(levels.get(l) ? "</ul>\n" : "</ol>\n");
levels.clear();
}
if (i != text.length())
out.append("<br>\n");
} else {
eu.faircode.email.LineSpan[] line = getSpans(text, i, next, eu.faircode.email.LineSpan.class);
if (line.length > 0) {
for (int l = 0; l < line.length; l++)
out.append("<hr>");
continue;
} }
int level = 0; if (next == i) {
Boolean isBulletListItem = null; if (levels.size() > 0) {
ParagraphStyle[] paragraphStyles = getSpans(text, i, next, ParagraphStyle.class); // Current paragraph is no longer a list item; close the previously opened list
for (ParagraphStyle paragraphStyle : paragraphStyles) { for (int l = levels.size() - 1; l >= 0; l--)
final int spanFlags = text.getSpanFlags(paragraphStyle); out.append(levels.get(l) ? "</ul>\n" : "</ol>\n");
if ((spanFlags & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH levels.clear();
&& paragraphStyle instanceof BulletSpan) { }
isBulletListItem = !(paragraphStyle instanceof eu.faircode.email.NumberSpan); if (i != text.length())
if (paragraphStyle instanceof NumberSpan) out.append("<br>\n");
level = ((NumberSpan) paragraphStyle).getLevel(); } else {
else if (paragraphStyle instanceof BulletSpanEx) eu.faircode.email.LineSpan[] line = getSpans(text, i, next, eu.faircode.email.LineSpan.class);
level = ((BulletSpanEx) paragraphStyle).getLevel(); if (line.length > 0) {
break; for (int l = 0; l < line.length; l++)
out.append("<hr>");
continue;
} }
}
if (isBulletListItem == null) int level = 0;
level = -1; Boolean isBulletListItem = null;
ParagraphStyle[] paragraphStyles = getSpans(text, i, next, ParagraphStyle.class);
for (ParagraphStyle paragraphStyle : paragraphStyles) {
final int spanFlags = text.getSpanFlags(paragraphStyle);
if ((spanFlags & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH
&& paragraphStyle instanceof BulletSpan) {
isBulletListItem = !(paragraphStyle instanceof eu.faircode.email.NumberSpan);
if (paragraphStyle instanceof NumberSpan)
level = ((NumberSpan) paragraphStyle).getLevel();
else if (paragraphStyle instanceof BulletSpanEx)
level = ((BulletSpanEx) paragraphStyle).getLevel();
break;
}
}
while (levels.size() > level + 1) { if (isBulletListItem == null)
Boolean bullet = levels.remove(levels.size() - 1); level = -1;
out.append(bullet ? "</ul>\n" : "</ol>\n");
} while (levels.size() > level + 1) {
if (level >= 0 && Boolean bullet = levels.remove(levels.size() - 1);
levels.size() == level + 1 && out.append(bullet ? "</ul>\n" : "</ol>\n");
levels.get(level) != isBulletListItem) { }
Boolean bullet = levels.remove(level); if (level >= 0 &&
out.append(bullet ? "</ul>\n" : "</ol>\n"); levels.size() == level + 1 &&
} levels.get(level) != isBulletListItem) {
while (levels.size() < level + 1) { Boolean bullet = levels.remove(level);
levels.add(isBulletListItem); out.append(bullet ? "</ul>\n" : "</ol>\n");
out.append(isBulletListItem ? "<ul" : "<ol") }
.append(getTextStyles(text, i, next, true, false)) while (levels.size() < level + 1) {
.append(">\n"); levels.add(isBulletListItem);
} out.append(isBulletListItem ? "<ul" : "<ol")
.append(getTextStyles(text, i, next, true, false))
.append(">\n");
}
String tagType = isBulletListItem != null ? "li" : "span"; String tagType = isBulletListItem != null ? "li" : "span";
out.append("<").append(tagType) out.append("<").append(tagType)
.append(getTextDirection(text, i, next)) .append(getTextDirection(text, i, next))
.append(getTextStyles(text, i, next, isBulletListItem == null, true)) .append(getTextStyles(text, i, next, isBulletListItem == null, true))
.append(">"); .append(">");
withinParagraph(out, text, i, next); withinParagraph(out, text, i, next);
out.append("</"); out.append("</");
out.append(tagType); out.append(tagType);
out.append(">\n"); out.append(">\n");
if (isBulletListItem == null) if (isBulletListItem == null)
out.append("<br>\n"); out.append("<br>\n");
if (next == end && levels.size() > 0) { if (next == end && levels.size() > 0) {
for (int l = levels.size() - 1; l >= 0; l--) for (int l = levels.size() - 1; l >= 0; l--)
out.append(levels.get(l) ? "</ul>\n" : "</ol>\n"); out.append(levels.get(l) ? "</ul>\n" : "</ol>\n");
levels.clear(); levels.clear();
}
} }
}
next++; next++;
} catch (Throwable ex) {
Log.e("withinBlockquoteIndividual " + start + "..." + end + "/" + text.length() +
" i=" + i + " next=" + next);
throw ex;
}
} }
} }
@ -371,155 +374,161 @@ public class HtmlEx {
int next; int next;
for (int i = start; i < end; i = next) { for (int i = start; i < end; i = next) {
next = text.nextSpanTransition(i, end, CharacterStyle.class); next = text.nextSpanTransition(i, end, CharacterStyle.class);
CharacterStyle[] style = getSpans(text, i, next, CharacterStyle.class); try {
CharacterStyle[] style = getSpans(text, i, next, CharacterStyle.class);
for (int j = 0; j < style.length; j++) {
if (style[j] instanceof StyleSpan) {
int s = ((StyleSpan) style[j]).getStyle();
if ((s & Typeface.BOLD) != 0) {
out.append("<b>");
}
if ((s & Typeface.ITALIC) != 0) {
out.append("<i>");
}
}
if (style[j] instanceof TypefaceSpan) {
String s = ((TypefaceSpan) style[j]).getFamily();
for (int j = 0; j < style.length; j++) { //if ("monospace".equals(s)) {
if (style[j] instanceof StyleSpan) { // out.append("<tt>");
int s = ((StyleSpan) style[j]).getStyle(); //}
if ((s & Typeface.BOLD) != 0) { out.append("<span style='font-family:" + s + ";'>");
out.append("<b>");
} }
if ((s & Typeface.ITALIC) != 0) { if (style[j] instanceof SuperscriptSpan) {
out.append("<i>"); out.append("<sup>");
} }
} if (style[j] instanceof SubscriptSpan) {
if (style[j] instanceof TypefaceSpan) { out.append("<sub>");
String s = ((TypefaceSpan) style[j]).getFamily();
//if ("monospace".equals(s)) {
// out.append("<tt>");
//}
out.append("<span style='font-family:" + s + ";'>");
}
if (style[j] instanceof SuperscriptSpan) {
out.append("<sup>");
}
if (style[j] instanceof SubscriptSpan) {
out.append("<sub>");
}
if (style[j] instanceof UnderlineSpan) {
out.append("<u>");
}
if (style[j] instanceof StyleHelper.MarkSpan) {
out.append("<mark>");
}
if (style[j] instanceof StrikethroughSpan) {
out.append("<span style=\"text-decoration:line-through;\">");
}
if (style[j] instanceof URLSpan) {
out.append("<a href=\"");
out.append(((URLSpan) style[j]).getURL());
out.append("\">");
}
if (style[j] instanceof ImageSpan) {
out.append("<img src=\"");
out.append(((ImageSpan) style[j]).getSource());
out.append("\"");
if (style[j] instanceof ImageSpanEx) {
ImageSpanEx img = (ImageSpanEx) style[j];
int w = img.getWidth();
if (w > 0)
out.append(" width=\"").append(w).append("\"");
int h = img.getHeight();
if (h > 0)
out.append(" height=\"").append(h).append("\"");
} }
if (style[j] instanceof UnderlineSpan) {
out.append(">"); out.append("<u>");
}
// Don't output the dummy character underlying the image. if (style[j] instanceof StyleHelper.MarkSpan) {
i = next; out.append("<mark>");
} }
if (style[j] instanceof AbsoluteSizeSpan) { if (style[j] instanceof StrikethroughSpan) {
AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]); out.append("<span style=\"text-decoration:line-through;\">");
float sizeDip = s.getSize(); }
if (!s.getDip()) { if (style[j] instanceof URLSpan) {
//Application application = ActivityThread.currentApplication(); out.append("<a href=\"");
sizeDip /= context.getResources().getDisplayMetrics().density; out.append(((URLSpan) style[j]).getURL());
out.append("\">");
}
if (style[j] instanceof ImageSpan) {
out.append("<img src=\"");
out.append(((ImageSpan) style[j]).getSource());
out.append("\"");
if (style[j] instanceof ImageSpanEx) {
ImageSpanEx img = (ImageSpanEx) style[j];
int w = img.getWidth();
if (w > 0)
out.append(" width=\"").append(w).append("\"");
int h = img.getHeight();
if (h > 0)
out.append(" height=\"").append(h).append("\"");
}
out.append(">");
// Don't output the dummy character underlying the image.
i = next;
}
if (style[j] instanceof AbsoluteSizeSpan) {
AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
float sizeDip = s.getSize();
if (!s.getDip()) {
//Application application = ActivityThread.currentApplication();
sizeDip /= context.getResources().getDisplayMetrics().density;
}
// px in CSS is the equivalance of dip in Android
out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
}
if (style[j] instanceof RelativeSizeSpan) {
float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
if (sizeEm < 1)
out.append(String.format("<span style=\"font-size:%s;\">",
sizeEm < HtmlHelper.FONT_SMALL ? "x-small" : "small"));
else if (sizeEm > 1)
out.append(String.format("<span style=\"font-size:%s;\">",
sizeEm > HtmlHelper.FONT_LARGE ? "x-large" : "large"));
}
if (style[j] instanceof ForegroundColorSpan) {
int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
//out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
out.append(String.format("<span style=\"color:%s;\">",
eu.faircode.email.HtmlHelper.encodeWebColor(color)));
}
if (style[j] instanceof BackgroundColorSpan && !(style[j] instanceof StyleHelper.MarkSpan)) {
int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
//out.append(String.format("<span style=\"background-color:#%06X;\">",
// 0xFFFFFF & color));
out.append(String.format("<span style=\"background-color:%s;\">",
eu.faircode.email.HtmlHelper.encodeWebColor(color)));
} }
// px in CSS is the equivalance of dip in Android
out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
}
if (style[j] instanceof RelativeSizeSpan) {
float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
if (sizeEm < 1)
out.append(String.format("<span style=\"font-size:%s;\">",
sizeEm < HtmlHelper.FONT_SMALL ? "x-small" : "small"));
else if (sizeEm > 1)
out.append(String.format("<span style=\"font-size:%s;\">",
sizeEm > HtmlHelper.FONT_LARGE ? "x-large" : "large"));
}
if (style[j] instanceof ForegroundColorSpan) {
int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
//out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
out.append(String.format("<span style=\"color:%s;\">",
eu.faircode.email.HtmlHelper.encodeWebColor(color)));
}
if (style[j] instanceof BackgroundColorSpan && !(style[j] instanceof StyleHelper.MarkSpan)) {
int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
//out.append(String.format("<span style=\"background-color:#%06X;\">",
// 0xFFFFFF & color));
out.append(String.format("<span style=\"background-color:%s;\">",
eu.faircode.email.HtmlHelper.encodeWebColor(color)));
} }
}
withinStyle(out, text, i, next); withinStyle(out, text, i, next);
for (int j = style.length - 1; j >= 0; j--) {
if (style[j] instanceof BackgroundColorSpan && !(style[j] instanceof StyleHelper.MarkSpan)) {
out.append("</span>");
}
if (style[j] instanceof ForegroundColorSpan) {
out.append("</span>");
}
if (style[j] instanceof RelativeSizeSpan) {
out.append("</span>");
}
if (style[j] instanceof AbsoluteSizeSpan) {
out.append("</span>");
}
if (style[j] instanceof URLSpan) {
out.append("</a>");
}
if (style[j] instanceof StrikethroughSpan) {
out.append("</span>");
}
if (style[j] instanceof StyleHelper.MarkSpan) {
out.append("</mark>");
}
if (style[j] instanceof UnderlineSpan) {
out.append("</u>");
}
if (style[j] instanceof SubscriptSpan) {
out.append("</sub>");
}
if (style[j] instanceof SuperscriptSpan) {
out.append("</sup>");
}
if (style[j] instanceof TypefaceSpan) {
//String s = ((TypefaceSpan) style[j]).getFamily();
//if (s.equals("monospace")) { for (int j = style.length - 1; j >= 0; j--) {
// out.append("</tt>"); if (style[j] instanceof BackgroundColorSpan && !(style[j] instanceof StyleHelper.MarkSpan)) {
//} out.append("</span>");
}
if (style[j] instanceof ForegroundColorSpan) {
out.append("</span>");
}
if (style[j] instanceof RelativeSizeSpan) {
out.append("</span>");
}
if (style[j] instanceof AbsoluteSizeSpan) {
out.append("</span>");
}
if (style[j] instanceof URLSpan) {
out.append("</a>");
}
if (style[j] instanceof StrikethroughSpan) {
out.append("</span>");
}
if (style[j] instanceof StyleHelper.MarkSpan) {
out.append("</mark>");
}
if (style[j] instanceof UnderlineSpan) {
out.append("</u>");
}
if (style[j] instanceof SubscriptSpan) {
out.append("</sub>");
}
if (style[j] instanceof SuperscriptSpan) {
out.append("</sup>");
}
if (style[j] instanceof TypefaceSpan) {
//String s = ((TypefaceSpan) style[j]).getFamily();
out.append("</span>"); //if (s.equals("monospace")) {
} // out.append("</tt>");
if (style[j] instanceof StyleSpan) { //}
int s = ((StyleSpan) style[j]).getStyle();
if ((s & Typeface.BOLD) != 0) { out.append("</span>");
out.append("</b>");
} }
if ((s & Typeface.ITALIC) != 0) { if (style[j] instanceof StyleSpan) {
out.append("</i>"); int s = ((StyleSpan) style[j]).getStyle();
if ((s & Typeface.BOLD) != 0) {
out.append("</b>");
}
if ((s & Typeface.ITALIC) != 0) {
out.append("</i>");
}
} }
} }
} catch (Throwable ex) {
Log.e("withinParagraph " + start + "..." + end + "/" + text.length() +
" i=" + i + " next=" + next);
throw ex;
} }
} }
} }
@ -528,34 +537,39 @@ public class HtmlEx {
private /* static */ void withinStyle(StringBuilder out, CharSequence text, private /* static */ void withinStyle(StringBuilder out, CharSequence text,
int start, int end) { int start, int end) {
for (int i = start; i < end; i++) { for (int i = start; i < end; i++) {
char c = text.charAt(i); try {
char c = text.charAt(i);
if (c == '<') {
out.append("&lt;"); if (c == '<') {
} else if (c == '>') { out.append("&lt;");
out.append("&gt;"); } else if (c == '>') {
} else if (c == '&') { out.append("&gt;");
out.append("&amp;"); } else if (c == '&') {
} else if (c >= 0xD800 && c <= 0xDFFF) { out.append("&amp;");
if (c < 0xDC00 && i + 1 < end) { } else if (c >= 0xD800 && c <= 0xDFFF) {
char d = text.charAt(i + 1); if (c < 0xDC00 && i + 1 < end) {
if (d >= 0xDC00 && d <= 0xDFFF) { char d = text.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF) {
i++;
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
out.append("&#").append(codepoint).append(";");
}
}
} else if (c > 0x7E || c < ' ') {
out.append("&#").append((int) c).append(";");
} else if (c == ' ') {
while (i + 1 < end && text.charAt(i + 1) == ' ') {
out.append("&nbsp;");
i++; i++;
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
out.append("&#").append(codepoint).append(";");
} }
}
} else if (c > 0x7E || c < ' ') {
out.append("&#").append((int) c).append(";");
} else if (c == ' ') {
while (i + 1 < end && text.charAt(i + 1) == ' ') {
out.append("&nbsp;");
i++;
}
out.append(' '); out.append(' ');
} else { } else {
out.append(c); out.append(c);
}
} catch (Throwable ex) {
Log.e("withinStyle " + start + "..." + end + "/" + text.length() + " i=" + i);
throw ex;
} }
} }
} }

Loading…
Cancel
Save