diff --git a/moli-layout/src/layout_tree/source_query.rs b/moli-layout/src/layout_tree/source_query.rs index ad18f64f7de..70e01832690 100644 --- a/moli-layout/src/layout_tree/source_query.rs +++ b/moli-layout/src/layout_tree/source_query.rs @@ -6,9 +6,9 @@ use crate::LayoutPosition; use super::{ model::{ - LayoutBoxModel, LayoutCoordinateSpaceId, LayoutFragmentBoxModel, LayoutFragmentKind, - LayoutOutputBoxId, LayoutPoint, LayoutQuad, LayoutRect, LayoutResolvedGridTracks, - LayoutSize, LayoutTransform2D, LayoutUsedSize, + LayoutBoxModel, LayoutCoordinateSpaceId, LayoutFragmentBoxModel, LayoutFragmentId, + LayoutFragmentKind, LayoutOutputBoxId, LayoutPoint, LayoutQuad, LayoutRect, + LayoutResolvedGridTracks, LayoutSize, LayoutTransform2D, LayoutUsedSize, }, query::{ LayoutElementMetrics, LayoutNodeOutput, LayoutResizeObserverMetrics, @@ -390,8 +390,7 @@ where } pub(super) fn client_rects_for_output(&self, output: &LayoutNodeOutput) -> Vec { - output - .fragments + self.geometry_fragment_ids(output) .iter() .filter_map(|id| self.fragment(*id)) .filter(|fragment| { @@ -424,14 +423,102 @@ where }) } + /// The element's own fragments, plus the block boxes that block-in-inline + /// splitting moved out of an inline element (e.g. `
..
`). + /// Those blocks are part of the inline's geometry; its zero-width edge + /// fragments around them are dropped. + fn geometry_fragment_ids<'a>( + &self, + output: &'a LayoutNodeOutput, + ) -> std::borrow::Cow<'a, [LayoutFragmentId]> { + let is_inline = |id: &LayoutFragmentId| { + self.fragment(*id).is_some_and(|fragment| { + matches!(fragment.kind, LayoutFragmentKind::InlineBox { .. }) + }) + }; + // Only a block-in-inline split creates continuation boxes, so ordinary + // inlines do no extra work. + let Some(principal) = output.principal_box.filter(|principal| { + output.fragments.iter().any(|id| { + self.fragment(*id).is_some_and(|fragment| { + matches!(fragment.kind, LayoutFragmentKind::InlineBox { box_id, .. } if box_id != *principal) + }) + }) + }) else { + return output.fragments.as_slice().into(); + }; + let has_inline_fragment = + |layout_box: &FrozenLayoutBox| layout_box.geometry.fragments.iter().any(is_inline); + // The box's source ancestry reaches this inline through non-atomic + // inlines only (e.g. `
`). + let split_from_principal = |layout_box: &FrozenLayoutBox| { + let mut parent = layout_box.geometry.structural_parent; + while let Some(id) = parent { + if id == principal { + return true; + } + match self.boxes.get(id.index()) { + Some(ancestor) if has_inline_fragment(ancestor) => { + parent = ancestor.geometry.structural_parent; + } + _ => return false, + } + } + false + }; + let split_blocks = self + .boxes + .iter() + .filter(|layout_box| { + !has_inline_fragment(layout_box) + && split_from_principal(layout_box) + && layout_box + .geometry + .parent + .and_then(|parent| self.boxes.get(parent.index())) + .is_some_and(|parent| !has_inline_fragment(parent)) + }) + .flat_map(|layout_box| layout_box.geometry.fragments.iter().copied()) + .filter(|id| { + self.fragment(*id) + .is_some_and(|fragment| matches!(fragment.kind, LayoutFragmentKind::Box { .. })) + }) + .collect::>(); + if split_blocks.is_empty() { + return output.fragments.as_slice().into(); + } + let top = |id: &LayoutFragmentId| { + self.fragment(*id) + .and_then(|fragment| { + self.coordinate_space(fragment.coordinate_space) + .map(|space| { + space + .local_to_viewport + .map_rect(fragment.rect) + .bounding_rect() + .y + }) + }) + .unwrap_or_default() + }; + let mut ids = output + .fragments + .iter() + .copied() + .filter(|id| !is_inline(id) || self.fragment(*id).is_some_and(|f| f.rect.width > 0.0)) + .chain(split_blocks) + .collect::>(); + ids.sort_by(|a, b| top(a).total_cmp(&top(b))); + ids.into() + } + pub fn content_quads_for_source(&self, source: N) -> Vec { self.source_output(source) .map_or_else(Vec::new, |output| self.content_quads_for_output(&output)) } pub(super) fn content_quads_for_output(&self, output: &LayoutNodeOutput) -> Vec { - output - .fragments + self.geometry_fragment_ids(output) .iter() .filter_map(|id| self.fragment(*id)) .filter(|fragment| { diff --git a/moli-layout/tests/phase5_output_contract.rs b/moli-layout/tests/phase5_output_contract.rs index 30867447840..8203d9a051d 100644 --- a/moli-layout/tests/phase5_output_contract.rs +++ b/moli-layout/tests/phase5_output_contract.rs @@ -2527,6 +2527,90 @@ fn pass_result_owns_complete_box_models_and_answers_a_batch_from_one_pass() { assert_eq!(answers.answers[4], LayoutQueryAnswer::ElementMetrics(None)); } +#[test] +fn inline_with_block_child_reports_the_block_geometry() { + //
: the block split out of the inline + // is the inline's geometry, without zero-width edge fragments. + let source = Source(vec![ + Node::element("root", vec![1]), + Node::element("a", vec![2]), + Node::element("div", Vec::new()), + ]); + let mut styles = Styles::default(); + styles + .0 + .insert(0, fixed_size(LayoutDisplay::Block, 800.0, 600.0)); + styles + .0 + .insert(1, resolved(LayoutDisplay::Inline, Style::default())); + styles + .0 + .insert(2, fixed_size(LayoutDisplay::Block, 120.0, 20.0)); + + let output = build(&source, &mut styles); + let block = output.client_rects_for_source(2)[0].bounding_rect(); + let rects = output.client_rects_for_source(1); + assert_eq!(rects.len(), 1, "{rects:?}"); + assert_rect(rects[0].bounding_rect(), block); + assert_eq!(block.width, 120.0); + let quads = output.content_quads_for_source(1); + assert_eq!(quads.len(), 1, "{quads:?}"); + assert_rect(quads[0].bounding_rect(), block); +} + +#[test] +fn inline_with_nested_inline_block_child_reports_the_block_geometry() { + //
+ // Only the block split out of the inlines is their geometry; atomic and + // out-of-flow boxes inside the nested inline are not. + let source = Source(vec![ + Node::element("root", vec![1]), + Node::element("a", vec![2]), + Node::element("span", vec![3, 4, 5]), + Node::element("div", Vec::new()), + Node::element("inline-block", Vec::new()), + Node::element("abspos", Vec::new()), + ]); + let mut styles = Styles::default(); + styles + .0 + .insert(0, fixed_size(LayoutDisplay::Block, 800.0, 600.0)); + styles + .0 + .insert(1, resolved(LayoutDisplay::Inline, Style::default())); + styles + .0 + .insert(2, resolved(LayoutDisplay::Inline, Style::default())); + styles + .0 + .insert(3, fixed_size(LayoutDisplay::Block, 120.0, 20.0)); + styles + .0 + .insert(4, fixed_size(LayoutDisplay::InlineBlock, 30.0, 10.0)); + styles.0.insert( + 5, + fixed_size(LayoutDisplay::Block, 40.0, 10.0).with_position(LayoutPosition::Absolute), + ); + + let output = build(&source, &mut styles); + let rect_of = |source| output.client_rects_for_source(source)[0].bounding_rect(); + let (block, inline_block, abspos) = (rect_of(3), rect_of(4), rect_of(5)); + for inline in [1, 2] { + let rects = output + .client_rects_for_source(inline) + .iter() + .map(|quad| quad.bounding_rect()) + .collect::>(); + // The block, then the line fragment that holds the inline-block. + assert_eq!(rects.len(), 2, "{inline}: {rects:?}"); + assert_rect(rects[0], block); + assert!( + !rects.contains(&inline_block) && !rects.contains(&abspos), + "{inline}: {rects:?}" + ); + } +} + #[test] fn own_border_is_visual_geometry_not_scrollable_content() { let source = Source(vec![ @@ -3063,12 +3147,15 @@ fn split_inline_continuations_remain_mapped_to_the_originating_element() { let output = build(&source, &mut styles); assert_fragment_batches_match_direct_queries(&output, 0..7); + // Before text, the block split out of the inline, after text. let rects = output.client_rects_for_source(1); - assert_eq!(rects.len(), 2, "{rects:?}"); + assert_eq!(rects.len(), 3, "{rects:?}"); let first = rects[0].bounding_rect(); - let second = rects[1].bounding_rect(); + let block = rects[1].bounding_rect(); + let second = rects[2].bounding_rect(); assert!(first.width > 0.0 && second.width > 0.0); - assert!(second.y > first.y, "{rects:?}"); + assert_eq!(block.width, 50.0); + assert!(block.y > first.y && second.y > block.y, "{rects:?}"); let union = output .box_model_for_source(1) .expect("split inline box model")