The headless browser for AI agents and web scraping
fix(render): exclude hidden rows from table layout
SGavrl2b61abfd094d
diff --git a/crates/obscura-render/src/dom.rs b/crates/obscura-render/src/dom.rsindex 8dbb68cc..187ca719 100644--- a/crates/obscura-render/src/dom.rs+++ b/crates/obscura-render/src/dom.rs@@ -11317,9 +11317,13 @@ where/// Collect rows together with the exclusive end of their originating row/// group. HTML rowspans never cross a thead/tbody/tfoot boundary; `rowspan=0`/// means the remainder of that group, not the remainder of the whole table.-fn collect_table_rows(tree: &DomTree, id: NodeId, rows: &mut Vec<(NodeId, usize)>) {+fn collect_table_rows(tree: &DomTree, id: NodeId, styles: &HashMap<NodeId, crate::LayoutStyle>,+ rows: &mut Vec<(NodeId, usize)>) {let mut direct_start: Option<usize> = None;for cid in tree.children(id) {+ if styles.get(&cid).is_some_and(|style| style.display == crate::Display::None) {+ continue;+ }let local = tree.borrow_node(cid).and_then(|n| n.as_element().map(|e| e.local.to_string()));@@ -11337,7 +11341,8 @@ fn collect_table_rows(tree: &DomTree, id: NodeId, rows: &mut Vec<(NodeId, usize)}let start = rows.len();for row in tree.children(cid) {- if tree.borrow_node(row).is_some_and(|node| {+ if !styles.get(&row).is_some_and(|style| style.display == crate::Display::None)+ && tree.borrow_node(row).is_some_and(|node| {node.as_element().is_some_and(|element| element.local.as_ref() == "tr")}) {@@ -11713,7 +11718,7 @@ fn build_table(};let mut rows: Vec<(NodeId, usize)> = Vec::new();if native_html_table {- collect_table_rows(tree, id, &mut rows);+ collect_table_rows(tree, id, styles, &mut rows);if rows.is_empty() {return None;}diff --git a/crates/obscura-render/tests/layout_test.rs b/crates/obscura-render/tests/layout_test.rsindex d013b833..91e0ae3b 100644--- a/crates/obscura-render/tests/layout_test.rs+++ b/crates/obscura-render/tests/layout_test.rs@@ -326,6 +326,51 @@ fn table_width_uses_local_space_and_keeps_width_hints_shrinkable() {);}+#[cfg(feature = "paint")]+#[test]+fn hidden_table_rows_do_not_generate_cells_tracks_or_rowspan_slots() {+ let tree = parse_html(r#"<style>+ html,body{margin:0}table{border-spacing:0}td{width:30px;height:20px;padding:0}+ .gone{display:none}.wide{width:500px;height:90px}+ </style><table id="table"><tbody>+ <tr><td id="span" rowspan="2"></td><td></td></tr>+ <tr class="gone" id="gone"><td class="wide" id="gone-cell"></td></tr>+ <tr><td id="next"></td></tr><tr><td id="last"></td></tr>+ </tbody></table>"#);+ let layout = layout_dom(&tree, (600.0, 400.0));+ let rect = |id| layout.rects[&tree.get_element_by_id(id).unwrap()];+ for id in ["gone", "gone-cell"] {+ assert!(!layout.rects.contains_key(&tree.get_element_by_id(id).unwrap()),+ "hidden {id} generated a box");+ }+ assert_eq!((rect("table").width, rect("table").height), (60.0, 60.0));+ assert_eq!(rect("span").height, 40.0);+ assert_eq!((rect("next").x, rect("next").y), (30.0, 20.0));+ assert_eq!((rect("last").x, rect("last").y), (0.0, 40.0));+}++#[cfg(feature = "paint")]+#[test]+fn hidden_table_sections_do_not_contribute_rows_or_cell_widths() {+ for section in ["thead", "tbody", "tfoot"] {+ let tree = parse_html(&format!(r#"<style>+ html,body{{margin:0}}table{{border-spacing:0}}td{{width:30px;height:20px;padding:0}}+ </style><table id="table"><{section} id="gone" style="display:none">+ <tr id="gone-row"><td id="gone-cell" style="width:500px;height:90px;visibility:visible"></td></tr>+ </{section}><tbody><tr style="visibility:hidden"><td id="invisible"></td></tr>+ <tr><td id="visible"></td></tr></tbody></table>"#));+ let layout = layout_dom(&tree, (600.0, 400.0));+ for id in ["gone", "gone-row", "gone-cell"] {+ assert!(!layout.rects.contains_key(&tree.get_element_by_id(id).unwrap()),+ "display:none {section} generated a box for {id}");+ }+ let rect = |id| layout.rects[&tree.get_element_by_id(id).unwrap()];+ assert_eq!((rect("table").width, rect("table").height), (30.0, 40.0));+ assert_eq!(rect("invisible").height, 20.0, "visibility:hidden must retain its track");+ assert_eq!(rect("visible").y, 20.0);+ }+}+#[test]fn table_rowspans_are_clipped_to_the_originating_row_group() {let tree = parse_html(diff --git a/render-repros/checks.json b/render-repros/checks.jsonindex f9d0f93e..b722504d 100644--- a/render-repros/checks.json+++ b/render-repros/checks.json@@ -1,4 +1,10 @@{+ "hidden-table-rows": [+ {"name": "rowspan counts visible rows only", "color": "#1971c2", "x": 20, "y": 20, "width": 30, "height": 40},+ {"name": "next visible row occupies the second column", "color": "#e8590c", "x": 50, "y": 40, "width": 30, "height": 20},+ {"name": "last row starts after two visible rows", "color": "#7048e8", "x": 20, "y": 60, "width": 30, "height": 20},+ {"name": "hidden groups do not reserve tracks or width", "color": "#0b7285", "x": 150, "y": 20, "width": 30, "height": 20}+ ],"inline-block-block-children": [{"name": "first block keeps its authored width", "color": "#d02020", "x": 12, "y": 12, "width": 80, "height": 20},{"name": "second block stacks without formatting whitespace", "color": "#2050d0", "x": 12, "y": 32, "width": 120, "height": 30},diff --git a/render-repros/hidden-table-rows.html b/render-repros/hidden-table-rows.htmlnew file mode 100644index 00000000..1a180048--- /dev/null+++ b/render-repros/hidden-table-rows.html@@ -0,0 +1,29 @@+<!doctype html>+<meta charset="utf-8">+<title>Hidden table rows and sections</title>+<style>+ html, body { margin: 0; background: white; }+ table { position: absolute; border-spacing: 0; }+ td { width: 30px; height: 20px; padding: 0; }+ .gone { display: none; }+ .wide { width: 500px; height: 90px; background: #d02020; }+ #rows { left: 20px; top: 20px; }+ #span { background: #1971c2; }+ #first { background: #087f5b; }+ #next { background: #e8590c; }+ #last { background: #7048e8; }+ #groups { left: 150px; top: 20px; }+ #group-cell { background: #0b7285; }+</style>+<table id="rows"><tbody>+ <tr><td id="span" rowspan="2"></td><td id="first"></td></tr>+ <tr id="gone-row" class="gone"><td id="gone-cell" class="wide"></td></tr>+ <tr><td id="next"></td></tr>+ <tr><td id="last"></td></tr>+</tbody></table>+<table id="groups">+ <thead id="gone-head" class="gone"><tr><td class="wide"></td></tr></thead>+ <tbody id="gone-body" class="gone"><tr><td class="wide"></td></tr></tbody>+ <tfoot id="gone-foot" class="gone"><tr><td class="wide"></td></tr></tfoot>+ <tbody><tr><td id="group-cell"></td></tr></tbody>+</table>