Skip to content

The headless browser for AI agents and web scraping

fix(render): retain intrinsic size for stretched grid controls

SGavrl6234ed387638
diff --git a/crates/obscura-render/src/dom.rs b/crates/obscura-render/src/dom.rs
index 90245e84..68cb182d 100644
--- a/crates/obscura-render/src/dom.rs
+++ b/crates/obscura-render/src/dom.rs
@@ -6817,10 +6817,7 @@ fn layout_dom_once(
width: taffy::AvailableSpace::Definite(initial_cb_width),
height: taffy::AvailableSpace::Definite(viewport.1),
};
- #[cfg(feature = "paint")]
- {
- let engine = &mut engine;
- let mut measure = |known: taffy::Size<Option<f32>>,
+ let mut measure = |known: taffy::Size<Option<f32>>,
avail: taffy::Size<taffy::AvailableSpace>,
_node,
ctx: Option<&mut usize>,
@@ -6831,6 +6828,9 @@ fn layout_dom_once(
}
};
+ #[cfg(feature = "paint")]
+ {
+
// Table used-width pass. A grid table is built at width:auto, so
// its final width has to be chosen the way CSS chooses a table's
// used width: at least the content min-content (so an unbreakable
@@ -7359,7 +7359,7 @@ fn layout_dom_once(
}
#[cfg(not(feature = "paint"))]
{
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
if deferred_cyclic_inline_sizes.is_empty()
&& apply_fit_content_widths(
&mut taffy_tree,
@@ -7367,19 +7367,20 @@ fn layout_dom_once(
&styles,
initial_cb_width,
|tree, node, width| {
- tree.compute_layout(
+ tree.compute_layout_with_measure(
node,
taffy::Size {
width,
height: taffy::AvailableSpace::MaxContent,
},
+ &mut measure,
)
.ok()?;
tree.layout(node).ok().map(|layout| layout.size.width)
},
)
{
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
if resolve_atomic_percentage_heights(
tree,
@@ -7389,11 +7390,11 @@ fn layout_dom_once(
&styles,
&definite_height_nodes,
) {
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
if repair_intrinsic_column_flex_negative_margins(&mut taffy_tree, &id_map, &styles)
{
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
let _ = resolve_deferred_flex_inline_sizes(
tree,
@@ -7406,7 +7407,7 @@ fn layout_dom_once(
vh,
|tree, resolved_styles, phase| match phase {
DeferredFlexReflowPhase::Layout => {
- let _ = tree.compute_layout(taffy_root, available);
+ let _ = tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
DeferredFlexReflowPhase::FitContent => {
if apply_fit_content_widths(
@@ -7415,30 +7416,31 @@ fn layout_dom_once(
resolved_styles,
initial_cb_width,
|tree, node, width| {
- tree.compute_layout(
+ tree.compute_layout_with_measure(
node,
taffy::Size {
width,
height: taffy::AvailableSpace::MaxContent,
},
+ &mut measure,
)
.ok()?;
tree.layout(node).ok().map(|layout| layout.size.width)
},
) {
- let _ = tree.compute_layout(taffy_root, available);
+ let _ = tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
}
},
);
if apply_multicol_balance(&mut taffy_tree, &ifc_items.multicol) {
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
if apply_float_continuations(tree, &mut taffy_tree, &id_map, &styles, &ifc_items) {
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
if apply_table_row_geometry(&mut taffy_tree, &id_map, &styles, &ifc_items) {
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
if apply_full_span_column_subgrids(
tree,
@@ -7446,21 +7448,22 @@ fn layout_dom_once(
&id_map,
&styles,
|tree, node| {
- tree.compute_layout(
+ tree.compute_layout_with_measure(
node,
taffy::Size {
width: taffy::AvailableSpace::MaxContent,
height: taffy::AvailableSpace::MaxContent,
},
+ &mut measure,
)
.ok()?;
tree.layout(node).ok().map(|layout| layout.size.width)
},
) {
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
if apply_table_cell_block_alignment(tree, &mut taffy_tree, &id_map, &styles) {
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
// Keep the no-paint geometry path in the same final-position
// contract as screenshots: static coordinates are resolved
@@ -7470,7 +7473,7 @@ fn layout_dom_once(
&mut taffy_tree,
&static_position_candidates,
);
- let _ = taffy_tree.compute_layout(taffy_root, available);
+ let _ = taffy_tree.compute_layout_with_measure(taffy_root, available, &mut measure);
}
}
sync_resolved_percentage_padding(
@@ -12027,6 +12030,14 @@ fn assign_native_control_size(
) {
let (stretch_inline, stretch_block) = stretched_grid_item;
let content_box = style.box_sizing == crate::BoxSizing::ContentBox;
+ if stretch_inline || stretch_block {
+ // Stretch controls the final size, not the intrinsic track contribution.
+ // Measure content axes independently; native controls have no natural ratio.
+ style.intrinsic_size = Some((
+ (intrinsic_width - horizontal_edges).max(0.0),
+ (intrinsic_height - vertical_edges).max(0.0),
+ ));
+ }
if style.width == crate::Dimension::Auto && !stretch_inline {
let declared = if content_box {
(intrinsic_width - horizontal_edges).max(0.0)
@@ -12870,6 +12881,15 @@ fn build(
}
}
+ if matches!(_name.local.as_ref(), "input" | "select" | "textarea") {
+ if let Some((width, height)) = style.intrinsic_size {
+ let context = engine.register_native_control(width, height, style);
+ let leaf = taffy_tree.new_leaf_with_context(taffy_style, context).ok()?;
+ id_map.insert(leaf, id);
+ return Some(leaf);
+ }
+ }
+
// An inline SVG is an atomic replaced box in the surrounding formatting
// context. Its descendants paint inside its SVG viewport; they must not
// become CSS layout children. In particular, a viewBox supplies an
diff --git a/crates/obscura-render/src/inline.rs b/crates/obscura-render/src/inline.rs
index 0897de55..aabaf609 100644
--- a/crates/obscura-render/src/inline.rs
+++ b/crates/obscura-render/src/inline.rs
@@ -1074,6 +1074,7 @@ const REPLACED_CONTEXT_BIT: usize = 1usize << (usize::BITS - 1);
#[derive(Clone, Copy)]
struct ReplacedItem {
+ independent_axes: bool,
intrinsic_width: Option<f32>,
intrinsic_height: Option<f32>,
preferred_width: Option<f32>,
@@ -1131,6 +1132,7 @@ impl ReplacedItem {
})
.unwrap_or(2.0);
ReplacedItem {
+ independent_axes: false,
intrinsic_width: intrinsic
.width
.filter(|width| width.is_finite() && *width > 0.0),
@@ -1210,6 +1212,12 @@ impl ReplacedItem {
}
fn size(self, known: taffy::Size<Option<f32>>) -> taffy::Size<f32> {
+ if self.independent_axes {
+ return taffy::Size {
+ width: known.width.or(self.intrinsic_width).unwrap_or(0.0),
+ height: known.height.or(self.intrinsic_height).unwrap_or(0.0),
+ };
+ }
let (width, height) = match (known.width, known.height) {
(Some(width), Some(height)) => (width, height),
(Some(width), None) => (width, width / self.preferred_ratio),
@@ -1898,6 +1906,16 @@ impl TextEngine {
)
}
+ pub(crate) fn register_native_control(
+ &mut self, width: f32, height: f32, style: &LayoutStyle,
+ ) -> usize {
+ let mut item = ReplacedItem::from_style(width, height, style);
+ item.independent_axes = true;
+ let index = self.replaced.len();
+ self.replaced.push(item);
+ REPLACED_CONTEXT_BIT | index
+ }
+
pub(crate) fn register_replaced_intrinsic(
&mut self,
intrinsic: crate::ReplacedIntrinsic,
diff --git a/crates/obscura-render/src/lib.rs b/crates/obscura-render/src/lib.rs
index d72bef51..e4fac858 100644
--- a/crates/obscura-render/src/lib.rs
+++ b/crates/obscura-render/src/lib.rs
@@ -156,7 +156,9 @@ pub mod inline {
}
#[derive(Default)]
- pub struct TextEngine;
+ pub struct TextEngine {
+ controls: Vec<(taffy::Size<f32>, bool)>,
+ }
pub(crate) fn text_may_need_emoji_font(_text: &str) -> bool {
false
@@ -164,15 +166,44 @@ pub mod inline {
impl TextEngine {
pub fn new() -> Self {
- TextEngine
+ Self::default()
}
pub(crate) fn new_with_web_fonts(_fonts: &[WebFont]) -> Self {
- TextEngine
+ Self::default()
}
pub(crate) fn new_with_web_fonts_and_emoji(_fonts: &[WebFont], _load_emoji: bool) -> Self {
- TextEngine
+ Self::default()
+ }
+
+ pub(crate) fn register_native_control(
+ &mut self, width: f32, height: f32, style: &crate::LayoutStyle,
+ ) -> usize {
+ let index = self.controls.len();
+ let zero_min_content = matches!(style.width, crate::Dimension::Percent(_))
+ || matches!(style.max_width, crate::Dimension::Percent(_))
+ || [0, 4].into_iter().any(|index| style.size_expressions[index]
+ .as_deref().is_some_and(|expression| expression.contains('%')));
+ self.controls.push((taffy::Size { width, height }, zero_min_content));
+ (1usize << (usize::BITS - 1)) | index
+ }
+
+ pub fn measure_taffy(
+ &mut self, index: usize, known: taffy::Size<Option<f32>>,
+ available: taffy::Size<taffy::AvailableSpace>,
+ ) -> taffy::Size<f32> {
+ let bit = 1usize << (usize::BITS - 1);
+ if index & bit == 0 { return taffy::Size::ZERO; }
+ let (intrinsic, zero_min_content) = self.controls[index & !bit];
+ taffy::Size {
+ width: known.width.unwrap_or_else(|| {
+ if zero_min_content && matches!(available.width, taffy::AvailableSpace::MinContent) {
+ 0.0
+ } else { intrinsic.width }
+ }),
+ height: known.height.unwrap_or(intrinsic.height),
+ }
}
pub fn register_replaced(
@@ -247,7 +278,7 @@ pub mod inline {
}
pub(crate) fn used_line_height(style: &crate::LayoutStyle) -> f32 {
- TextEngine.selected_line_height(style)
+ TextEngine::new().selected_line_height(style)
}
pub(crate) fn is_replaced(local: &str) -> bool {
diff --git a/crates/obscura-render/tests/layout_test.rs b/crates/obscura-render/tests/layout_test.rs
index 647db96d..f86dfe16 100644
--- a/crates/obscura-render/tests/layout_test.rs
+++ b/crates/obscura-render/tests/layout_test.rs
@@ -4423,6 +4423,36 @@ fn grid_replaced_normal_and_explicit_stretch_match_browser_geometry() {
assert_eq!(item.x - layout.rects[&parent].x, 150.0);
}
+#[test]
+fn nested_grid_input_contributes_intrinsic_height_without_preventing_stretch() {
+ let tree = parse_html(
+ r#"
+ <style>
+ html, body { margin:0; font:14px Arial; line-height:22px }
+ .outer { display:grid; width:214px; align-items:center }
+ .editor { display:inline-grid; grid-area:1 / 1 / 2 / 3;
+ grid-template-columns:0 min-content }
+ input { display:block; width:100%; grid-area:1 / 2;
+ font:inherit; min-width:2px; padding:0; border:0 }
+ .short { grid-template-rows:12px }
+ </style>
+ <div class="outer"><div class="editor" id="auto-editor"><input id="auto-input"></div></div>
+ <div class="outer"><div class="editor short" id="short-editor"><input id="short-input"></div></div>
+ "#,
+ );
+ let layout = layout_dom(&tree, (800.0, 600.0));
+ for (input, editor, height) in [
+ ("auto-input", "auto-editor", 22.0),
+ ("short-input", "short-editor", 12.0),
+ ] {
+ let input_rect = layout.rects[&tree.get_element_by_id(input).unwrap()];
+ let editor_rect = layout.rects[&tree.get_element_by_id(editor).unwrap()];
+ assert_eq!(input_rect.height, height, "{input}");
+ assert_eq!(editor_rect.height, height, "{editor}");
+ assert!(input_rect.width >= 2.0, "{input}");
+ }
+}
+
#[test]
fn grid_replaced_classification_keeps_controls_stretched_and_media_natural() {
let tree = parse_html(