Skip to content

Truly independent web browser

LibWeb: Let a child's placement take the fragments handed to it

Aliaksandr Kalenikd0aa35ef3d6c
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 60c305271f9..95f0d5ef136 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -3123,7 +3123,6 @@ impl<'pass> AbsposEngine<'pass> {
),
false,
);
- formatting_context::hold_fragment_arrival_until_placement(run, node, fragment_arrival);
outcome.report_percentage_block_size_dependency_of(run, node);
let mut used_insets = match outcome {
ChildLayoutOutcome::Created(result) => result
@@ -3188,6 +3187,7 @@ impl<'pass> AbsposEngine<'pass> {
},
None,
Some(inputs),
+ fragment_arrival,
);
}
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 53d1eb17c5f..edc5e43a8e0 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -364,9 +364,28 @@ pub(crate) fn place_child(
node: Node,
offset: FfiCssPixelPoint,
containing_line_box_fragment: Option<used_values::LineBoxFragmentCoordinate>,
+) -> Option<fragment_tree::PlacedFragment> {
+ place_laid_out_child(run, node, offset, containing_line_box_fragment, None)
+}
+
+/// Places `node`, taking in what the layout of the box handed its placement.
+pub(crate) fn place_laid_out_child(
+ run: &FormattingContextRun,
+ node: Node,
+ offset: FfiCssPixelPoint,
+ containing_line_box_fragment: Option<used_values::LineBoxFragmentCoordinate>,
+ fragment_arrival: Option<fragment_tree::ChildFragmentArrival>,
) -> Option<fragment_tree::PlacedFragment> {
let containing_block = run.callbacks.in_flow_containing_block(node);
- place_child_in_containing_block(run, node, containing_block, offset, containing_line_box_fragment, None)
+ place_child_in_containing_block(
+ run,
+ node,
+ containing_block,
+ offset,
+ containing_line_box_fragment,
+ None,
+ fragment_arrival,
+ )
}
/// Places `node` in `containing_block`. An absolutely positioned box hands its link the inputs it was laid out with.
@@ -377,6 +396,7 @@ pub(crate) fn place_child_in_containing_block(
offset: FfiCssPixelPoint,
containing_line_box_fragment: Option<used_values::LineBoxFragmentCoordinate>,
abspos_layout_inputs: Option<abspos_inputs::AbsposLayoutInputs>,
+ fragment_arrival: Option<fragment_tree::ChildFragmentArrival>,
) -> Option<fragment_tree::PlacedFragment> {
let records = run.records;
let callbacks = &run.callbacks;
@@ -386,11 +406,45 @@ pub(crate) fn place_child_in_containing_block(
containing_block,
};
records.place_box(node, placement);
- let fragments = run.fragments.as_deref()?;
+ let Some(fragments) = run.fragments.as_deref() else {
+ debug_assert!(
+ fragment_arrival.is_none(),
+ "a run that builds no fragments is handed none"
+ );
+ return None;
+ };
let facts_decided_by_parent = records.facts_decided_by_parent_of(node);
- if let Some((fragment, reuses_subtree)) =
- fragments.take_reused_committed_fragment(callbacks, node, used, facts_decided_by_parent.fragment_facts)
- {
+ debug_assert!(
+ fragment_arrival
+ .as_ref()
+ .is_none_or(|arrival| arrival.node() == node && !fragments.holds_fragment_arrival_of(node, used)),
+ "a box is placed with fragments handed to it for another box, or handed to it twice"
+ );
+ let mut child_run_root = None;
+ let committed_fragment = match fragment_arrival {
+ Some(fragment_tree::ChildFragmentArrival::CommittedFragment {
+ fragment,
+ reuses_subtree,
+ }) => Some((
+ fragment_tree::committed_fragment_showing_box(
+ callbacks,
+ node,
+ used,
+ facts_decided_by_parent.fragment_facts,
+ fragment,
+ ),
+ reuses_subtree,
+ )),
+ Some(fragment_tree::ChildFragmentArrival::RunRoot {
+ root,
+ reuses_committed_subtree,
+ }) => {
+ child_run_root = Some((root, reuses_committed_subtree));
+ None
+ }
+ None => fragments.take_reused_committed_fragment(callbacks, node, used, facts_decided_by_parent.fragment_facts),
+ };
+ if let Some((fragment, reuses_subtree)) = committed_fragment {
fragments.attach_committed_fragment(
node,
fragment.clone(),
@@ -411,7 +465,12 @@ pub(crate) fn place_child_in_containing_block(
exports_payloads: false,
});
}
- fragments.normalize_arrivals_for_placement(node, callbacks);
+ match child_run_root {
+ Some((root, reuses_committed_subtree)) => {
+ fragments.take_in_child_root_at_placement(node, root, reuses_committed_subtree, callbacks);
+ }
+ None => fragments.normalize_arrivals_for_placement(node, callbacks),
+ }
loop {
let batch = fragments.take_drainable_abspos(node, records, callbacks);
if batch.is_empty() {
@@ -3425,8 +3484,13 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layo
input,
None,
);
- hold_fragment_arrival_until_placement(&entry_run, root_for_layout, root_outcome.fragment_arrival);
- place_child(&entry_run, root_for_layout, FfiCssPixelPoint::default(), None);
+ place_laid_out_child(
+ &entry_run,
+ root_for_layout,
+ FfiCssPixelPoint::default(),
+ None,
+ root_outcome.fragment_arrival,
+ );
finish_entry_pass(
entry_records,
&entry_fragments,
@@ -3572,10 +3636,16 @@ fn layout_subtree_with_frozen_root_geometry(run: &FormattingContextRun<'_>) {
input,
None,
);
- hold_fragment_arrival_until_placement(run, root, root_outcome.fragment_arrival);
// The retained offset already includes relative positioning. Publish it directly instead
// of applying placement adjustments a second time.
- fragments.normalize_arrivals_for_placement(root, callbacks);
+ let Some(fragment_tree::ChildFragmentArrival::RunRoot {
+ root: unplaced_root,
+ reuses_committed_subtree,
+ }) = root_outcome.fragment_arrival
+ else {
+ panic!("the root of a partial relayout is laid out by a run of its own");
+ };
+ fragments.take_in_child_root_at_placement(root, unplaced_root, reuses_committed_subtree, callbacks);
let facts_decided_by_parent = run.records.facts_decided_by_parent_of(root);
let placement = facts_decided_by_parent
.placement
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
index a0834bcdfcd..1a088e08845 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
@@ -453,6 +453,28 @@ impl FragmentPayloads {
}
}
+/// The committed fragment `fragment` of `node`, or a fragment of its record around the committed children where the box
+/// is now hidden or shown for line clamping unlike the committed fragment.
+pub(crate) fn committed_fragment_showing_box(
+ callbacks: &LayoutPass<'_>,
+ node: crate::layout::node_data::NodeSlotId,
+ used: &UsedValues,
+ fragment_facts: run_records::FragmentFactsDecidedByParent,
+ fragment: std::sync::Arc<Fragment>,
+) -> std::sync::Arc<Fragment> {
+ if fragment_facts.is_invisible_for_line_clamp == fragment.is_invisible_for_line_clamp {
+ return fragment;
+ }
+ snapshot_fragment(
+ callbacks,
+ node,
+ fragment.children.clone(),
+ used,
+ fragment_facts,
+ Some(Box::new(FragmentPayloads::of_fragment(&fragment))),
+ )
+}
+
fn snapshot_fragment(
callbacks: &LayoutPass<'_>,
node: crate::layout::node_data::NodeSlotId,
@@ -850,6 +872,13 @@ pub(crate) struct HeldCommittedFragment {
pub(crate) struct HeldCommittedFragmentCell(std::cell::Cell<Option<HeldCommittedFragment>>);
impl HeldCommittedFragmentCell {
+ fn holds(&self) -> bool {
+ let held = self.0.take();
+ let holds = held.is_some();
+ self.0.set(held);
+ holds
+ }
+
fn hold(&self, fragment: std::sync::Arc<Fragment>, reuses_subtree: bool) {
self.0.set(Some(HeldCommittedFragment {
fragment,
@@ -1243,19 +1272,47 @@ impl RunFragmentBuilder {
fragment_facts: run_records::FragmentFactsDecidedByParent,
) -> Option<(std::sync::Arc<Fragment>, bool)> {
let held = used.held_committed_fragment.take()?;
- let fragment = if fragment_facts.is_invisible_for_line_clamp == held.fragment.is_invisible_for_line_clamp {
- held.fragment
- } else {
- snapshot_fragment(
- callbacks,
- node,
- held.fragment.children.clone(),
- used,
- fragment_facts,
- Some(Box::new(FragmentPayloads::of_fragment(&held.fragment))),
- )
- };
- Some((fragment, held.reuses_subtree))
+ Some((
+ committed_fragment_showing_box(callbacks, node, used, fragment_facts, held.fragment),
+ held.reuses_subtree,
+ ))
+ }
+
+ /// Whether the run keeps fragments the layout of `node` handed its placement.
+ pub(crate) fn holds_fragment_arrival_of(
+ &self,
+ node: crate::layout::node_data::NodeSlotId,
+ used: &UsedValues,
+ ) -> bool {
+ used.held_committed_fragment.holds()
+ || self
+ .inner
+ .borrow()
+ .child_roots_awaiting_placement
+ .contains_key(&node.slot_index())
+ }
+
+ /// Takes in the unplaced root of the run that laid out `node`, which is being placed, so that its fragment is built
+ /// around the root's descendants.
+ pub(crate) fn take_in_child_root_at_placement(
+ &self,
+ node: crate::layout::node_data::NodeSlotId,
+ root: UnplacedRootFragment,
+ reuses_committed_subtree: bool,
+ callbacks: &LayoutPass<'_>,
+ ) {
+ {
+ let mut inner = self.inner.borrow_mut();
+ inner
+ .reused_subtree_roots
+ .extend(root.reused_subtree_roots.iter().copied());
+ if reuses_committed_subtree {
+ inner.reused_subtree_roots.insert(node.slot_index());
+ } else {
+ inner.reused_subtree_roots.remove(&node.slot_index());
+ }
+ }
+ self.merge_child_root_into_pending_fragment(node, Some(root), callbacks);
}
/// Drops the fragments laid out inside `node`. What the box's own layout handed its fragment stays for the box,
@@ -1312,10 +1369,23 @@ impl RunFragmentBuilder {
&self,
node: crate::layout::node_data::NodeSlotId,
callbacks: &LayoutPass<'_>,
+ ) {
+ let root = self
+ .inner
+ .borrow_mut()
+ .child_roots_awaiting_placement
+ .remove(&node.slot_index());
+ self.merge_child_root_into_pending_fragment(node, root, callbacks);
+ }
+
+ fn merge_child_root_into_pending_fragment(
+ &self,
+ node: crate::layout::node_data::NodeSlotId,
+ root: Option<UnplacedRootFragment>,
+ callbacks: &LayoutPass<'_>,
) {
let mut inner = self.inner.borrow_mut();
let slot = node.slot_index();
- let root = inner.child_roots_awaiting_placement.remove(&slot);
let pending_fragment = inner
.pending_fragments
.entry(slot)
diff --git a/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs
index 28fcaca2562..50725f27023 100644
--- a/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs
@@ -69,14 +69,19 @@ pub(super) fn layout_replaced_with_children(
},
None,
);
- formatting_context::hold_fragment_arrival_until_placement(run, wrapper, fragment_arrival);
formatting_context::propagate_percentage_block_size_dependency_to_containing_block(
run.records,
&run.callbacks,
wrapper,
wrapper_result.depends_on_percentage_block_size,
);
- formatting_context::place_child(run, wrapper, used_values::FfiCssPixelPoint::default(), None);
+ formatting_context::place_laid_out_child(
+ run,
+ wrapper,
+ used_values::FfiCssPixelPoint::default(),
+ None,
+ fragment_arrival,
+ );
formatting_context::ChildLayoutResult {
automatic_content_block_size: wrapper_result.automatic_content_block_size,
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index a78eaeae4d0..6b42c809d8a 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -1870,7 +1870,17 @@ impl<'pass> SvgFormattingContext<'pass> {
}
fn place_child(&self, node: Node, x: CssPixels, y: CssPixels) {
- formatting_context::place_child(&self.run, node, FfiCssPixelPoint { x, y }, None);
+ self.place_laid_out_child(node, x, y, None);
+ }
+
+ fn place_laid_out_child(
+ &self,
+ node: Node,
+ x: CssPixels,
+ y: CssPixels,
+ fragment_arrival: Option<fragment_tree::ChildFragmentArrival>,
+ ) {
+ formatting_context::place_laid_out_child(&self.run, node, FfiCssPixelPoint { x, y }, None, fragment_arrival);
}
/// Gives `node` a content size that is definite in both axes.
@@ -2043,7 +2053,6 @@ impl<'pass> SvgFormattingContext<'pass> {
outcome,
fragment_arrival,
} = formatting_context::layout_inside_child(run, None, None, child, child_input, true);
- formatting_context::hold_fragment_arrival_until_placement(run, child, fragment_arrival);
outcome.report_percentage_block_size_dependency_of(run, child);
match outcome {
ChildLayoutOutcome::Created(_) => {}
@@ -2053,7 +2062,7 @@ impl<'pass> SvgFormattingContext<'pass> {
};
// Masks and clips may use this offset for objectBoundingBox units.
- self.place_child(child, rect.x, rect.y);
+ self.place_laid_out_child(child, rect.x, rect.y, fragment_arrival);
if let Some(mask) = self.first_child_of_kind(child, NodeKind::SVGMaskBox) {
self.layout_mask_or_clip(run, mask);
}
@@ -2157,9 +2166,9 @@ impl<'pass> SvgFormattingContext<'pass> {
if self.svg_attributes(shape).geometry_kind == SVG_GEOMETRY_KIND_LINE {
return false;
}
- let Some(fragments) = self.run.fragments.as_deref() else {
+ if self.run.fragments.is_none() {
return false;
- };
+ }
// A committed fragment stops being current when something marks its box for layout.
let Some(fragment) = self
.run
@@ -2192,8 +2201,15 @@ impl<'pass> SvgFormattingContext<'pass> {
let used = self.used_values(shape);
used.set_box_metrics_from_fragment(&fragment);
used.has_definite_block_size.set(true);
- fragments.hold_committed_fragment_of_empty_box(used, fragment);
- self.place_child(shape, committed_offset.x, committed_offset.y);
+ self.place_laid_out_child(
+ shape,
+ committed_offset.x,
+ committed_offset.y,
+ Some(fragment_tree::ChildFragmentArrival::CommittedFragment {
+ fragment,
+ reuses_subtree: false,
+ }),
+ );
true
}