Skip to content

Truly independent web browser

LibWeb: Hand an unplaced child's geometry to the fragment builder

Aliaksandr Kalenikcfceec808074
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 267424803ae..948bcd9f9f3 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -4593,7 +4593,13 @@ impl<'pass> BlockFormattingContext<'pass> {
None
};
let Some(side) = side else {
- formatting_context::leave_child_unplaced(run, fragment_arrival);
+ formatting_context::leave_child_unplaced(
+ run,
+ float_handed_back_by_its_run
+ .unwrap_or_else(|| formatting_context::PlacedChild::of_record(self.used(node), &self.run))
+ .with_fragment_facts(fragment_facts),
+ fragment_arrival,
+ );
return abspos_engine::InFlowBoxInsets::default();
};
let float_box = float_box.cells;
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index bacf7a5004b..ef87de6cca0 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -3056,13 +3056,14 @@ pub(crate) fn layout_inside_child_with_input_cells(
/// but not placed.
pub(crate) fn leave_child_unplaced(
run: &FormattingContextRun,
+ unplaced_child: PlacedChild,
fragment_arrival: Option<fragment_tree::ChildFragmentArrival>,
) {
if let Some(arrival) = fragment_arrival {
run.fragments
.as_deref()
.expect("only a run that builds fragments is handed a child's fragments")
- .leave_child_unplaced(arrival);
+ .leave_child_unplaced(arrival, unplaced_child);
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
index 38c2b37b0b9..3aa2a9d7b9c 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
@@ -676,6 +676,28 @@ impl PlacementData {
}
}
+ /// The placement data of `node`, a child the run left unplaced, which `close()` hands on as `unplaced_child`
+ /// shows it.
+ fn of_unplaced_child(
+ unplaced_child: &crate::layout::formatting_context::PlacedChild,
+ node: crate::layout::node_data::NodeSlotId,
+ frame: geometry::Frame,
+ ) -> Self {
+ if cfg!(debug_assertions) || crate::layout::run_repeatability::verification_is_enabled() {
+ assert_eq!(
+ unplaced_child.frame, frame,
+ "{node:?} is left unplaced in another frame than the one its box is kept in"
+ );
+ }
+ Self::of_box(
+ &unplaced_child.facts_decided_by_parent,
+ None,
+ None,
+ FfiCssPixelPoint::default(),
+ frame,
+ )
+ }
+
/// The placement data of a box `close()` hands on unplaced or placed as it is.
fn of_box_as_this_run_left_it(
records: &RunRecords,
@@ -708,6 +730,13 @@ fn link_fragment(fragment: std::sync::Arc<Fragment>, placement: PlacementData) -
}
}
+/// A child the run laid out but does not place: what its layout handed its placement, and what placing it would have
+/// taken.
+struct ChildLeftUnplaced {
+ arrival: ChildFragmentArrival,
+ unplaced_child: crate::layout::formatting_context::PlacedChild,
+}
+
#[derive(Clone)]
pub(crate) struct UnplacedRootFragment {
pub(crate) node: crate::layout::node_data::NodeSlotId,
@@ -933,7 +962,7 @@ struct RunFragmentBuilderInner {
reused_subtree_roots: HashSet<u32>,
/// What the layouts of children the run laid out but never placed handed their placements, which close() links at
/// the top of the run's fragments.
- children_left_unplaced: Vec<ChildFragmentArrival>,
+ children_left_unplaced: Vec<ChildLeftUnplaced>,
/// What the run's layout of its root hands the root's fragment.
root_payloads: Option<Box<FragmentPayloads>>,
/// Where the run placed each box it placed so far, from the moment it places the box, before the placement drains
@@ -1284,13 +1313,21 @@ impl RunFragmentBuilder {
taken
}
- /// Keeps what the layout of a child the run laid out but does not place handed its placement.
- pub(crate) fn leave_child_unplaced(&self, arrival: ChildFragmentArrival) {
+ /// Keeps what the layout of a child the run laid out but does not place handed its placement, with what placing the
+ /// child would have taken.
+ pub(crate) fn leave_child_unplaced(
+ &self,
+ arrival: ChildFragmentArrival,
+ unplaced_child: crate::layout::formatting_context::PlacedChild,
+ ) {
debug_assert!(
!self.left_child_unplaced(arrival.node()),
"a child was left unplaced twice"
);
- self.inner.borrow_mut().children_left_unplaced.push(arrival);
+ self.inner.borrow_mut().children_left_unplaced.push(ChildLeftUnplaced {
+ arrival,
+ unplaced_child,
+ });
}
pub(crate) fn left_child_unplaced(&self, node: crate::layout::node_data::NodeSlotId) -> bool {
@@ -1298,7 +1335,7 @@ impl RunFragmentBuilder {
.borrow()
.children_left_unplaced
.iter()
- .any(|arrival| arrival.node() == node)
+ .any(|child| child.arrival.node() == node)
}
pub(crate) fn adopt_fragmentainer_root(
@@ -1717,7 +1754,11 @@ impl RunFragmentBuilder {
// root has, and closes the same way.
// An empty box that was never placed has no fragment in this run.
let mut child_run_roots_left_unplaced = Vec::new();
- for arrival in std::mem::take(&mut inner.children_left_unplaced) {
+ for ChildLeftUnplaced {
+ arrival,
+ unplaced_child,
+ } in std::mem::take(&mut inner.children_left_unplaced)
+ {
match arrival {
ChildFragmentArrival::CommittedFragment {
fragment,
@@ -1725,16 +1766,17 @@ impl RunFragmentBuilder {
} => {
let node = fragment.node;
inner.reused_subtree_roots.insert(node.slot_index());
+ let placement = PlacementData::of_unplaced_child(&unplaced_child, node, callbacks.frame);
inner.top_scope_links.push(link_fragment(
snapshot_fragment(
callbacks,
node,
Vec::new(),
- records.fragment_geometry_of(node, callbacks),
- records.facts_decided_by_parent_of(node).fragment_facts,
+ unplaced_child.geometry,
+ unplaced_child.facts_decided_by_parent.fragment_facts,
Some(Box::new(FragmentPayloads::of_fragment(&fragment))),
),
- PlacementData::of_box_as_this_run_left_it(records, node, callbacks.frame),
+ placement,
));
}
ChildFragmentArrival::CommittedFragment {
@@ -1743,26 +1785,27 @@ impl RunFragmentBuilder {
ChildFragmentArrival::RunRoot {
root,
reuses_committed_subtree,
- } => child_run_roots_left_unplaced.push((root, reuses_committed_subtree)),
+ } => child_run_roots_left_unplaced.push((root, reuses_committed_subtree, unplaced_child)),
}
}
- for (root, reuses_committed_subtree) in child_run_roots_left_unplaced {
+ for (root, reuses_committed_subtree, unplaced_child) in child_run_roots_left_unplaced {
inner
.reused_subtree_roots
.extend(root.reused_subtree_roots.iter().copied());
if reuses_committed_subtree {
inner.reused_subtree_roots.insert(root.node.slot_index());
}
+ let placement = PlacementData::of_unplaced_child(&unplaced_child, root.node, callbacks.frame);
inner.top_scope_links.push(link_fragment(
snapshot_fragment(
callbacks,
root.node,
root.scoped_descendants,
- records.fragment_geometry_of(root.node, callbacks),
- records.facts_decided_by_parent_of(root.node).fragment_facts,
+ unplaced_child.geometry,
+ unplaced_child.facts_decided_by_parent.fragment_facts,
root.root_payloads,
),
- PlacementData::of_box_as_this_run_left_it(records, root.node, callbacks.frame),
+ placement,
));
}
let placed_boxes = &inner.placed_boxes;
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 068aadd9daa..203ce04ea1d 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -2505,8 +2505,8 @@ impl<'context> InlineFormattingContext<'context> {
std::cmp::Ordering::Greater
}
});
- for (_, fragment_arrival) in unplaced {
- formatting_context::leave_child_unplaced(self.run, Some(fragment_arrival));
+ for (node, fragment_arrival) in unplaced {
+ formatting_context::leave_child_unplaced(self.run, self.placed_atomic_inline(node), Some(fragment_arrival));
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs
index 79fd003ecf2..619a51eaf59 100644
--- a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs
@@ -1463,6 +1463,15 @@ pub(super) struct MathFormattingContext<'pass> {
}
impl<'pass> MathFormattingContext<'pass> {
+ #[track_caller]
+ fn placed_child(&self, child: Node) -> formatting_context::PlacedChild {
+ self.placed_children
+ .borrow()
+ .get(&child)
+ .cloned()
+ .expect("a math child is laid out before it is placed")
+ }
+
fn take_fragment_arrival_of_child(&self, child: Node) -> Option<fragment_tree::ChildFragmentArrival> {
let mut arrivals = self.fragment_arrivals_of_children.borrow_mut();
let index = arrivals.iter().position(|arrival| arrival.node() == child)?;
@@ -2304,15 +2313,15 @@ impl<'pass> MathFormattingContext<'pass> {
y: margin_box_top + metrics.margin_box_top(collapsed),
};
let fragment_arrival = self.take_fragment_arrival_of_child(child.node);
- let placed_child = self
- .placed_children
- .borrow_mut()
- .remove(&child.node)
- .expect("a math child is laid out before it is placed");
+ let placed_child = self.placed_child(child.node);
formatting_context::place_laid_out_child(run, child.node, placed_child, offset, None, fragment_arrival);
}
for fragment_arrival in self.fragment_arrivals_of_children.take() {
- formatting_context::leave_child_unplaced(run, Some(fragment_arrival));
+ formatting_context::leave_child_unplaced(
+ run,
+ self.placed_child(fragment_arrival.node()),
+ Some(fragment_arrival),
+ );
}
if run.fragments.is_some() && (!content.rules.is_empty() || !content.glyph_runs.is_empty()) {