Fixed not appearing tables

pull/198/head
M66B 4 years ago
parent 7c54351b9f
commit 5d07897ac6

@ -106,6 +106,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -548,189 +549,198 @@ public class HtmlHelper {
if (!TextUtils.isEmpty(style)) { if (!TextUtils.isEmpty(style)) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Map<String, String> kv = new LinkedHashMap<>();
String[] params = style.split(";"); String[] params = style.split(";");
for (String param : params) { for (String param : params) {
int colon = param.indexOf(':'); int colon = param.indexOf(':');
if (colon > 0) { if (colon <= 0)
String key = param.substring(0, colon).trim().toLowerCase(Locale.ROOT); continue;
String value = param.substring(colon + 1).toLowerCase(Locale.ROOT) String key = param.substring(0, colon).trim().toLowerCase(Locale.ROOT);
.replace("!important", "") String value = param.substring(colon + 1).toLowerCase(Locale.ROOT)
.trim() .replace("!important", "")
.replaceAll("\\s+", " "); .trim()
switch (key) { .replaceAll("\\s+", " ");
case "color": kv.put(key, value);
// https://developer.mozilla.org/en-US/docs/Web/CSS/color }
if (!text_color)
continue;
Integer color = parseColor(value); for (String key : kv.keySet()) {
String value = kv.get(key);
switch (key) {
case "color":
// https://developer.mozilla.org/en-US/docs/Web/CSS/color
if (!text_color)
continue;
if (color != null && !view && Helper.isDarkTheme(context)) { Integer color = parseColor(value);
float lum = (float) ColorUtils.calculateLuminance(color);
if (lum < 0.1f)
color = null;
}
if (color == null) if (color != null && !view && Helper.isDarkTheme(context)) {
element.removeAttr("color"); float lum = (float) ColorUtils.calculateLuminance(color);
else { if (lum < 0.1f)
if (view) color = null;
color = adjustColor(dark, textColorPrimary, color); }
// fromHtml does not support transparency
String c = String.format("#%06x", color);
sb.append("color:").append(c).append(";");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
element.attr("color", c);
}
break; if (color == null)
element.removeAttr("color");
else {
if (view)
color = adjustColor(dark, textColorPrimary, color);
// fromHtml does not support transparency
String c = String.format("#%06x", color);
sb.append("color:").append(c).append(";");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
element.attr("color", c);
}
case "font-size": break;
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-size
if (!text_size)
continue;
float current; case "font-size":
if (tag.length() == 2 && // https://developer.mozilla.org/en-US/docs/Web/CSS/font-size
tag.charAt(0) == 'h' && if (!text_size)
Character.isDigit(tag.charAt(1))) continue;
current = HEADING_SIZES[tag.charAt(1) - '1'];
else
current = 1.0f;
Element parent = element.parent(); float current;
while (parent != null) { if (tag.length() == 2 &&
String xFontSize = parent.attr("x-font-size"); tag.charAt(0) == 'h' &&
if (!TextUtils.isEmpty(xFontSize)) { Character.isDigit(tag.charAt(1)))
current = Float.parseFloat(xFontSize); current = HEADING_SIZES[tag.charAt(1) - '1'];
break; else
} current = 1.0f;
parent = parent.parent();
Element parent = element.parent();
while (parent != null) {
String xFontSize = parent.attr("x-font-size");
if (!TextUtils.isEmpty(xFontSize)) {
current = Float.parseFloat(xFontSize);
break;
} }
parent = parent.parent();
}
Float fsize = getFontSize(value, current); Float fsize = getFontSize(value, current);
if (fsize != null && fsize != 0) { if (fsize != null && fsize != 0) {
if (!view) { if (!view) {
if (fsize < 1) if (fsize < 1)
fsize = FONT_SMALL; fsize = FONT_SMALL;
else if (fsize > 1) else if (fsize > 1)
fsize = FONT_LARGE; fsize = FONT_LARGE;
}
element.attr("x-font-size", Float.toString(fsize));
element.attr("x-font-size-rel", Float.toString(fsize / current));
} }
break; element.attr("x-font-size", Float.toString(fsize));
element.attr("x-font-size-rel", Float.toString(fsize / current));
}
break;
case "font-weight": case "font-weight":
if (element.parent() != null) { if (element.parent() != null) {
Integer fweight = getFontWeight(value); Integer fweight = getFontWeight(value);
if (fweight != null && fweight >= 600) { if (fweight != null && fweight >= 600) {
Element strong = new Element("strong"); Element strong = new Element("strong");
for (Node child : new ArrayList<>(element.childNodes())) { for (Node child : new ArrayList<>(element.childNodes())) {
child.remove(); child.remove();
strong.appendChild(child); strong.appendChild(child);
}
element.appendChild(strong);
} }
element.appendChild(strong);
} }
break; }
break;
case "font-family": case "font-family":
if (!text_font) if (!text_font)
continue; continue;
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-family // https://developer.mozilla.org/en-US/docs/Web/CSS/font-family
sb.append(key).append(":").append(value).append(";"); sb.append(key).append(":").append(value).append(";");
break; break;
case "text-decoration": case "text-decoration":
// https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration // https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration
if (value.contains("line-through")) if (value.contains("line-through"))
sb.append("text-decoration:line-through;"); sb.append("text-decoration:line-through;");
break; break;
case "display": case "display":
// https://developer.mozilla.org/en-US/docs/Web/CSS/display // https://developer.mozilla.org/en-US/docs/Web/CSS/display
if (element.parent() != null && if (element.parent() != null &&
!display_hidden && "none".equals(value)) { !display_hidden && "none".equals(value)) {
Log.i("Removing display none " + element.tagName()); Log.i("Removing display none " + element.tagName());
element.remove(); element.remove();
} }
if ("block".equals(value) || "inline-block".equals(value)) if ("block".equals(value) || "inline-block".equals(value))
element.attr("x-block", "true"); element.attr("x-block", "true");
if ("inline".equals(value) || "inline-block".equals(value)) { if ("inline".equals(value) || "inline-block".equals(value)) {
if (element.nextSibling() != null) if (element.nextSibling() != null)
element.attr("x-inline", "true"); element.attr("x-inline", "true");
} }
break; break;
case "height": case "height":
case "width": case "width":
//case "font-size": //case "font-size":
//case "line-height": //case "line-height":
if (element.parent() != null && !display_hidden) { if (element.parent() != null && !display_hidden) {
Float s = getFontSize(value, 1.0f); Float s = getFontSize(value, 1.0f);
if (s != null && s == 0) { if (s != null && s == 0) {
if (!"table".equals(element.tagName()) ||
!"fixed".endsWith(kv.get("table-layout"))) {
Log.i("Removing no height/width " + element.tagName()); Log.i("Removing no height/width " + element.tagName());
element.remove(); element.remove();
} }
} }
break; }
break;
case "margin":
case "padding":
case "margin-top":
case "margin-bottom":
case "padding-top":
case "padding-bottom":
// https://developer.mozilla.org/en-US/docs/Web/CSS/margin
// https://developer.mozilla.org/en-US/docs/Web/CSS/padding
if (element.isBlock()) {
Float[] p = new Float[4];
String[] v = value.split(" ");
for (int i = 0; i < v.length && i < p.length; i++)
p[i] = getFontSize(v[i], 1.0f);
if (v.length == 1) {
p[1] = p[0];
p[2] = p[0];
p[3] = p[0];
} else if (v.length == 2) {
p[2] = p[0];
p[3] = p[1];
}
if (key.endsWith("top")) case "margin":
p[2] = null; case "padding":
else if (key.endsWith("bottom")) case "margin-top":
p[0] = null; case "margin-bottom":
case "padding-top":
if (p[0] != null) case "padding-bottom":
if (p[0] == 0) // https://developer.mozilla.org/en-US/docs/Web/CSS/margin
element.attr("x-line-before", "false"); // https://developer.mozilla.org/en-US/docs/Web/CSS/padding
else if (p[0] > 0.5) if (element.isBlock()) {
element.attr("x-line-before", "true"); Float[] p = new Float[4];
if (p[2] != null)
if (p[2] == 0) String[] v = value.split(" ");
element.attr("x-line-after", "false"); for (int i = 0; i < v.length && i < p.length; i++)
else if (p[2] > 0.5) p[i] = getFontSize(v[i], 1.0f);
element.attr("x-line-after", "true");
if (v.length == 1) {
p[1] = p[0];
p[2] = p[0];
p[3] = p[0];
} else if (v.length == 2) {
p[2] = p[0];
p[3] = p[1];
} }
break;
case "text-align": if (key.endsWith("top"))
// https://developer.mozilla.org/en-US/docs/Web/CSS/text-align p[2] = null;
if (text_align) { else if (key.endsWith("bottom"))
element.attr("x-align", value); p[0] = null;
sb.append("display:block;");
sb.append(key).append(':').append(value).append(';'); if (p[0] != null)
} if (p[0] == 0)
break; element.attr("x-line-before", "false");
} else if (p[0] > 0.5)
element.attr("x-line-before", "true");
if (p[2] != null)
if (p[2] == 0)
element.attr("x-line-after", "false");
else if (p[2] > 0.5)
element.attr("x-line-after", "true");
}
break;
case "text-align":
// https://developer.mozilla.org/en-US/docs/Web/CSS/text-align
if (text_align) {
element.attr("x-align", value);
sb.append("display:block;");
sb.append(key).append(':').append(value).append(';');
}
break;
} }
} }

Loading…
Cancel
Save