Flowing layout for tables with headers only

pull/187/head
M66B 5 years ago
parent 44f8ee9d86
commit 002c983472

@ -831,20 +831,28 @@ public class HtmlHelper {
Element table = tables.get(t); Element table = tables.get(t);
// Get rows and caption // Get rows and caption
boolean titles = false; Boolean heading = null;
Elements rows = new Elements(); Elements rows = new Elements();
Elements extras = new Elements(); Elements extras = new Elements();
for (Element child : table.children()) { for (Element child : table.children()) {
switch (child.tagName()) { switch (child.tagName()) {
case "thead": case "thead":
case "tfoot": case "tfoot":
titles = true;
case "tbody": case "tbody":
for (Element sub : child.children()) for (Element sub : child.children())
if ("tr".equals(sub.tagName())) if ("tr".equals(sub.tagName())) {
rows.add(sub); rows.add(sub);
else if (!"tbody".equals(child.tagName()) &&
(heading == null || heading))
for (Element col : sub.children()) {
heading = "th".equals(col.tagName());
if (!heading)
break;
}
} else {
Log.e("Row unexpected tag=" + sub.tagName());
extras.add(sub); extras.add(sub);
}
break; break;
case "tr": case "tr":
rows.add(child); rows.add(child);
@ -866,7 +874,8 @@ public class HtmlHelper {
switch (col.tagName()) { switch (col.tagName()) {
case "td": case "td":
if (!titles && col.childNodeSize() == 1) { if (col.childNodeSize() == 1 &&
(heading == null || !heading)) {
Node first = col.childNode(0); Node first = col.childNode(0);
if (first instanceof TextNode) { if (first instanceof TextNode) {
if (((TextNode) first).text().length() != 1) if (((TextNode) first).text().length() != 1)
@ -888,7 +897,7 @@ public class HtmlHelper {
col.appendChild(strong); col.appendChild(strong);
break; break;
default: default:
Log.e("Column expected tag=" + col.tagName()); Log.e("Column unexpected tag=" + col.tagName());
if (tdebug) { if (tdebug) {
col.prependText("COLUMN=" + col.tagName() + "["); col.prependText("COLUMN=" + col.tagName() + "[");
col.appendText("]"); col.appendText("]");
@ -932,60 +941,79 @@ public class HtmlHelper {
node.remove(); node.remove();
if (node instanceof Element) if (node instanceof Element)
node.removeAttr("x-block"); node.removeAttr("x-block");
row.child(col + 1).prependText(" ").prependChild(node); row.child(col + 1).prependText("\u00a0").prependChild(node);
} }
// Rebuild table // Rebuild table
table.tagName("div"); table.tagName("div");
table.children().remove(); // leaves nodes table.children().remove(); // leaves nodes
// Process extras: caption, etc
for (Element extra : extras) { for (Element extra : extras) {
if (tdebug) { if (tdebug) {
extra.prependText("EXTRA=" + extra.tagName() + "["); extra.prependText("EXTRA=" + extra.tagName() + "[");
extra.appendText("]"); extra.appendText("]");
} }
table.appendChild(extra.tagName("div").attr("x-block", "true")); table.appendChild(extra.tagName("div")
.attr("x-block", "true"));
} }
for (int c = 0; c < maxcols; c++) { if (heading == null || !heading) {
Element col = document.createElement("div")
.attr("x-block", "true");
if (tdebug)
col.appendText("merge=" + !nomerge.get(c));
int r = 0;
for (Element row : rows) { for (Element row : rows) {
r++; Element line = document.createElement("div")
Elements rowcol = row.children(); .attr("x-block", "true");
if (c < rowcol.size()) { for (Element col : row.children())
Element cell = rowcol.get(c).clone().tagName("div"); line.appendChild(col.tagName("div"));
if (tdebug) {
StringBuilder sb = new StringBuilder(); table.appendChild(line);
sb.append(rowcol.get(c).tagName() + "=" + rowcol.get(c).className());
for (Node node : rowcol.get(c).childNodes()) { if (text_separators && view)
sb.append(':'); line.appendElement("hr")
if (node instanceof Element) .attr("x-block", "true")
sb.append(node.nodeName()); .attr("x-dashed", "true");
else if (node instanceof TextNode) }
sb.append("~"); } else {
else // Process columns
sb.append(node.getClass().getSimpleName()); for (int c = 0; c < maxcols; c++) {
Element col = document.createElement("div")
.attr("x-block", "true");
if (tdebug)
col.appendText("merge=" + !nomerge.get(c));
int r = 0;
for (Element row : rows) {
r++;
Elements rowcol = row.children();
if (c < rowcol.size()) {
Element cell = rowcol.get(c).clone().tagName("div");
if (tdebug) {
StringBuilder sb = new StringBuilder();
sb.append(rowcol.get(c).tagName() + "=" + rowcol.get(c).className());
for (Node node : rowcol.get(c).childNodes()) {
sb.append(':');
if (node instanceof Element)
sb.append(node.nodeName());
else if (node instanceof TextNode)
sb.append("~");
else
sb.append(node.getClass().getSimpleName());
}
cell.prependText("CELL=" + (t + 1) +
":" + r + "/" + rows.size() +
":" + (c + 1) + "/" + maxcols + "/" +
sb.toString() + "[");
cell.appendText("]");
} }
cell.prependText("CELL=" + (t + 1) + col.appendChild(cell);
":" + r + "/" + rows.size() +
":" + (c + 1) + "/" + maxcols + "/" +
sb.toString() + "[");
cell.appendText("]");
} }
col.appendChild(cell);
} }
}
table.appendChild(col); table.appendChild(col);
if (text_separators && view) if (text_separators && view)
col.appendElement("hr") col.appendElement("hr")
.attr("x-block", "true") .attr("x-block", "true")
.attr("x-dashed", "true"); .attr("x-dashed", "true");
}
} }
} }

Loading…
Cancel
Save