Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(layout): preserve table cell sizing and alignment
ldm0f4fa0c70dd97
diff --git a/moli-layout/src/table.rs b/moli-layout/src/table.rsindex b92569885..8746b4a7d 100644--- a/moli-layout/src/table.rs+++ b/moli-layout/src/table.rs@@ -20,7 +20,10 @@ use taffy::{TraverseTree, WritingMode, compute_grid_layout, style_helpers,};-use crate::{LayoutBoxId, LayoutBoxKind, LayoutWorld, style::resolve_stylo_calc_value};+use crate::{+ LayoutBoxId, LayoutBoxKind, LayoutWorld,+ style::{LayoutInlineAlignment, resolve_stylo_calc_value},+};mod collapsed_borders;mod columns;@@ -685,8 +688,19 @@ fn collect_rows<N>(let data = table_data(world, cell);let column_span = usize::from(data.column_span.max(1));let row_span = usize::from(data.row_span.max(1));- let mut cell_style = world.boxes[cell.index()].style.taffy.clone();+ let authored_style = &world.boxes[cell.index()].style;+ let mut cell_style = authored_style.taffy.clone();cell_style.margin = Rect::ZERO.map(style_helpers::length);+ if cell_style.align_content.is_none() {+ cell_style.align_content = match authored_style.vertical_align().kind {+ LayoutInlineAlignment::Middle => Some(taffy::AlignContent::CENTER),+ LayoutInlineAlignment::Bottom => Some(taffy::AlignContent::END),+ LayoutInlineAlignment::Top => Some(taffy::AlignContent::START),+ LayoutInlineAlignment::Baseline+ | LayoutInlineAlignment::TextTop+ | LayoutInlineAlignment::TextBottom => None,+ };+ }cells.push(TableCell {id: cell,style: cell_style,diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rsindex 5cb17de3b..6ad112f16 100644--- a/moli-layout/src/taffy_tree.rs+++ b/moli-layout/src/taffy_tree.rs@@ -1606,6 +1606,16 @@ whereif self.should_hide(node_id, inputs) {return compute_hidden_layout(self, node_id);}+ // A block parent's intrinsic height calculation consumes the child's+ // collapsible-margin sets as well as its numeric size. Taffy's compact+ // measurement cache intentionally retains only the latter, so a cache+ // hit reconstructed as `LayoutOutput` would silently turn escaped+ // descendant margins into zero. Keep this legacy LayoutOutput path+ // uncached until block sizing uses the dedicated size protocol end to+ // end; final layout remains cached below.+ if inputs.run_mode == RunMode::ComputeSize {+ return self.compute_child_layout_uncached(node_id, inputs, block_context);+ }compute_cached_layout(self, node_id, inputs, |world, node_id, inputs| {world.compute_child_layout_uncached(node_id, inputs, block_context)})diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rsindex d08f24e1b..6bd3829b8 100644--- a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs+++ b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs@@ -2851,6 +2851,147 @@ return [.expect("table-cell presentational-style fixture should run");}+#[tokio::test(flavor = "current_thread")]+async fn screenshot_applies_legacy_table_width_spacing_and_colors() {+ run_page_vm_async_test(async move {+ let loader =+ crate::network::ResourceRequestClient::new(&FetchConfig::default()).expect("loader");+ let mut page_vm = test_page_vm_with_loader_and_document_url(+ &loader,+ Vec::new(),+ Url::parse("https://example.com/table-presentation-style.html")?,+ );+ page_vm.vm_mut().eval(+ r##"+document.head.innerHTML = `<style>+html,body{margin:0;padding:0;background:white}+td{padding:0}+.block{width:10px;height:10px}+</style>`;+document.body.innerHTML = `+<table id=legacy width="85%" bgcolor="#f6f6ef" cellspacing="0" cellpadding="0">+ <tr><td id=header bgcolor="#ff6600" valign="top" align="right"><div class=block></div></td></tr>+ <tr><td id=body-cell><div class=block></div></td></tr>+</table>`;+'installed'+"##,+ )?;+ page_vm.vm_mut().sync_live_document_style_sources();++ let viewport = moli_layout::PaintViewport::new(200, 100, 1.0);+ let snapshot = page_vm+ .vm_mut()+ .screenshot_layout_snapshot(viewport)?+ .expect("legacy table fixture must retain a layout root");+ let read_table = r#"(() => {+const table = document.getElementById('legacy');+const tableStyle = getComputedStyle(table);+const cellStyle = getComputedStyle(document.getElementById('header'));+return [+ tableStyle.borderSpacing,+ tableStyle.backgroundColor,+ cellStyle.backgroundColor,+ cellStyle.textAlign,+ table.offsetWidth,+].join('|');+})()"#;+ assert_eq!(+ page_vm.vm_mut().eval(read_table)?,+ "0px|rgb(246, 246, 239)|rgb(255, 102, 0)|right|170",+ "legacy table attributes must participate in the presentation-hint cascade",+ );++ let image = moli_paint::raster_snapshot(&snapshot)?;+ let pixel = |x: u32, y: u32| {+ let index = ((y * image.width + x) * 4) as usize;+ &image.rgba[index..index + 4]+ };+ assert_eq!(pixel(160, 5), [255, 102, 0, 255]);+ assert_eq!(pixel(160, 15), [246, 246, 239, 255]);+ assert_eq!(pixel(180, 15), [255, 255, 255, 255]);++ page_vm.vm_mut().eval(+ r#"+const table = document.getElementById('legacy');+table.setAttribute('width', '50%');+table.setAttribute('cellspacing', '5');+table.setAttribute('bgcolor', '#010203');+const header = document.getElementById('header');+header.setAttribute('bgcolor', '#040506');+header.setAttribute('valign', 'bottom');+header.setAttribute('align', 'left');+"#,+ )?;+ page_vm+ .vm_mut()+ .screenshot_layout_snapshot(viewport)?+ .expect("mutated legacy table fixture must retain a layout root");+ assert_eq!(+ page_vm.vm_mut().eval(read_table)?,+ "5px|rgb(1, 2, 3)|rgb(4, 5, 6)|left|100",+ "legacy table attribute mutations must recascade their presentation hints",+ );+ Ok::<_, anyhow::Error>(())+ })+ .await+ .expect("legacy table presentational-style fixture should run");+}++#[tokio::test(flavor = "current_thread")]+async fn screenshot_preserves_nested_table_cell_height_and_vertical_alignment() {+ run_page_vm_async_test(async move {+ let loader =+ crate::network::ResourceRequestClient::new(&FetchConfig::default()).expect("loader");+ let mut page_vm = test_page_vm_with_loader_and_document_url(+ &loader,+ Vec::new(),+ Url::parse("https://example.com/nested-table-cell-layout.html")?,+ );+ page_vm.vm_mut().eval(+ r##"+document.head.innerHTML = `<style>+html,body{margin:0}+table{border-spacing:0}+td{padding:0}+#outer{width:300px;font:13.3333px Verdana,Arial,sans-serif}+.arrow{width:10px;height:10px;margin:3px 2px 6px;background:#777}+.box{width:10px;height:10px;background:red}+</style>`;+document.body.innerHTML = `+<table id=outer><tr><td>+ <table id=inner><tr id=row>+ <td valign=top><span>1.</span></td>+ <td valign=top><center><a><div id=arrow class=arrow></div></a></center></td>+ <td valign=middle><span id=middle-content>Title</span></td>+ <td valign=bottom><div id=bottom-content class=box></div></td>+ </tr></table>+</td></tr></table>`;+'installed'+"##,+ )?;+ page_vm.vm_mut().sync_live_document_style_sources();++ page_vm+ .vm_mut()+ .screenshot_layout_snapshot(moli_layout::PaintViewport::new(320, 100, 1.0))?+ .expect("nested table fixture must retain a layout root");+ assert_eq!(+ page_vm.vm_mut().eval(+ r#"(() => {+const row = document.getElementById('row').getBoundingClientRect();+const offset = id => document.getElementById(id).getBoundingClientRect().y - row.y;+return [row.height, offset('arrow'), offset('middle-content'), offset('bottom-content')].join('|');+})()"#,+ )?,+ "19|3|2|9",+ "nested table measurement must retain collapsed child margins and align cell content",+ );+ Ok::<_, anyhow::Error>(())+ })+ .await+ .expect("nested table-cell layout fixture should run");+}+#[tokio::test(flavor = "current_thread")]async fn screenshot_paints_fresh_inline_svg_resources_with_computed_current_color() {run_page_vm_async_test(async move {diff --git a/moli-renderer-v8/src/style_engine/ua/html.css b/moli-renderer-v8/src/style_engine/ua/html.cssindex 4cd234d58..32831c632 100644--- a/moli-renderer-v8/src/style_engine/ua/html.css+++ b/moli-renderer-v8/src/style_engine/ua/html.css@@ -89,14 +89,26 @@ table {thead {display: table-header-group;+ vertical-align: middle;+ border-color: inherit;}tbody {display: table-row-group;+ vertical-align: middle;+ border-color: inherit;}tfoot {display: table-footer-group;+ vertical-align: middle;+ border-color: inherit;+}++/* Dynamically-created XHTML tables need the section default without an+ implied tbody. */+table > tr {+ vertical-align: middle;}col {@@ -109,10 +121,13 @@ colgroup {tr {display: table-row;+ vertical-align: inherit;+ border-color: inherit;}td, th {display: table-cell;+ vertical-align: inherit;}/* Normal table cells receive their padding from the nearest table's legacy@@ -122,6 +137,11 @@ th:not(table th) {padding: 1px;}+th {+ font-weight: bold;+ text-align: -moz-center;+}+caption {display: table-caption;}