Skip to content

Truly independent web browser

LibWeb: Read abspos containing blocks through a frame projection

Aliaksandr Kalenik3275445c0831
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 0b288e01dbf..70ba245cc7a 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -150,7 +150,10 @@ impl<'pass> AbsposEngine<'pass> {
entry: &abspos_inputs::PendingAbsposChild,
) -> ContainingBlockGeometry {
let containing_block = entry.containing_block();
- let Some(containing_block_metrics) = self.records.box_metrics_of_if_owned(containing_block) else {
+ let Some(containing_block_cells) = self
+ .records
+ .box_cells_in_frame_if_owned(containing_block, self.callbacks.frame)
+ else {
panic!(
"the run holds no containing block for slot {} (cb slot {})",
entry.child_box.slot_index(),
@@ -159,7 +162,7 @@ impl<'pass> AbsposEngine<'pass> {
};
let content_origin_in_entry_space =
self.origin_of_containing_block_in_entry_space(containing_block, entry.coordinate_space_box);
- ContainingBlockGeometry::from_box_metrics(&containing_block_metrics, content_origin_in_entry_space)
+ ContainingBlockGeometry::from_box_metrics(&containing_block_cells, content_origin_in_entry_space)
}
fn origin_of_containing_block_in_entry_space(&self, containing_block: Node, entry_space: Node) -> FfiCssPixelPoint {
@@ -3463,7 +3466,7 @@ impl<'pass> AbsposEngine<'pass> {
ParticipationInParentFormattingContext::AbsolutelyPositioned(inputs),
),
);
- let used = laid_out.root_cells_left_by_run.materialize_record();
+ let used = laid_out.root_cells_left_by_run.materialize_record(self.callbacks.frame);
let used_insets = laid_out
.result
.out_of_flow_root_used_insets
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 3c76ca25b25..f9f99b3d9c3 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -609,7 +609,9 @@ impl<'pass> BlockFormattingContext<'pass> {
.borrow_mut()
.remove(&node);
if let Some(cells) = cells {
- self.run.records.lay_out_child_in_this_run(node, cells);
+ self.run
+ .records
+ .lay_out_child_in_this_run(node, cells, self.run.callbacks.frame);
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index fa7ef9f307c..c254e5e2dd1 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -327,34 +327,16 @@ pub(crate) fn lay_out_pending_abspos_children(
let engine = abspos_engine::AbsposEngine::for_run(run);
for entry in batch {
let containing_block = entry.containing_block();
- if frame_established_by(&run.callbacks, containing_block) == run.callbacks.frame {
+ let containing_block_frame = frame_established_by(&run.callbacks, containing_block);
+ if containing_block_frame == run.callbacks.frame {
engine.layout_pending_child(run, entry);
continue;
}
- in_frame_established_by(run, containing_block, |run| {
- abspos_engine::AbsposEngine::for_run(run).layout_pending_child(run, entry);
- });
+ let framed_run = run.in_frame_of(containing_block_frame);
+ abspos_engine::AbsposEngine::for_run(&framed_run).layout_pending_child(&framed_run, entry);
}
}
-/// Runs `body` with a run in the frame `node` establishes, with the box metrics of `node` moved into that frame.
-pub(crate) fn in_frame_established_by<R>(
- run: &FormattingContextRun,
- node: Node,
- body: impl FnOnce(&FormattingContextRun) -> R,
-) -> R {
- let parent_frame = run.callbacks.frame;
- let own_frame = frame_established_by(&run.callbacks, node);
- if own_frame == parent_frame {
- return body(run);
- }
- run.records.move_box_metrics_to_frame(node, parent_frame, own_frame);
- let framed_run = run.in_frame_of(own_frame);
- let result = body(&framed_run);
- run.records.move_box_metrics_to_frame(node, own_frame, parent_frame);
- result
-}
-
pub(crate) fn place_child(
run: &FormattingContextRun,
node: Node,
@@ -2853,8 +2835,12 @@ pub(crate) fn layout_inside_child_with_input_cells(
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 record_for_this_run = || {
+ record_kept_by_parent.unwrap_or_else(|| {
+ run.records
+ .lay_out_child_in_this_run(child, child_input_cells, run.callbacks.frame)
+ })
+ };
let skipped = || ChildLayoutOutcome::Skipped {
depends_on_percentage_block_size: run.sizing().skipped_child_depends_on_percentage_block_size(child),
};
diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rs
index e5147c0612e..46407d71195 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs
@@ -609,7 +609,7 @@ impl<'arena> RunRecords<'arena> {
let scratch = callbacks.scratch();
let records = Self {
root,
- root_used: root_cells.map(|cells| cells.materialize_record()),
+ root_used: root_cells.map(|cells| cells.materialize_record(callbacks.frame)),
scratch,
nonce: scratch.begin_run(),
stack_length_at_start: scratch.run_record_stack.length(),
@@ -697,7 +697,12 @@ impl<'arena> RunRecords<'arena> {
/// 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 {
+ pub(crate) fn lay_out_child_in_this_run(
+ &self,
+ child: Node,
+ cells: used_values::RootInputCells,
+ frame: geometry::Frame,
+ ) -> &UsedValues {
assert_eq!(
self.scratch.innermost_run_nonce(),
Some(self.nonce),
@@ -706,7 +711,7 @@ impl<'arena> RunRecords<'arena> {
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 (record_index, record) = self.scratch.run_record_stack.push(cells.materialize_record(frame));
let holding = &mut self.scratch.kept_boxes.borrow_mut()[index as usize].holding;
assert!(
!matches!(holding, KeptBoxHolding::LaidOutByThisRun { .. }),
@@ -1096,42 +1101,35 @@ impl<'arena> RunRecords<'arena> {
}))
}
- /// Moves the sizes, size constraints and box sides this run holds for `node`, if it holds the box, from frame `from`
- /// to frame `to`.
- pub(crate) fn move_box_metrics_to_frame(&self, node: Node, from: geometry::Frame, to: geometry::Frame) {
- let index = match self.held_box(node) {
- None => return,
- Some(HeldBox::RootWithRecord) => {
- if let Some(root_used) = &self.root_used {
- root_used.move_box_metrics_to_frame(from, to);
- }
- return;
- }
- Some(HeldBox::Kept(index)) => index,
+ /// The sizes, size constraints and box sides this run holds for `node`, if it holds the box, read in frame `reader`.
+ pub(crate) fn box_cells_in_frame_if_owned(
+ &self,
+ node: Node,
+ reader: geometry::Frame,
+ ) -> Option<used_values::RootInputCells> {
+ let cells_of_record =
+ |record: &UsedValues| used_values::RootInputCells::capture(record).in_frame(record.frame.get(), reader);
+ let index = match self.held_box(node)? {
+ HeldBox::RootWithRecord => return self.root_used.as_ref().map(cells_of_record),
+ HeldBox::Kept(index) => index,
};
- match &mut self.scratch.kept_boxes.borrow_mut()[index as usize].holding {
- KeptBoxHolding::LaidOutByThisRun { record } => {
- // SAFETY: This run registered the record, and it stays on the stack until this run returns.
- unsafe { self.scratch.run_record_stack.get(*record).as_ref() }.move_box_metrics_to_frame(from, to);
- }
- KeptBoxHolding::ChildAwaitingItsRun { .. } => {
+ let record = match &self.scratch.kept_boxes.borrow()[index as usize].holding {
+ KeptBoxHolding::LaidOutByThisRun { record } => *record,
+ KeptBoxHolding::ChildAwaitingItsRun { cells_before_its_run } => {
debug_assert!(
false,
- "a child's box moves to another frame only once its run handed it back"
+ "a child's box is read in another frame only once its run handed it back"
);
+ return Some(*cells_before_its_run);
}
- KeptBoxHolding::ChildHandedBack(ChildLaidOutByItsOwnRun { laid_out, frame, .. }) => {
- debug_assert_eq!(*frame, from, "a kept box moves out of the frame it is in");
- // NB: The baselines stay those of the parent's frame, which every reader of them is in.
- laid_out.cells = used_values::UsedValuesCellState {
- inputs: laid_out.cells,
- baselines: laid_out.baselines,
- }
- .in_frame(from, to)
- .inputs;
- *frame = to;
+ KeptBoxHolding::ChildHandedBack(handed_back) => {
+ return Some(handed_back.laid_out.cells.in_frame(handed_back.frame, reader));
}
- }
+ };
+ // SAFETY: This run registered the record, and it stays on the stack until this run returns.
+ Some(cells_of_record(unsafe {
+ self.scratch.run_record_stack.get(record).as_ref()
+ }))
}
/// The last line laid out in `node`, a box this run holds: by this run, or by the run of a child that handed it back.
diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rs
index 3048dbf4a02..eb57db02240 100644
--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs
+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs
@@ -219,6 +219,8 @@ pub(crate) struct UsedValues {
pub inline_alignment_baseline: Cell<Option<CssPixels>>,
pub(crate) line_data: RefCell<LineDataState>,
+ /// The frame the box's sizes, size constraints and box sides are held in.
+ pub(crate) frame: Cell<geometry::Frame>,
/// The anchor-size() resolutions of an absolutely positioned box, set once as the abspos engine places it.
pub(crate) anchor_resolved_values: std::cell::OnceCell<Box<style_values::AnchorResolvedValues>>,
}
@@ -252,6 +254,7 @@ impl Default for UsedValues {
last_baseline: Cell::new(zero),
inline_alignment_baseline: Cell::new(None),
line_data: RefCell::new(LineDataState::Empty),
+ frame: Cell::new(geometry::Frame::PHYSICAL),
anchor_resolved_values: std::cell::OnceCell::new(),
}
}
@@ -340,6 +343,7 @@ impl UsedValues {
if from == to {
return;
}
+ self.frame.set(to);
let state = UsedValuesCellState::capture(self).in_frame(from, to);
macro_rules! move_sealable {
($($field:ident),+) => {
@@ -473,8 +477,10 @@ impl RootInputCells {
self.content_block_size = clamp_to_max_dimension_value(value.max(CssPixels::default()));
}
- pub(crate) fn materialize_record(&self) -> UsedValues {
+ /// A record holding the cells, which hold the box's metrics in `frame`.
+ pub(crate) fn materialize_record(&self, frame: geometry::Frame) -> UsedValues {
let record = UsedValues::default();
+ record.frame.set(frame);
self.apply_to_record(&record);
record
}
@@ -837,6 +843,7 @@ pub(crate) fn create_used_values(
// they are assigned through set_content_inline_size() or
// set_content_block_size().
let used = UsedValues::default();
+ used.frame.set(callbacks.frame);
#[derive(Clone, Copy)]
enum Axis {
@@ -1004,6 +1011,7 @@ pub(crate) fn committed_box_from_fragment_link(callbacks: &LayoutPass<'_>, node:
// percentage bases, and every resulting geometry field is replaced by
// the previously committed value immediately.
let used = UsedValues::default();
+ used.frame.set(link.frame);
used.set_box_metrics_from_fragment(fragment);
used.has_definite_inline_size.set(true);
used.has_definite_block_size.set(true);