craft-html: keep zero row spans in their row group

This commit is contained in:
Rees committed 2026-11-19 09:01:42 +01:00
1 parent 56c3c833bb
commit da4c3caba5
2 files changed
+77 -8

No files matched your search

+56 -8
View File
@@ -53,6 +53,7 @@ struct El {
struct TableB {
rows: Vec<Vec<Cell>>,
row: Option<Vec<Cell>>,
group_start: usize,
/// The table or one of its cells asks for borders.
bordered: bool,
/// Explicit column widths from `<col>` and childless `<colgroup>`.
@@ -62,6 +63,26 @@ struct TableB {
}
impl TableB {
fn finish_row_group(&mut self) {
if let Some(row) = self.row.take() {
self.rows.push(row);
}
let end = self.rows.len().min(craft_wordmodel::table::MAX_ROWS);
if let Some(rows) = self.rows.get_mut(self.group_start..end) {
let mut remaining = 0u32;
for row in rows.iter_mut().rev() {
if !row.is_empty() {
remaining += 1;
}
// Resolve zero spans before expanding the table's covered cells.
for cell in row.iter_mut().filter(|cell| cell.rowspan == 0) {
cell.rowspan = remaining.clamp(1, 1000);
}
}
}
self.group_start = end;
}
/// Commit pending colgroup widths: if childless, push span columns with group width.
/// When child `<col>` elements are present, they have already received the group width
/// during `<col>` handling; columns from earlier groups are never backfilled.
@@ -219,6 +240,10 @@ pub(super) fn block_kind(n: &str) -> Option<ElKind> {
}
/// Closes an open `p` (HTML's "close a p element" on block starts).
fn is_row_group(name: &str) -> bool {
matches!(name, "thead" | "tbody" | "tfoot")
}
fn closes_p(n: &str) -> bool {
matches!(
n,
@@ -921,9 +946,7 @@ impl<'r> Builder<'r> {
ElKind::Table => {
if let Some(mut t) = self.tables.pop() {
t.commit_pending_colgroup();
if let Some(r) = t.row.take() {
t.rows.push(r);
}
t.finish_row_group();
let borderless = !t.bordered;
// Keep at most MAX_ROWS rows (the model drops the rest anyway): bounds the
// O(rows × rowspan) work of `insert_covered` on a table of millions of `<tr>`.
@@ -1014,6 +1037,24 @@ impl<'r> Builder<'r> {
false
}
fn finish_row_group(&mut self) {
let Some(table) = self.stack.iter().rposition(|element| element.name == "table") else { return };
let owns_rows = self.stack.get(table).is_some_and(|element| element.kind == ElKind::Table);
if let Some((index, _)) = self
.stack
.iter()
.enumerate()
.skip(table + 1)
.find(|(_, element)| is_row_group(&element.name) || matches!(element.name.as_str(), "tr" | "td" | "th"))
{
self.pop_to(index);
}
// A table flattened at the nesting limit does not own the containing table's rows.
if owns_rows && let Some(table) = self.tables.last_mut() {
table.finish_row_group();
}
}
fn close_implicitly(&mut self, name: &str, is_table_part: bool) {
if closes_p(name)
&& let Some(k) = self.find_open(|e| e.name == "p", |e| matches!(e.kind, ElKind::Cell | ElKind::Table | ElKind::Li | ElKind::Quote))
@@ -1035,10 +1076,8 @@ impl<'r> Builder<'r> {
{
self.pop_to(k);
}
if matches!(name, "tbody" | "thead" | "tfoot")
&& let Some(k) = self.find_open(|e| matches!(e.name.as_str(), "tbody" | "thead" | "tfoot"), |e| e.kind == ElKind::Table)
{
self.pop_to(k);
if is_row_group(name) || matches!(name, "caption" | "colgroup" | "col") {
self.finish_row_group();
}
if is_table_part && !self.tables.is_empty() {
if matches!(name, "td" | "th")
@@ -1260,6 +1299,7 @@ impl<'r> Builder<'r> {
ElKind::Table => self.tables.push(TableB {
rows: Vec::new(),
row: None,
group_start: 0,
bordered: declares_border(attrs, &css, true),
col_widths: Vec::new(),
pending_colgroup: None,
@@ -1321,7 +1361,7 @@ impl<'r> Builder<'r> {
let mso_vmerge = decls(&css).filter(|(k, _)| k == "mso-yfti-vmerge").last().map(|(_, v)| v.trim().to_ascii_lowercase());
let is_mso_vmerge_continue = mso_vmerge.as_deref() == Some("continue");
let is_mso_vmerge_restart = mso_vmerge.as_deref() == Some("restart");
let rowspan = attr(attrs, "rowspan").and_then(|v| v.trim().parse::<u32>().ok()).map(|v| v.clamp(1, 1000)).unwrap_or(1);
let rowspan = attr(attrs, "rowspan").and_then(|v| v.trim().parse::<u32>().ok()).map(|v| v.min(1000)).unwrap_or(1);
self.cells.push(Cell {
blocks: Vec::new(),
colspan,
@@ -1375,6 +1415,14 @@ impl<'r> Builder<'r> {
}
return;
}
if is_row_group(name) {
let open = self.find_open(|element| is_row_group(&element.name), |element| element.name == "table");
// Without an explicit group, a row-group end tag ends the implicit group.
if open.is_none_or(|index| self.stack.get(index).is_some_and(|element| element.name == name)) {
self.finish_row_group();
}
return;
}
let crosses = matches!(name, "table");
let target = if matches!(name, "td" | "th") {
self.find_open(|e| e.kind == ElKind::Cell, |e| e.kind == ElKind::Table)
+21
View File
@@ -2049,3 +2049,24 @@ fn zero_alpha_colors_with_none_channels() {
let craft_wordmodel::Block::Para(p2) = &*doc2.body[0] else { panic!("expected Para") };
assert_eq!(p2.props.shading, None);
}
#[test]
fn zero_rowspan_stops_at_the_row_group_boundary() {
for boundary in ["</tbody><tbody>", "<tbody>", "</tbody>", "<caption>Heading</caption>", "<colgroup><col></colgroup>", "<col>"] {
let html = format!("<table><tr><td rowspan=0>A<td>B<tr><td>C{boundary}<tr><td>D<td>E</table>");
let flow = parse(&html);
let table = flow
.blocks
.iter()
.find_map(|block| match block {
FBlock::Table(table) => Some(table),
_ => None,
})
.unwrap();
assert_eq!(table.rows.len(), 3, "{boundary}: expected three rows");
assert_eq!(table.rows[0][0].rowspan, 2, "{boundary}: A spans the first two rows");
assert!(table.rows[1][0].covered, "{boundary}: row two continues A");
assert!(!table.rows[2][0].covered, "{boundary}: the next group starts at column one");
assert_eq!(table.rows[2].len(), 2, "{boundary}: D and E retain their columns");
}
}