Skip to content

The headless browser for AI agents and web scraping

fix(render): resolve percentage heights across anonymous inline wrappers

SGavrl0e99b3afb5f7
diff --git a/crates/obscura-render/src/dom.rs b/crates/obscura-render/src/dom.rs
index 90245e84..f7994a85 100644
--- a/crates/obscura-render/src/dom.rs
+++ b/crates/obscura-render/src/dom.rs
@@ -1952,18 +1952,17 @@ fn sync_resolved_percentage_padding(
}
/// Taffy's flex stand-in for an inline formatting context can lose the
-/// block-axis percentage basis of an atomic containing block. This is most
-/// visible when the atomic box is floated: the float's synthetic placement
-/// wrapper has an indefinite block size, so a `height:100%` descendant
-/// collapses even though the float itself has a definite used height.
+/// block-axis percentage basis of its real containing block. Anonymous
+/// inline-run and float-placement wrappers have indefinite block sizes, so a
+/// `height:100%` descendant can collapse despite a definite containing block.
///
/// Blink's `CalculateChildAvailableSize` and Gecko's `ReflowInput` both pass
/// the containing block's used content-box block size to such descendants.
/// Reify that basis after the preliminary layout, when min/max constraints and
/// border-box edges are known exactly. Keep the repair to ordinary direct
-/// descendants of definite floated/inline-block containing blocks; grid-area,
-/// flex-item, positioned, and anonymous containing-block rules remain native.
-fn resolve_atomic_percentage_heights(
+/// descendants of atomic containing blocks or ordinary block children crossing
+/// anonymous inline wrappers. Grid-area, flex-item and positioned rules remain native.
+fn resolve_inline_percentage_heights(
tree: &DomTree,
taffy_tree: &mut TaffyTree<usize>,
taffy_root: taffy::NodeId,
@@ -2000,13 +1999,16 @@ fn resolve_atomic_percentage_heights(
_ => None,
})
});
- let parent_is_definite_atomic = styles.get(&parent_id).is_some_and(|style| {
- (style.float.is_some() || style.is_inline_block)
+ let parent_is_definite = styles.get(&parent_id).is_some_and(|style| {
+ (style.float.is_some() || style.is_inline_block
+ || (style.display == crate::Display::Block
+ && style.column_count.is_none()
+ && taffy_tree.parent(node).is_some_and(|parent| !id_map.contains_key(&parent))))
&& definite_height_nodes.contains(&parent_id)
&& style.size_expressions[1].is_none()
});
if is_direct_dom_child
- && parent_is_definite_atomic
+ && parent_is_definite
&& containing_block_height.is_finite()
{
if let Some(percent) = child_percent {
@@ -7242,7 +7244,7 @@ fn layout_dom_once(
let _ =
taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
- if resolve_atomic_percentage_heights(
+ if resolve_inline_percentage_heights(
tree,
&mut taffy_tree,
taffy_root,
@@ -7381,7 +7383,7 @@ fn layout_dom_once(
{
let _ = taffy_tree.compute_layout(taffy_root, available);
}
- if resolve_atomic_percentage_heights(
+ if resolve_inline_percentage_heights(
tree,
&mut taffy_tree,
taffy_root,
diff --git a/crates/obscura-render/tests/layout_test.rs b/crates/obscura-render/tests/layout_test.rs
index c40f2bbd..6f6030bb 100644
--- a/crates/obscura-render/tests/layout_test.rs
+++ b/crates/obscura-render/tests/layout_test.rs
@@ -4485,6 +4485,34 @@ fn grid_replaced_classification_keeps_controls_stretched_and_media_natural() {
assert_eq!(auto_button.x + auto_button.width, auto_button_grid.x + auto_button_grid.width);
}
+#[test]
+fn inline_block_percentage_height_uses_definite_block_content_height() {
+ let tree = parse_html(r#"<!doctype html>
+ <style>
+ html, body { margin:0 }
+ .switch { position:relative; width:32px; height:16px; line-height:1 }
+ input { position:absolute; width:100%; height:100%; margin:0; opacity:0; z-index:-1000 }
+ label { display:inline-block; box-sizing:border-box; width:100%; height:100%; border:1px solid }
+ .padded { height:30px; box-sizing:border-box; padding:4px; border:1px solid }
+ .indefinite { height:auto; min-height:40px }
+ </style>
+ <div class="switch"><input type="checkbox"><label id="percentage"></label></div>
+ <div class="switch"><input type="checkbox"><label id="pixels" style="height:16px"></label></div>
+ <div class="switch padded"><label id="content-box"></label></div>
+ <div class="switch padded"><label id="half" style="height:50%"></label></div>
+ <div class="switch indefinite"><label id="indefinite"></label></div>
+ "#);
+ let layout = layout_dom(&tree, (800.0, 600.0));
+ for (name, width, height) in [
+ ("percentage", 32.0, 16.0), ("pixels", 32.0, 16.0),
+ ("content-box", 22.0, 20.0), ("half", 22.0, 10.0), ("indefinite", 32.0, 2.0),
+ ] {
+ let rect = layout.rects[&tree.get_element_by_id(name).unwrap()];
+ assert_eq!(rect.width, width, "{name}");
+ assert_eq!(rect.height, height, "{name}");
+ }
+}
+
#[test]
fn grid_ordinary_aspect_ratio_preserves_normal_alignment_provenance() {
let tree = parse_html(