Skip to content

Truly independent web browser

LibWeb: Hand foreignObject children to their runs without a record

Aliaksandr Kalenik3feabbb554ef
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 54dee35aa41..fdf4e335e7b 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -2867,7 +2867,7 @@ pub(crate) fn layout_inside_child(
input: LayoutInput,
force_independent_context_run: bool,
) -> ChildLayout {
- let child_input_cells = used_values::RootInputCells::capture(run.records.used_values(child));
+ let child_input_cells = run.records.box_metrics_of(child).input_cells();
layout_inside_child_with_input_cells(
run,
parent_block,
@@ -2891,7 +2891,6 @@ pub(crate) fn layout_inside_child_with_input_cells(
force_independent_context_run: bool,
) -> 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());
if let Some((padding_top, padding_bottom)) = input.sizing.table_cell_intrinsic_block_padding {
debug_assert!(facts.is_table_cell());
@@ -2902,7 +2901,14 @@ pub(crate) fn layout_inside_child_with_input_cells(
child_input_cells.padding_top += padding_top;
child_input_cells.padding_bottom += padding_bottom;
}
- child_input_cells.apply_to_record(used);
+ let record_kept_by_parent = (!run.records.keeps_child_without_record(child)).then(|| {
+ let used = run.records.used_values(child);
+ child_input_cells.apply_to_record(used);
+ used
+ });
+ // NB: A child kept without a record that this run lays out itself gets a record of the cells it was handed.
+ let record_for_this_run =
+ || record_kept_by_parent.unwrap_or_else(|| run.records.lay_out_child_in_this_run(child, child_input_cells));
let skipped = || ChildLayoutOutcome::Skipped {
depends_on_percentage_block_size: run.sizing().skipped_child_depends_on_percentage_block_size(child),
};
@@ -2917,6 +2923,7 @@ pub(crate) fn layout_inside_child_with_input_cells(
{
// OPTIMIZATION: An empty atomic block has no formatting-context body output. Size it in the
// parent run, which will also construct its fragment when placing the box.
+ let used = record_for_this_run();
let committed_fragment = run.sizing().dimension_empty_atomic_root(
child,
input.available_space,
@@ -2947,6 +2954,7 @@ pub(crate) fn layout_inside_child_with_input_cells(
Some(FormattingContextType::InternalReplaced | FormattingContextType::ReplacedWithChildren)
);
if !facts.can_have_children() && !creates_replaced_context {
+ record_for_this_run();
size_skipped_independent_root(run, parent_block, child, &input);
return ChildLayout::without_fragments(skipped());
}
@@ -2957,6 +2965,7 @@ pub(crate) fn layout_inside_child_with_input_cells(
input,
}) = child_run_plan(run, child, input, force_independent_context_run)
else {
+ record_for_this_run();
if force_independent_context_run {
return ChildLayout::without_fragments(skipped());
}
@@ -2964,8 +2973,8 @@ pub(crate) fn layout_inside_child_with_input_cells(
};
let (result, fragment_arrival) = run_formatting_context(
run.fragments.as_deref(),
- Some(used),
- used_values::RootInputCells::capture(used),
+ record_kept_by_parent,
+ child_input_cells,
child,
root_containing_block,
parent_grid,
diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rs
index 5379904d0d7..3f365a94dd5 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs
@@ -697,6 +697,46 @@ impl<'arena> RunRecords<'arena> {
);
}
+ /// Whether this run keeps `child` without a record, for runs of its own: before the first of them returns, or as
+ /// the last one handed it back.
+ pub(crate) fn keeps_child_without_record(&self, child: Node) -> bool {
+ let Some(HeldBox::Kept(index)) = self.held_box(child) else {
+ return false;
+ };
+ !matches!(
+ self.scratch.kept_boxes.borrow()[index as usize].holding,
+ KeptBoxHolding::Record { .. }
+ )
+ }
+
+ /// Gives `child`, a box this run keeps without a record, a record with the cells `cells`, for this run to lay the
+ /// box out itself.
+ #[track_caller]
+ pub(crate) fn lay_out_child_in_this_run(&self, child: Node, cells: used_values::RootInputCells) -> &UsedValues {
+ assert_eq!(
+ self.scratch.innermost_run_nonce(),
+ Some(self.nonce),
+ "only the innermost layout run may register records"
+ );
+ let Some(HeldBox::Kept(index)) = self.held_box(child) else {
+ self.panic_for_box_not_held(child)
+ };
+ let (record_index, record) = self.scratch.run_record_stack.push(cells.materialize_record());
+ let holding = &mut self.scratch.kept_boxes.borrow_mut()[index as usize].holding;
+ assert!(
+ !matches!(holding, KeptBoxHolding::Record { .. }),
+ "slot {} already has a record in the run rooted at slot {}",
+ child.slot_index(),
+ self.root.slot_index()
+ );
+ *holding = KeptBoxHolding::Record {
+ record: record_index,
+ handed_back_by_its_run: None,
+ };
+ // SAFETY: The record stays on the stack until this run returns, which ends this borrow of the run.
+ unsafe { record.as_ref() }
+ }
+
fn assert_registers_in_innermost_run(&self, node: Node) {
if node == self.root && self.root_used.is_some() {
self.panic_for_box_registered_twice(node);
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index 716112bb4c8..5d03c194d9c 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -2030,7 +2030,12 @@ impl<'pass> SvgFormattingContext<'pass> {
if self.svg_attributes(child).is_fit_to_view_box {
self.layout_nested_viewport(run, child);
} else if kind == NodeKind::SVGForeignObjectBox {
- let mut child_cells = used_values::RootInputCells::capture(self.create_used_values(child));
+ // SVG descendants carry no percentage basis, as `create_used_values` explains.
+ let mut child_cells = self.run.records.keep_child_for_its_run(
+ &self.run.callbacks,
+ child,
+ ContainingBlockConstraints::default(),
+ );
let style = self.style(child);
let rect = SvgCssPixelRect {
x: style.x().to_px(self.viewport_width),