The headless browser for AI agents and web scraping
fix(render): ignore grid placements with span 0 instead of aborting
Lars Karlslund025088cdf2bf
diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rsindex d1371343..66f68a7e 100644--- a/crates/obscura-js/src/ops.rs+++ b/crates/obscura-js/src/ops.rs@@ -8188,9 +8188,13 @@ fn ensure_prepared_render_with_base_url(.then(|| state.prepared_render.take()).flatten();let mutations = std::mem::take(&mut state.pending_style_mutations);- let prepared = {+ // Anti-panic boundary: style and layout run inside a JS op (a geometry+ // read), and deno_core's op callbacks are `extern "C"`, so a panic in+ // the layout engine would abort the process. A page whose layout+ // panics loses its layout for this read instead.+ let prepared = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {let dom = state.dom.as_ref()?;- match previous {+ Some(match previous {Some(previous) => obscura_render::prepare_dom_with_retained_styles_with_animation_state(dom,viewport,@@ -8238,6 +8242,13 @@ fn ensure_prepared_render_with_base_url(&mut state.animation_timeline,)?,},+ })+ }));+ let prepared = match prepared {+ Ok(prepared) => prepared?,+ Err(_) => {+ tracing::error!("layout panicked; the page has no layout for this read");+ return None;}};if animation_sample.mode == obscura_render::AnimationSampleMode::DocumentTime {diff --git a/crates/obscura-render/src/style.rs b/crates/obscura-render/src/style.rsindex 9524a82d..c12d40ec 100644--- a/crates/obscura-render/src/style.rs+++ b/crates/obscura-render/src/style.rs@@ -1927,6 +1927,12 @@ fn apply_value(style: &mut LayoutStyle, name: &str, value: &str) {"grid" => parse_grid_shorthand(style, value),"grid-auto-flow" => style.grid_auto_flow = parse_grid_auto_flow(value),"grid-area" => set_grid_area(style, value),+ "grid-column" | "grid-row" | "grid-column-start" | "grid-column-end"+ | "grid-row-start" | "grid-row-end"+ if !grid_placement_declaration_is_valid(+ value,+ matches!(name, "grid-column" | "grid-row"),+ ) => {}"grid-column" => set_grid_placement(style, value, true),"grid-row" => set_grid_placement(style, value, false),"grid-column-start" => set_grid_placement_side(style, value, true, true),@@ -4884,6 +4890,31 @@ fn consume_grid_integer_math(parser: &mut cssparser::Parser<'_, '_>) -> bool {valid}+/// Whether a `grid-column`/`grid-row` shorthand (`shorthand`) or one of their+/// longhands is a valid declaration. CSS Grid 1 section 8.3 makes zero and+/// negative spans and a line number of zero invalid (`span 0`, `0`), so the+/// declaration is ignored like any other invalid one, and `var()` values that+/// substitute to one (Shopify's `span var(--menu-content-columns-desktop) / -1`+/// with the variable at 0) get the same treatment here as an unresolvable+/// `var()`. Letting `span 0` through gave taffy a zero-track grid area, which+/// panics in track sizing.+fn grid_placement_declaration_is_valid(value: &str, shorthand: bool) -> bool {+ let value = value.trim();+ if matches!(+ value.to_ascii_lowercase().as_str(),+ "initial" | "inherit" | "unset" | "revert" | "revert-layer"+ ) {+ return true;+ }+ let parts = split_top_level(value, '/');+ if parts.len() > if shorthand { 2 } else { 1 } {+ return false;+ }+ parts+ .iter()+ .all(|part| !part.trim().is_empty() && parse_grid_line_kind(part.trim()).is_some())+}+/// Store a `grid-column`/`grid-row` value. Numeric/`span` forms resolve to a/// `taffy::Line` now; a value that names a grid line (`content-start //// content-end`, or the `grid-column: content` area shorthand) is kept raw anddiff --git a/crates/obscura-render/tests/layout_test.rs b/crates/obscura-render/tests/layout_test.rsindex 830f10ea..4ad80996 100644--- a/crates/obscura-render/tests/layout_test.rs+++ b/crates/obscura-render/tests/layout_test.rs@@ -2041,6 +2041,50 @@ fn absolute_inline_svg_keeps_percentage_width_and_view_box_ratio() {assert!((art.height - 160.0).abs() < 0.01, "viewBox ratio: {art:?}");}+/// `span 0` is an invalid grid line (CSS Grid 1 section 8.3), so the+/// declaration is ignored: the earlier placement wins, or the item stays+/// auto-placed. Shopify themes reach it through+/// `grid-column: span var(--menu-content-columns-desktop) / -1` with the+/// variable at 0; that once gave taffy a zero-track grid area and aborted the+/// whole page ("slice index starts at 13 but ends at 12").+#[test]+fn grid_placement_with_zero_span_is_ignored() {+ let tree = parse_html(+ r#"<html><head><style>+ html,body{margin:0}+ .grid{--n:0;display:grid;grid-template-columns:repeat(6,1fr);width:600px}+ #literal{grid-column:span 0 / -1}+ #variable{grid-column:span var(--n) / -1}+ #kept{grid-column:2 / 4;grid-column:span 0 / -1}+ #side{grid-column-start:3;grid-column-start:span 0}+ </style></head><body>+ <div class="grid"><div id="literal">x</div></div>+ <div class="grid"><div id="variable">x</div></div>+ <div class="grid"><div id="kept">x</div></div>+ <div class="grid"><div id="side">x</div></div>+ </body></html>"#,+ );+ let layout = layout_dom(&tree, (800.0, 600.0));+ let rect = |id| layout.rects[&tree.get_element_by_id(id).unwrap()];+ for id in ["literal", "variable"] {+ let r = rect(id);+ assert!(+ r.x.abs() < 0.01 && (r.width - 100.0).abs() < 0.01,+ "{id}: an ignored declaration leaves the item auto-placed in the first track: {r:?}"+ );+ }+ let kept = rect("kept");+ assert!(+ (kept.x - 100.0).abs() < 0.01 && (kept.width - 200.0).abs() < 0.01,+ "the earlier valid grid-column must win: {kept:?}"+ );+ let side = rect("side");+ assert!(+ (side.x - 200.0).abs() < 0.01 && (side.width - 100.0).abs() < 0.01,+ "the earlier valid grid-column-start must win: {side:?}"+ );+}+/// Chromium 150 includes collapsed descendant margins in a grid item's/// intrinsic block-size. The nested 64px margin reaches through `section`,/// making the first item 200px tall; the second row therefore begins afterdiff --git a/vendor/taffy/src/compute/grid/mod.rs b/vendor/taffy/src/compute/grid/mod.rsindex 1911e469..85054f93 100644--- a/vendor/taffy/src/compute/grid/mod.rs+++ b/vendor/taffy/src/compute/grid/mod.rs@@ -974,3 +974,45 @@ impl DetailedGridItemsInfo {}}}++#[cfg(test)]+mod tests {+ use crate::prelude::*;++ /// `grid-column: span 0 / -1` (Shopify themes reach it through+ /// `span var(--menu-content-columns-desktop) / -1` with the variable at 0).+ /// A span of zero is invalid CSS; it must not give the item a grid area of+ /// zero tracks, which used to panic in `GridItem::grid_area_size` with+ /// "slice index starts at 13 but ends at 12".+ #[test]+ fn zero_span_is_treated_as_auto() {+ let mut tree: TaffyTree<()> = TaffyTree::new();+ tree.disable_rounding();+ let item = tree+ .new_leaf(Style {+ grid_column: Line { start: span(0), end: line(-1) },+ ..Style::default()+ })+ .unwrap();+ let root = tree+ .new_with_children(+ Style {+ display: Display::Grid,+ size: Size { width: length(600.0), height: auto() },+ grid_template_columns: vec![fr(1.0); 6],+ ..Style::default()+ },+ &[item],+ )+ .unwrap();+ tree.compute_layout_with_measure(root, Size::MAX_CONTENT, |known, _, _, _, _| Size {+ width: known.width.unwrap_or(40.0),+ height: known.height.unwrap_or(10.0),+ })+ .unwrap();+ let layout = tree.layout(item).unwrap();+ // `auto / -1`: one track ending at the last explicit line.+ assert_eq!(layout.location.x, 500.0);+ assert_eq!(layout.size.width, 100.0);+ }+}diff --git a/vendor/taffy/src/style/grid.rs b/vendor/taffy/src/style/grid.rsindex 22c3507b..4447a03d 100644--- a/vendor/taffy/src/style/grid.rs+++ b/vendor/taffy/src/style/grid.rs@@ -497,6 +497,10 @@ impl<S: CheapCloneStr> GridPlacement<S> {pub fn into_origin_zero_placement_ignoring_named(&self, explicit_track_count: u16) -> OriginZeroGridPlacement {match self {Self::Auto => OriginZeroGridPlacement::Auto,+ // A span of zero is invalid (CSS Grid 1 section 8.3: "Negative integers or zero are invalid"),+ // and it would give the item a grid area of zero tracks, which track sizing indexes as an+ // inverted range. Treat it as Auto, like the invalid line zero below.+ Self::Span(0) => OriginZeroGridPlacement::Auto,Self::Span(span) => OriginZeroGridPlacement::Span(*span),// Grid line zero is an invalid index, so it gets treated as Auto// See: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row-start#values@@ -529,6 +533,10 @@ impl NonNamedGridPlacement {) -> OriginZeroGridPlacement {match self {Self::Auto => OriginZeroGridPlacement::Auto,+ // A span of zero is invalid (CSS Grid 1 section 8.3: "Negative integers or zero are invalid"),+ // and it would give the item a grid area of zero tracks, which track sizing indexes as an+ // inverted range. Treat it as Auto, like the invalid line zero below.+ Self::Span(0) => OriginZeroGridPlacement::Auto,Self::Span(span) => OriginZeroGridPlacement::Span(*span),// Grid line zero is an invalid index, so it gets treated as Auto// See: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row-start#values