Truly independent web browser
LibWeb: Return the fragments a child's layout hands its placement
Aliaksandr Kalenik09c8d6ca11d7
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rsindex a97ef7fc8e8..60c305271f9 100644--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs@@ -3108,7 +3108,10 @@ impl<'pass> AbsposEngine<'pass> {assert!(!self.facts(node).is_svg_box());let (available_space, constraints) = out_of_flow_root_space(inputs);- let outcome = formatting_context::layout_inside_child(+ let formatting_context::ChildLayout {+ outcome,+ fragment_arrival,+ } = formatting_context::layout_inside_child(run,None,None,@@ -3120,6 +3123,7 @@ 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) => resultdiff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex ea03a496fa3..f10d0a26d3e 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -1745,7 +1745,10 @@ impl<'pass> BlockFormattingContext<'pass> {inline_size: AvailableSize::definite(max_content_inline_size),block_size: AvailableSize::Indefinite,};- let marker_outcome = formatting_context::layout_inside_child(+ let formatting_context::ChildLayout {+ outcome: marker_outcome,+ fragment_arrival,+ } = formatting_context::layout_inside_child(run,None,None,@@ -1761,6 +1764,7 @@ impl<'pass> BlockFormattingContext<'pass> {},true,);+ formatting_context::hold_fragment_arrival_until_placement(run, marker, fragment_arrival);marker_outcome.report_percentage_block_size_dependency_of(run, marker);match marker_outcome {ChildLayoutOutcome::Created(_) | ChildLayoutOutcome::Skipped { .. } => {}@@ -1806,8 +1810,11 @@ impl<'pass> BlockFormattingContext<'pass> {input: LayoutInput,force_independent_context_run: bool,) -> Option<formatting_context::ChildLayoutResult> {- let outcome =- formatting_context::layout_inside_child(run, Some(self), None, node, input, force_independent_context_run);+ let formatting_context::ChildLayout {+ outcome,+ fragment_arrival,+ } = formatting_context::layout_inside_child(run, Some(self), None, node, input, force_independent_context_run);+ formatting_context::hold_fragment_arrival_until_placement(run, node, fragment_arrival);outcome.report_percentage_block_size_dependency_of(run, node);match outcome {ChildLayoutOutcome::Skipped { .. } => None,diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rsindex 723b49044b1..ca4fd4ab11a 100644--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs@@ -2984,7 +2984,11 @@ impl<'pass> FlexFormattingContext<'pass> {self.item_used(index),self.available_space_for_items.unwrap().space,);- let outcome = formatting_context::layout_inside_child(run, None, None, node, input, false);+ let formatting_context::ChildLayout {+ outcome,+ fragment_arrival,+ } = formatting_context::layout_inside_child(run, None, None, node, input, false);+ formatting_context::hold_fragment_arrival_until_placement(run, node, fragment_arrival);outcome.report_percentage_block_size_dependency_of(run, node);self.flex_items[index].content_baselines = match outcome {ChildLayoutOutcome::Created(result) => result.baselines,diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 26e7df00d6f..53d1eb17c5f 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -124,7 +124,10 @@ pub(crate) fn run_root_without_parent_fragments(let fc_type = independent_formatting_context_type(node, &callbacks);let root_containing_block = callbacks.containing_block_for_child_run(node, &input.participation);let root_used = root_cells.materialize_record();- let result = run_formatting_context(+ let ChildRunOutcome {+ result,+ fragment_arrival,+ } = run_formatting_context(None,&root_used,node,@@ -136,6 +139,10 @@ pub(crate) fn run_root_without_parent_fragments(input,None,);+ debug_assert!(+ fragment_arrival.is_none(),+ "a run without parent fragments hands its placement nothing"+ );RootLaidOutWithoutParentFragments {result,root_cells_left_by_run: used_values::RootInputCells::capture(&root_used),@@ -1851,6 +1858,14 @@ fn body_input_with_inner_available_space(run: &FormattingContextRun, input: &Laybody_input}+/// What a child's run gives its parent: its result, and the fragments its placement takes where the parent builds+/// fragments.+#[must_use]+pub(crate) struct ChildRunOutcome {+ pub(crate) result: ChildLayoutResult,+ pub(crate) fragment_arrival: Option<fragment_tree::ChildFragmentArrival>,+}+#[expect(clippy::too_many_arguments)]pub(super) fn run_formatting_context(parent_fragments: Option<&fragment_tree::RunFragmentBuilder>,@@ -1863,7 +1878,7 @@ pub(super) fn run_formatting_context(callbacks: LayoutPass<'_>,input: LayoutInput,table_inline_layout: Option<table_formatting_context::TableInlineLayout>,-) -> ChildLayoutResult {+) -> ChildRunOutcome {if let Some(parent_fragments) = parent_fragments {// A later run for this root supersedes a hit recorded earlier in the same pass.parent_fragments.clear_reused_subtree_root(box_, parent_used);@@ -1903,7 +1918,7 @@ pub(super) fn run_formatting_context() {Ok(attempt) => attempt,Err(hit) => {- if let Some(result) = reuse_committed_subtree_as_it_is(+ if let Some((result, fragment)) = reuse_committed_subtree_as_it_is(parent_fragments,parent_used,box_,@@ -1912,7 +1927,13 @@ pub(super) fn run_formatting_context(&cache_key,&hit,) {- return result;+ return ChildRunOutcome {+ result,+ fragment_arrival: Some(fragment_tree::ChildFragmentArrival::CommittedFragment {+ fragment,+ reuses_subtree: true,+ }),+ };}let reuses_committed_subtree = hit.reuses_committed_subtree;let action = if !builds_fragments {@@ -2018,8 +2039,8 @@ fn reuse_committed_subtree_as_it_is(callbacks: &LayoutPass<'_>,cache_key: &fc_run_cache::FcRunCacheKey,hit: &fc_run_cache::FcRunCacheHit,-) -> Option<ChildLayoutResult> {- let fragments = parent_fragments?;+) -> Option<(ChildLayoutResult, std::sync::Arc<fragment_tree::Fragment>)> {+ parent_fragments?;let entry = &*hit.entry;if !hit.reuses_committed_subtree|| !cache_key.has_no_table_cell_intrinsic_block_padding()@@ -2047,11 +2068,10 @@ fn reuse_committed_subtree_as_it_is(.arena().layout_trace.run(box_, cache_key.fc_type(), || "REUSE SUBTREE");- fragments.hold_reused_committed_fragment(box_, root_used, fragment);callbacks.scratch().receive_host_findings(entry.outputs.host_findings.clone());- Some(entry.outputs.result)+ Some((entry.outputs.result, fragment))}pub(crate) fn explicit_intrinsic_inner_inline_size(facts: &NodeFacts<'_>) -> CssPixels {@@ -2663,6 +2683,21 @@ pub(crate) fn propagate_percentage_block_size_dependency_to_containing_block(records.note_descendant_depends_on_percentage_block_size(containing_block_or_run_root_that_forwarded_the_basis);}+/// The layout of a child: its outcome, and the fragments its placement takes.+pub(crate) struct ChildLayout {+ pub(crate) outcome: ChildLayoutOutcome,+ pub(crate) fragment_arrival: Option<fragment_tree::ChildFragmentArrival>,+}++impl ChildLayout {+ fn without_fragments(outcome: ChildLayoutOutcome) -> Self {+ Self {+ outcome,+ fragment_arrival: None,+ }+ }+}+pub(crate) fn layout_inside_child(run: &FormattingContextRun,parent_block: Option<&block_formatting_context::BlockFormattingContext>,@@ -2670,7 +2705,7 @@ pub(crate) fn layout_inside_child(child: Node,input: LayoutInput,force_independent_context_run: bool,-) -> ChildLayoutOutcome {+) -> ChildLayout {let facts = NodeFacts::new(&run.callbacks, child);let used = run.records.used_values(child);let child_run_builds_fragments = run_builds_fragments(&input, run.fragments.is_some());@@ -2707,16 +2742,22 @@ pub(crate) fn layout_inside_child();// OPTIMIZATION: A box that took its metrics from its committed fragment is placed with that fragment, where// it shows the box as the record holds it.- if let Some(fragment) = committed_fragment- && let Some(fragments) = run.fragments.as_deref()- && fragment.is_plain_box()- && used.shows_box_of_fragment(&fragment)- && !facts.has_anchor_names()- {- fragments.hold_committed_fragment_of_empty_box(used, fragment);- }+ let fragment_arrival = committed_fragment+ .filter(|fragment| {+ run.fragments.is_some()+ && fragment.is_plain_box()+ && used.shows_box_of_fragment(fragment)+ && !facts.has_anchor_names()+ })+ .map(|fragment| fragment_tree::ChildFragmentArrival::CommittedFragment {+ fragment,+ reuses_subtree: false,+ });store_derived_baselines(used, DerivedBaselines::default());- return skipped();+ return ChildLayout {+ outcome: skipped(),+ fragment_arrival,+ };}let creates_replaced_context = matches!(formatting_context_type_created_by_box(facts),@@ -2724,7 +2765,7 @@ pub(crate) fn layout_inside_child();if !facts.can_have_children() && !creates_replaced_context {size_skipped_independent_root(run, parent_block, child, &input);- return skipped();+ return ChildLayout::without_fragments(skipped());}let Some(ChildRunPlan {@@ -2734,13 +2775,16 @@ pub(crate) fn layout_inside_child(}) = child_run_plan(run, child, input, force_independent_context_run)else {if force_independent_context_run {- return skipped();+ return ChildLayout::without_fragments(skipped());}- return ChildLayoutOutcome::ReenterCurrent;+ return ChildLayout::without_fragments(ChildLayoutOutcome::ReenterCurrent);};input.sizing.flex_self_block_size_resolution_space =dimension_root_in_parent_scope(parent_block, child, &input, true);- let result = run_formatting_context(+ let ChildRunOutcome {+ result,+ fragment_arrival,+ } = run_formatting_context(run.fragments.as_deref(),used,child,@@ -2752,7 +2796,25 @@ pub(crate) fn layout_inside_child(input,run.records.take_table_inline_layout(child),);- ChildLayoutOutcome::Created(result)+ ChildLayout {+ outcome: ChildLayoutOutcome::Created(result),+ fragment_arrival,+ }+}++/// Keeps what the layout of the child `node` handed its placement in the run's fragment builder until the placement+/// takes it.+pub(crate) fn hold_fragment_arrival_until_placement(+ run: &FormattingContextRun,+ node: Node,+ 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")+ .hold_child_fragment_arrival(node, run.records.used_values(node), arrival);+ }}/// The run a table cell's committing run will be (see layout_inside_child), but for the intrinsic block padding that@@ -3062,7 +3124,7 @@ fn absorb_run_outputs(input: &LayoutInput,outputs: RunOutputs,reuses_committed_subtree: bool,-) -> ChildLayoutResult {+) -> ChildRunOutcome {let RunOutputs {result,root,@@ -3075,14 +3137,17 @@ fn absorb_run_outputs(parent_used,content_block_size_adopted_after_run(input, result.automatic_content_block_size),);- if let (Some(fragments), Some(root)) = (parent_fragments, root) {+ let fragment_arrival = parent_fragments.and(root).map(|root| {debug_assert!(root.node == child, "a child run returned a root for a different box");- if reuses_committed_subtree {- fragments.note_reused_subtree_root(child);+ fragment_tree::ChildFragmentArrival::RunRoot {+ root,+ reuses_committed_subtree,}- fragments.hold_unplaced_root(root);+ });+ ChildRunOutcome {+ result,+ fragment_arrival,}- result}pub(super) fn independent_formatting_context_type(box_: Node, callbacks: &LayoutPass<'_>) -> FormattingContextType {@@ -3348,7 +3413,7 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layo).with_forced_sizes(viewport_inline_size, viewport_block_size);let fc_type = independent_formatting_context_type(root_for_layout, &callbacks);- run_formatting_context(+ let root_outcome = run_formatting_context(Some(&entry_fragments),root_for_layout_used,root_for_layout,@@ -3360,6 +3425,7 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layoinput,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);finish_entry_pass(entry_records,@@ -3494,7 +3560,7 @@ fn layout_subtree_with_frozen_root_geometry(run: &FormattingContextRun<'_>) {debug_assert!(facts.is_svg_svg_box());let fc_type = formatting_context_type_created_by_box(facts).expect("partial relayout root must establish an independent formatting context");- run_formatting_context(+ let root_outcome = run_formatting_context(Some(fragments),root_used,root,@@ -3506,6 +3572,7 @@ 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);diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rsindex 9d0765dc5e8..a0834bcdfcd 100644--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs@@ -623,6 +623,29 @@ impl UnplacedRootFragment {}}+/// What the layout of a child hands the child's placement: the unplaced root fragment of the child's run, or a+/// committed fragment the child is placed with as it is.+#[must_use = "a child's fragments are lost unless its placement takes them"]+pub(crate) enum ChildFragmentArrival {+ RunRoot {+ root: UnplacedRootFragment,+ reuses_committed_subtree: bool,+ },+ CommittedFragment {+ fragment: std::sync::Arc<Fragment>,+ reuses_subtree: bool,+ },+}++impl ChildFragmentArrival {+ pub(crate) fn node(&self) -> crate::layout::node_data::NodeSlotId {+ match self {+ Self::RunRoot { root, .. } => root.node,+ Self::CommittedFragment { fragment, .. } => fragment.node,+ }+ }+}+#[derive(Clone, Copy)]pub(crate) struct SlotFragment<'tree> {pub(crate) link: &'tree FragmentLink,@@ -1099,7 +1122,39 @@ impl RunFragmentBuilder {taken}- pub(crate) fn hold_unplaced_root(&self, root: UnplacedRootFragment) {+ /// Keeps what the layout of the child `node` handed its placement until the placement takes it.+ pub(crate) fn hold_child_fragment_arrival(+ &self,+ node: crate::layout::node_data::NodeSlotId,+ used: &UsedValues,+ arrival: ChildFragmentArrival,+ ) {+ debug_assert!(+ arrival.node() == node,+ "a child's fragments arrived for a different box"+ );+ match arrival {+ ChildFragmentArrival::RunRoot {+ root,+ reuses_committed_subtree,+ } => {+ if reuses_committed_subtree {+ self.note_reused_subtree_root(node);+ }+ self.hold_unplaced_root(root);+ }+ ChildFragmentArrival::CommittedFragment {+ fragment,+ reuses_subtree: true,+ } => self.hold_reused_committed_fragment(node, used, fragment),+ ChildFragmentArrival::CommittedFragment {+ fragment,+ reuses_subtree: false,+ } => self.hold_committed_fragment_of_empty_box(used, fragment),+ }+ }++ fn hold_unplaced_root(&self, root: UnplacedRootFragment) {let slot = root.node.slot_index();let mut inner = self.inner.borrow_mut();innerdiff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rsindex 9003fe93072..89f0b298dd7 100644--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs@@ -4380,7 +4380,11 @@ impl<'pass> GridFormattingContext<'pass> {// grid area in both axes for the table's percentage resolution.input.containing_block_constraints.percentage_basis_inline_size = Some(inline_basis);}- let outcome = formatting_context::layout_inside_child(run, None, Some(self), item.box_, input, false);+ let formatting_context::ChildLayout {+ outcome,+ fragment_arrival,+ } = formatting_context::layout_inside_child(run, None, Some(self), item.box_, input, false);+ formatting_context::hold_fragment_arrival_until_placement(run, item.box_, fragment_arrival);outcome.report_percentage_block_size_dependency_of(run, item.box_);match outcome {ChildLayoutOutcome::Created(_) | ChildLayoutOutcome::Skipped { .. } => {}diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 879cb742ac7..9b696948df1 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -1283,7 +1283,11 @@ impl<'context> InlineFormattingContext<'context> {self.input.containing_block_constraints,ParticipationInParentFormattingContext::AtomicInline,);- let outcome = formatting_context::layout_inside_child(self.run, Some(self.parent), None, node, input, false);+ let formatting_context::ChildLayout {+ outcome,+ fragment_arrival,+ } = formatting_context::layout_inside_child(self.run, Some(self.parent), None, node, input, false);+ formatting_context::hold_fragment_arrival_until_placement(self.run, node, fragment_arrival);outcome.report_percentage_block_size_dependency_of(self.run, node);match outcome {ChildLayoutOutcome::Created(result) => result.baselines,diff --git a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rsindex 862c08b79a9..5f5ea1917eb 100644--- a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs@@ -2135,7 +2135,11 @@ impl<'pass> MathFormattingContext<'pass> {);input.sizing.math_stretch_size_constraint = stretch_size_constraint;let used = run.records.used_values(child);- let outcome = formatting_context::layout_inside_child(run, None, None, child, input, true);+ let formatting_context::ChildLayout {+ outcome,+ fragment_arrival,+ } = formatting_context::layout_inside_child(run, None, None, child, input, true);+ formatting_context::hold_fragment_arrival_until_placement(run, child, fragment_arrival);outcome.report_percentage_block_size_dependency_of(run, child);let baselines = match outcome {ChildLayoutOutcome::Created(result) => result.baselines,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.rsindex f3305c4acb8..28fcaca2562 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@@ -46,7 +46,10 @@ pub(super) fn layout_replaced_with_children(.create_used_values(&run.callbacks, wrapper, wrapper_constraints);wrapper_state.set_content_inline_size(content_inline_size);- let wrapper_result = formatting_context::run_formatting_context(+ let formatting_context::ChildRunOutcome {+ result: wrapper_result,+ fragment_arrival,+ } = formatting_context::run_formatting_context(run.fragments.as_deref(),wrapper_state,wrapper,@@ -66,6 +69,7 @@ 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,diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex 7a69013d6f3..a78eaeae4d0 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -2039,7 +2039,11 @@ impl<'pass> SvgFormattingContext<'pass> {},..self.nested_layout_input()};- let outcome = formatting_context::layout_inside_child(run, None, None, child, child_input, true);+ let formatting_context::ChildLayout {+ 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(_) => {}diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rsindex dd7c8251f57..1f8f719607c 100644--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs@@ -2582,7 +2582,11 @@ impl<'pass> TableFormattingContext<'pass> {},participation: ParticipationInParentFormattingContext::Item,};- let outcome = formatting_context::layout_inside_child(run, None, None, cell.box_, layout_input, false);+ let formatting_context::ChildLayout {+ outcome,+ fragment_arrival,+ } = formatting_context::layout_inside_child(run, None, None, cell.box_, layout_input, false);+ formatting_context::hold_fragment_arrival_until_placement(run, cell.box_, fragment_arrival);outcome.report_percentage_block_size_dependency_of(run, cell.box_);match outcome {ChildLayoutOutcome::Created(result) => result.baselines,