Truly independent web browser
LibWeb: Return the insets compute_inset resolves instead of storing them
Aliaksandr Kalenikd2716409253f
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rsindex cbcb607b17b..430f583fbd9 100644--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs@@ -399,7 +399,7 @@ struct AnchorCalcCallbackContext<'pass> {engine: *const AbsposEngine<'pass>,positioned_box: Node,containing_block: Node,- containing_block_geometry: Option<ContainingBlockGeometry>,+ containing_block_geometry: ContainingBlockGeometry,entry_coordinate_space_box: Node,is_from_end: bool,is_horizontal_axis: bool,@@ -519,8 +519,7 @@ impl AbsposEngine<'_> {fn anchor_rect(&self,anchor_box: Node,- containing_block: Node,- entry_containing_block_geometry: Option<&ContainingBlockGeometry>,+ entry_containing_block_geometry: &ContainingBlockGeometry,entry_coordinate_space_box: Node,) -> CssPixelRect {let (rect, coordinate_space_box) = self@@ -528,28 +527,15 @@ impl AbsposEngine<'_> {.as_deref().and_then(|fragments| fragments.find_anchor_candidate(anchor_box)).expect("an accepted anchor is placed in the draining subtree and propagated here");- match entry_containing_block_geometry {- Some(geometry) => {- let fold_into_entry_space =- self.translation_between_payload_resting_spaces(coordinate_space_box, entry_coordinate_space_box);- CssPixelRect {- x: rect.x + fold_into_entry_space.x - geometry.content_origin_in_entry_space.x- + geometry.padding_left,- y: rect.y + fold_into_entry_space.y - geometry.content_origin_in_entry_space.y- + geometry.padding_top,- width: rect.width,- height: rect.height,- }- }- None => {- let containing_block_used = self.used(containing_block);- CssPixelRect {- x: rect.x + containing_block_used.padding_left(),- y: rect.y + containing_block_used.padding_top(),- width: rect.width,- height: rect.height,- }- }+ let fold_into_entry_space =+ self.translation_between_payload_resting_spaces(coordinate_space_box, entry_coordinate_space_box);+ CssPixelRect {+ x: rect.x + fold_into_entry_space.x - entry_containing_block_geometry.content_origin_in_entry_space.x+ + entry_containing_block_geometry.padding_left,+ y: rect.y + fold_into_entry_space.y - entry_containing_block_geometry.content_origin_in_entry_space.y+ + entry_containing_block_geometry.padding_top,+ width: rect.width,+ height: rect.height,}}@@ -693,7 +679,7 @@ impl AbsposEngine<'_> {calculated: *const c_void,positioned_box: Node,containing_block: Node,- containing_block_geometry: Option<ContainingBlockGeometry>,+ containing_block_geometry: ContainingBlockGeometry,entry_coordinate_space_box: Node,axis: AnchorValueAxis,resolution_state: &mut AnchorResolutionState,@@ -726,7 +712,7 @@ impl AbsposEngine<'_> {&self,node: Node,containing_block: Node,- entry_containing_block_geometry: Option<&ContainingBlockGeometry>,+ entry_containing_block_geometry: &ContainingBlockGeometry,entry_coordinate_space_box: Node,position_area_anchor_box: Option<Node>,) -> (@@ -777,7 +763,7 @@ impl AbsposEngine<'_> {&self,node: Node,containing_block: Node,- entry_containing_block_geometry: Option<&ContainingBlockGeometry>,+ entry_containing_block_geometry: &ContainingBlockGeometry,entry_coordinate_space_box: Node,) -> (Option<formatting_context::ResolvedAnchorInsets>, AnchorResolutionState) {let style = self.style(node);@@ -796,12 +782,7 @@ impl AbsposEngine<'_> {if containing_block.is_invalid() {return (None, AnchorResolutionState::NO_SCROLL_SHIFT);}- let containing_block_geometry = match entry_containing_block_geometry {- Some(geometry) => *geometry,- None => {- ContainingBlockGeometry::from_box_metrics(&self.used(containing_block), FfiCssPixelPoint::default())- }- };+ let containing_block_geometry = *entry_containing_block_geometry;let default_anchor_box = if style.has_position_anchor() {self.anchor_lookup(node, style.position_anchor_name()).unwrap_or(NodeSlotId::INVALID)@@ -820,7 +801,7 @@ impl AbsposEngine<'_> {inset.anchor_bearing_calculated(),node,containing_block,- entry_containing_block_geometry.copied(),+ containing_block_geometry,entry_coordinate_space_box,axis,&mut resolution_state,@@ -894,7 +875,7 @@ impl AbsposEngine<'_> {calculated,node,containing_block,- Some(*containing_block_geometry),+ *containing_block_geometry,entry_coordinate_space_box,AnchorValueAxis {is_from_end: false,@@ -1005,7 +986,6 @@ impl AbsposEngine<'_> {fn apply_anchor_center_alignment(&self,positioned_box: Node,- containing_block: Node,containing_block_geometry: &ContainingBlockGeometry,entry_coordinate_space_box: Node,resolved_anchor_insets: Option<&formatting_context::ResolvedAnchorInsets>,@@ -1030,12 +1010,7 @@ impl AbsposEngine<'_> {let Some(anchor_box) = self.anchor_lookup(positioned_box, style.position_anchor_name()) else {return;};- let anchor_rect = self.anchor_rect(- anchor_box,- containing_block,- Some(containing_block_geometry),- entry_coordinate_space_box,- );+ let anchor_rect = self.anchor_rect(anchor_box, containing_block_geometry, entry_coordinate_space_box);// NB: The static position rect and containing block rect are relative to the containing block's content box,// while anchor_rect() is relative to its padding box.if centers_inline {@@ -1136,12 +1111,7 @@ impl AbsposEngine<'_> {.in_frame(self.callbacks.frame);let anchor_box = self.anchor_lookup(positioned_box, style.position_anchor_name())?;- let anchor_rect = self.anchor_rect(- anchor_box,- containing_block,- Some(containing_block_geometry),- entry_coordinate_space_box,- );+ let anchor_rect = self.anchor_rect(anchor_box, containing_block_geometry, entry_coordinate_space_box);// anchor_rect() is padding-box-relative, while containing_block_info.rect is content-box-relative. Normalize// them before comparing their edges.@@ -1599,8 +1569,7 @@ unsafe extern "C" fn resolve_anchor_non_math_function(context: *mut c_void, shel{let rect = engine.anchor_rect(anchor_box,- context.containing_block,- context.containing_block_geometry.as_ref(),+ &context.containing_block_geometry,context.entry_coordinate_space_box,);// SAFETY: As above.@@ -3618,7 +3587,7 @@ impl<'pass> AbsposEngine<'pass> {let (resolved, default_scroll_shift) = self.resolve_anchor_insets(child_box,child.containing_block(),- Some(&position_area_geometry),+ &position_area_geometry,child.coordinate_space_box,Some(anchor_box),);@@ -3639,7 +3608,7 @@ impl<'pass> AbsposEngine<'pass> {let (resolved, default_scroll_shift) = self.resolve_anchor_insets(child_box,child.containing_block(),- Some(&containing_block_geometry),+ &containing_block_geometry,child.coordinate_space_box,None,);@@ -3653,7 +3622,6 @@ impl<'pass> AbsposEngine<'pass> {});self.apply_anchor_center_alignment(child_box,- child.containing_block(),&containing_block_geometry,child.coordinate_space_box,resolved.as_ref(),@@ -3732,10 +3700,11 @@ impl<'pass> AbsposEngine<'pass> {fn compute_inset(&self,node: Node,- containing_block_size: geometry::LogicalSize,+ containing_block: &impl BoxMetricCells,+ percentage_basis: geometry::LogicalSize,formatting_context_root: Node,treat_block_axis_percentage_insets_as_auto_beyond_root: bool,- ) {+ ) -> InFlowBoxInsets {// Most boxes are neither relatively positioned nor carry anchor()// insets, and both facts come straight from the node's style payload.let initial_style = self.style(node);@@ -3744,27 +3713,31 @@ impl<'pass> AbsposEngine<'pass> {|| initial_style.inset_bottom().contains_anchor_function()|| initial_style.inset_left().contains_anchor_function();if !has_anchor_insets && initial_style.position() != positioning::RELATIVE {- return;+ return InFlowBoxInsets::default();}+ let mut insets = InFlowBoxInsets::default();// Anchor resolution also refreshes the box's default scroll shift, so// an anchor-bearing box resolves its insets even when it turns out not// to be relatively positioned.let resolved = if has_anchor_insets {+ let in_flow_containing_block = self.callbacks.in_flow_containing_block(node);+ let containing_block_geometry =+ ContainingBlockGeometry::from_box_metrics(containing_block, FfiCssPixelPoint::default());let (resolved, default_scroll_shift) = self.resolve_anchor_insets(node,- self.callbacks.in_flow_containing_block(node),- None,+ in_flow_containing_block,+ &containing_block_geometry,NodeSlotId::INVALID,None,);- self.records.set_default_scroll_shift(node, Some(default_scroll_shift));+ insets.default_scroll_shift = Some(default_scroll_shift);resolved} else {None};let style = initial_style.with_resolved_insets(resolved.as_ref());if style.position() != positioning::RELATIVE {- return;+ return insets;}formatting_context::propagate_percentage_block_size_dependency_to_containing_block(self.records,@@ -3784,11 +3757,7 @@ impl<'pass> AbsposEngine<'pass> {(resolved_first, -resolved_first)}};- let (left, right) = resolve_opposing(- style.inset_left(),- style.inset_right(),- containing_block_size.inline_size,- );+ let (left, right) = resolve_opposing(style.inset_left(), style.inset_right(), percentage_basis.inline_size);let treat_block_axis_percentage_insets_as_auto = (style.inset_top().contains_percentage()|| style.inset_bottom().contains_percentage())@@ -3812,17 +3781,15 @@ impl<'pass> AbsposEngine<'pass> {let (top, bottom) = resolve_opposing(block_axis_inset_value(style.inset_top(), treat_block_axis_percentage_insets_as_auto),block_axis_inset_value(style.inset_bottom(), treat_block_axis_percentage_insets_as_auto),- containing_block_size.block_size,- );- self.records.set_used_insets(- node,- run_records::UsedInsets {- left,- right,- top,- bottom,- },+ percentage_basis.block_size,);+ insets.used_insets = Some(run_records::UsedInsets {+ left,+ right,+ top,+ bottom,+ });+ insets}}@@ -3851,21 +3818,29 @@ pub(crate) fn drain_abspos_with_placed_containing_blocks(}}+/// What the insets of an in-flow box resolve to: the used insets of a relatively positioned box, and the scroll shift+/// its anchor() insets compensate for.+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]+pub(crate) struct InFlowBoxInsets {+ pub(crate) used_insets: Option<run_records::UsedInsets>,+ pub(crate) default_scroll_shift: Option<abspos_inputs::DefaultScrollShift>,+}++/// Resolves the insets of `node`, an in-flow box whose containing block has the metrics `containing_block`, with+/// percentages against `percentage_basis`.pub(crate) fn compute_inset_native(run: &FormattingContextRun<'_>,node: Node,- inline_size: CssPixels,- block_size: CssPixels,-) {+ containing_block: &impl BoxMetricCells,+ percentage_basis: geometry::LogicalSize,+) -> InFlowBoxInsets {AbsposEngine::for_run(run).compute_inset(node,- geometry::LogicalSize {- inline_size,- block_size,- },+ containing_block,+ percentage_basis,run.box_,run.treat_block_axis_percentage_insets_as_auto_beyond_root,- );+ )}/// The position option a box is laid out with, whose styles its style reads see while this lives, and the style recordsdiff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex b540e2c191c..28034458b2f 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -850,18 +850,19 @@ impl<'pass> BlockFormattingContext<'pass> {self.derived_baselines_of_root_box.get()}- fn compute_inset(- &self,- run: &FormattingContextRun<'pass>,- node: Node,- containing_block_size: geometry::LogicalSize,- ) {- abspos_engine::compute_inset_native(+ /// Resolves the insets of `node`, an in-flow child of `block_container`, and keeps them for the box.+ fn compute_inset(&self, run: &FormattingContextRun<'pass>, node: Node, block_container: Node) {+ let block_container_used = self.used(block_container);+ let insets = abspos_engine::compute_inset_native(run,node,- containing_block_size.inline_size,- containing_block_size.block_size,+ block_container_used,+ geometry::LogicalSize {+ inline_size: block_container_used.content_inline_size.get(),+ block_size: block_container_used.content_block_size.get(),+ },);+ run.records.keep_in_flow_box_insets(node, insets);}fn containing_block_rect(&self, node: Node, position: FfiCssPixelPoint) -> CssPixelRect {@@ -2773,15 +2774,7 @@ impl<'pass> BlockFormattingContext<'pass> {);}- let block_container_used = self.used(block_container);- self.compute_inset(- run,- node,- geometry::LogicalSize {- inline_size: block_container_used.content_inline_size.get(),- block_size: block_container_used.content_block_size.get(),- },- );+ self.compute_inset(run, node, block_container);let mut placed = match pending_position {Some(position) => self.place_laid_out_child_on_line(@@ -2961,15 +2954,7 @@ impl<'pass> BlockFormattingContext<'pass> {node,entry.depends_on_percentage_block_size,);- let block_container_used = self.used(block_container);- self.compute_inset(- &self.run,- node,- geometry::LogicalSize {- inline_size: block_container_used.content_inline_size.get(),- block_size: block_container_used.content_block_size.get(),- },- );+ self.compute_inset(&self.run, node, block_container);formatting_context::place_reused_child(&self.run,node,@@ -4614,15 +4599,7 @@ impl<'pass> BlockFormattingContext<'pass> {if let Some(line_builder) = line_builder {line_builder.recalculate_available_space();}- let block_container_used = self.used(block_container);- self.compute_inset(- run,- node,- geometry::LogicalSize {- inline_size: block_container_used.content_inline_size.get(),- block_size: block_container_used.content_block_size.get(),- },- );+ self.compute_inset(run, node, block_container);let inline_offset = if side == FloatSide::Left {floating_box.offset_from_edgediff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rsindex 8493ed1c4f6..aa43c1bb621 100644--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs@@ -3052,9 +3052,17 @@ impl<'pass> FlexFormattingContext<'pass> {ChildLayoutOutcome::Skipped { .. } => run.records.box_metrics_of(node).content_baselines(),};- let container_inline_size = self.container_used().content_inline_size.get();- let container_block_size = self.container_used().content_block_size.get();- abspos_engine::compute_inset_native(run, node, container_inline_size, container_block_size);+ let container_used = self.container_used();+ let insets = abspos_engine::compute_inset_native(+ run,+ node,+ container_used,+ geometry::LogicalSize {+ inline_size: container_used.content_inline_size.get(),+ block_size: container_used.content_block_size.get(),+ },+ );+ run.records.keep_in_flow_box_insets(node, insets);fragment_arrival}diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rsindex 24286fcc345..3bee7188835 100644--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs@@ -4446,7 +4446,8 @@ impl<'pass> GridFormattingContext<'pass> {};// Resolve relative-position insets before placement seals the// item's committed metrics.- abspos_engine::compute_inset_native(run, item.box_, area.size.inline_size, area.size.block_size);+ let insets = abspos_engine::compute_inset_native(run, item.box_, self.container_used(), area.size);+ run.records.keep_in_flow_box_insets(item.box_, insets);if self.item_participates_in_row_baseline_alignment(item)|| self.item_participates_in_column_baseline_alignment(item){diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 5559b6c1876..904716c3b5e 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -1276,14 +1276,20 @@ impl<'context> InlineFormattingContext<'context> {self.callbacks.text_content(node).may_require_bidi_processing}- pub(crate) fn compute_inset(&self, node: Node) {+ /// Resolves the insets of `node`, an in-flow box in this context's lines, and keeps them for the box.+ pub(crate) fn compute_inset(&self, node: Node) -> abspos_engine::InFlowBoxInsets {let used = self.containing_used();- abspos_engine::compute_inset_native(+ let insets = abspos_engine::compute_inset_native(self.run,node,- used.content_inline_size.get(),- used.content_block_size.get(),+ used,+ geometry::LogicalSize {+ inline_size: used.content_inline_size.get(),+ block_size: used.content_block_size.get(),+ },);+ self.run.records.keep_in_flow_box_insets(node, insets);+ insets}pub(crate) fn parent_commit_pending_margin_before_inline_content(&self) -> CssPixels {diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rsindex 42418ac67e2..64b9bb40444 100644--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs@@ -997,6 +997,17 @@ impl<'arena> RunRecords<'arena> {);}+ /// Keeps for `node`, an in-flow box, what its insets resolved to.+ #[track_caller]+ pub(crate) fn keep_in_flow_box_insets(&self, node: Node, insets: abspos_engine::InFlowBoxInsets) {+ if let Some(default_scroll_shift) = insets.default_scroll_shift {+ self.set_default_scroll_shift(node, Some(default_scroll_shift));+ }+ if let Some(used_insets) = insets.used_insets {+ self.set_used_insets(node, used_insets);+ }+ }+/// Tracks the percentage block size dependency of `node` until the returned tracking is dropped, which the layout/// of `node` holds while it is in progress.pub(crate) fn track_percentage_block_size_dependency_while_laying_out(