Skip to content

The headless browser for AI agents and web scraping

fix(render): preserve mixed inline owner geometry

SGavrl62e8e7fb5cf4
diff --git a/crates/obscura-render/src/dom.rs b/crates/obscura-render/src/dom.rs
index 90245e84..3d477d63 100644
--- a/crates/obscura-render/src/dom.rs
+++ b/crates/obscura-render/src/dom.rs
@@ -13952,9 +13952,9 @@ fn inline_wraps_only_in_flow_blocks(
/// Recursively splice `display:contents`, decoration-free inline wrappers, and
/// inline wrappers whose only generated flow content is block-level into a
/// block container's effective child list. Descendants already carry inherited
-/// computed text styles, so removing these wrappers preserves shaping while
-/// exposing block-in-inline descendants early enough for anonymous block
-/// construction.
+/// computed text styles. Preserve wrappers the text engine can fold so their
+/// shaped runs retain element geometry; expose block-in-inline descendants
+/// early enough for anonymous block construction.
fn flatten_boxless_inline_children(
tree: &DomTree,
children: &[NodeId],
@@ -13970,6 +13970,14 @@ fn flatten_boxless_inline_children(
|| is_flattenable_inline(tree, cid, styles)
|| inline_wraps_only_in_flow_blocks(tree, cid, styles)
{
+ #[cfg(feature = "paint")]
+ if !display_contents {
+ let mut has_text = false;
+ if crate::inline::inline_child_ok(tree, cid, styles, &mut has_text) {
+ out.push(cid);
+ continue;
+ }
+ }
let kids = rendered_children(tree, cid);
flatten_boxless_inline_children(tree, &kids, styles, out);
} else {
diff --git a/crates/obscura-render/src/inline.rs b/crates/obscura-render/src/inline.rs
index 0897de55..06549b2b 100644
--- a/crates/obscura-render/src/inline.rs
+++ b/crates/obscura-render/src/inline.rs
@@ -3182,7 +3182,7 @@ pub(crate) fn default_replaced_intrinsic_size(
/// genuinely cannot fold are rejected: replaced/atomic elements, block-level
/// children, floats, out-of-flow positioned boxes, and elements with generated
/// content (which would be lost).
-fn inline_child_ok(
+pub(crate) fn inline_child_ok(
tree: &DomTree,
cid: NodeId,
styles: &std::collections::HashMap<NodeId, LayoutStyle>,
diff --git a/crates/obscura-render/src/paint.rs b/crates/obscura-render/src/paint.rs
index 7e676839..3cd295b2 100644
--- a/crates/obscura-render/src/paint.rs
+++ b/crates/obscura-render/src/paint.rs
@@ -16343,9 +16343,9 @@ mod tests {
assert!(
(20..100).any(|x| (20..60).any(|y| {
let pixel = pixmap.pixel(x, y).expect("scaled text region");
- pixel.red() < 80 && pixel.green() < 80 && pixel.blue() < 80
+ pixel.red() < 80 && pixel.green() < 80 && pixel.blue() > 160
})),
- "text must be rasterized inside the scaled atomic subtree"
+ "authored blue text must be rasterized inside the scaled atomic subtree"
);
let rotated_box = pixmap.pixel(165, 25).expect("rotated box");
diff --git a/crates/obscura-render/tests/layout_test.rs b/crates/obscura-render/tests/layout_test.rs
index c40f2bbd..273279a9 100644
--- a/crates/obscura-render/tests/layout_test.rs
+++ b/crates/obscura-render/tests/layout_test.rs
@@ -34,6 +34,40 @@ const HN_HTML: &str = r##"
</table>
"##;
+#[cfg(feature = "paint")]
+#[test]
+fn mixed_content_shaped_runs_retain_inline_owner_geometry() {
+ for content in ["ABCD", "AB<b id='nested'>CD</b>"] {
+ let tree = parse_html(&format!(
+ "<style>body{{margin:0;font:20px/24px monospace}}</style>\
+ <div id='parent'>AAAA<div style='height:30px'></div>\
+ <span id='owner'>{content}</span></div>"));
+ let layout = layout_dom(&tree, (200.0, 200.0));
+ let owner = tree.get_element_by_id("owner").unwrap();
+ let rect = layout.rects.get(&owner).expect("shaped inline owner has geometry");
+ assert!(rect.x.abs() < 0.01 && (52.0..=57.0).contains(&rect.y)
+ && (40.0..=55.0).contains(&rect.width) && (18.0..=26.0).contains(&rect.height),
+ "inline following a 24px line and 30px block: {rect:?}");
+ let pieces = layout.inline_fragments.get(&owner).expect("canonical inline fragments");
+ assert!(!pieces.is_empty());
+ for piece in pieces {
+ assert!(piece.width > 0.0 && piece.height > 0.0
+ && piece.x >= rect.x && piece.y >= rect.y
+ && piece.x + piece.width <= rect.x + rect.width + 0.01
+ && piece.y + piece.height <= rect.y + rect.height + 0.01,
+ "inline fragments remain within their owner bounds: {piece:?}, {rect:?}");
+ }
+ let parent = tree.get_element_by_id("parent").unwrap();
+ assert!((layout.rects[&parent].height - 78.0).abs() < 0.01,
+ "preserving inline ownership must not add a line or change block flow");
+ if let Some(nested) = tree.get_element_by_id("nested") {
+ let nested = layout.rects.get(&nested).expect("nested inline owner has geometry");
+ assert!((20.0..=28.0).contains(&nested.x) && (20.0..=28.0).contains(&nested.width)
+ && (nested.y - rect.y).abs() < 1.0, "nested text bounds: {nested:?}");
+ }
+ }
+}
+
/// Top-left of the tightest laid-out element box whose text contains
/// `needle`. Text geometry is no longer a per-word list: a pure-text
/// container collapses to a single cosmic-text inline formatting context