Truly independent web browser
LibWeb: Keep a flex root's own block size space out of its input
Aliaksandr Kalenik568ae3ab7ae8
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex aad02dca151..c289409ff9b 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -466,10 +466,6 @@ impl BlockMarginState {}/// What the probe for a stored in-flow layout found, and what storing the fresh layout needs.-#[expect(- clippy::large_enum_variant,- reason = "held on the stack while one child is laid out, where boxing would allocate for every child"-)]enum InFlowReuse {Ineligible,Hit {@@ -4997,6 +4993,7 @@ impl<'pass> BlockFormattingContext<'pass> {);let ParticipationInParentFormattingContext::Float {containing_block_position_in_bfc_root,+ ..} = input.participationelse {unreachable!("a float root is dimensioned as a float");@@ -5067,23 +5064,25 @@ impl<'pass> BlockFormattingContext<'pass> {let node = sized_float.node;let available_space = input.available_space;assert!(self.facts(node).is_floating());- let mut float_input = LayoutInput {+ // NB: This is found ahead of the float's inline size, which can make its block size transfer through its aspect+ // ratio.+ let flex_container_resolves_its_own_automatic_block_size =+ formatting_context::flex_self_block_size_resolution_space(+ &self.sizing(),+ sizing_context::SizedBox::new(node, &self.metrics_of(sized_float)),+ available_space,+ input.containing_block_constraints,+ )+ .is_some();+ let float_input = LayoutInput {available_space,containing_block_constraints: input.containing_block_constraints,sizing: RootSizingDirectives::default(),participation: ParticipationInParentFormattingContext::Float {containing_block_position_in_bfc_root: containing_block_position_in_root,+ flex_container_resolves_its_own_automatic_block_size,},};- // NB: The space is found ahead of the float's inline size, which can make its block size transfer through its- // aspect ratio.- float_input.sizing.flex_self_block_size_resolution_space =- formatting_context::flex_self_block_size_resolution_space(- &self.sizing(),- sizing_context::SizedBox::new(node, &self.metrics_of(sized_float)),- available_space,- input.containing_block_constraints,- );self.dimension_float_root_inline_size(sized_float, block_container, &float_input);let (float_layout, fragment_arrival, float_box) =self.layout_inside(run, sized_float, block_container, float_input, false);diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rsindex 52dabcfb1f6..75ad2c1c504 100644--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs@@ -109,6 +109,7 @@ impl FcRunCacheKey {// run and places it after. The run lays the float out from its own origin.if let ParticipationInParentFormattingContext::Float {containing_block_position_in_bfc_root,+ ..} = &mut input.participation{*containing_block_position_in_bfc_root = FfiCssPixelPoint::default();@@ -199,9 +200,15 @@ fn participation_matches_probe() -> bool {match (stored, probe) {(- ParticipationInParentFormattingContext::Float { .. },- ParticipationInParentFormattingContext::Float { .. },- ) => true,+ ParticipationInParentFormattingContext::Float {+ flex_container_resolves_its_own_automatic_block_size: stored,+ ..+ },+ ParticipationInParentFormattingContext::Float {+ flex_container_resolves_its_own_automatic_block_size: probe,+ ..+ },+ ) => stored == probe,(ParticipationInParentFormattingContext::AbsolutelyPositioned(stored),ParticipationInParentFormattingContext::AbsolutelyPositioned(probe),@@ -273,7 +280,6 @@ fn layout_inputs_match_ignoring_unobserved(&& sizing_directives_match_ignoring_unobserved(sizing,&b.sizing,- unobserved.available_block_size_definiteness(),unobserved.content_block_size_adopted_after_run,)}@@ -281,7 +287,6 @@ fn layout_inputs_match_ignoring_unobserved(fn sizing_directives_match_ignoring_unobserved(a: &RootSizingDirectives,b: &RootSizingDirectives,- block_size_definiteness_unobserved: bool,content_block_size_adopted_after_run_unobserved: bool,) -> bool {let RootSizingDirectives {@@ -291,7 +296,6 @@ fn sizing_directives_match_ignoring_unobserved(table_cell_intrinsic_block_padding,table_box_content_block_offset_in_wrapper,content_block_size_after_run,- flex_self_block_size_resolution_space,outer_float_intrusion_before_list_item_children,treat_block_axis_percentage_insets_as_auto_beyond_root,math_stretch_size_constraint,@@ -309,21 +313,6 @@ fn sizing_directives_match_ignoring_unobserved(== b.treat_block_axis_percentage_insets_as_auto_beyond_root&& math_stretch_size_constraint == b.math_stretch_size_constraint&& root_is_measured_for_its_content_block_size == b.root_is_measured_for_its_content_block_size- && match (- flex_self_block_size_resolution_space,- b.flex_self_block_size_resolution_space,- ) {- (None, None) => true,- (Some(a_space), Some(b_space)) => {- a_space.inline_size == b_space.inline_size- && available_block_sizes_match(- a_space.block_size,- b_space.block_size,- block_size_definiteness_unobserved,- )- }- _ => false,- }}/// What must still be true for a stored entry to be replayed: the slot@@ -1588,6 +1577,7 @@ mod tests {|probe| {probe.input.participation = ParticipationInParentFormattingContext::Float {containing_block_position_in_bfc_root: FfiCssPixelPoint::default(),+ flex_container_resolves_its_own_automatic_block_size: false,}},];diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rsindex d01d38def3a..bbd9ca265a1 100644--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs@@ -378,6 +378,8 @@ pub(super) struct FlexFormattingContext<'pass> {available_space_for_items: Option<AxisAgnosticAvailableSpace>,available_space: Option<AvailableSpace>,layout_input: Option<LayoutInput>,+ /// The space in which the container resolves its own automatic block size, if its run left that to it.+ own_automatic_block_size_resolution_space: Option<AvailableSpace>,item_percentage_bases: ContainingBlockConstraints,item_runs_offered_to_block_size_measurements: Option<ItemRunsOfferedToBlockSizeMeasurements>,flex_item_index_by_node: OnceCell<HashMap<Node, usize>>,@@ -427,6 +429,7 @@ impl<'pass> FlexFormattingContext<'pass> {available_space_for_items: None,available_space: None,layout_input: None,+ own_automatic_block_size_resolution_space: None,item_percentage_bases: ContainingBlockConstraints::default(),item_runs_offered_to_block_size_measurements: None,flex_item_index_by_node: OnceCell::new(),@@ -1772,13 +1775,7 @@ impl<'pass> FlexFormattingContext<'pass> {// indefinite available main size even when its calculation changes the used size.let available_main_size = if !self.main_axis_is_horizontal()&& crate::layout::sizing_context::is_plain_auto(self.container_computed_main_size())- && self- .layout_input- .as_ref()- .unwrap()- .sizing- .flex_self_block_size_resolution_space- .is_some()+ && self.own_automatic_block_size_resolution_space.is_some(){AvailableSize::definite(self.inner_main_size_used(&self.container_cells))} else {@@ -3764,7 +3761,7 @@ impl<'pass> FlexFormattingContext<'pass> {fn resolve_own_auto_block_size_from_max_content_main_size(&mut self) {let sizing = self.layout_input.as_ref().unwrap().sizing;- let resolution_space = sizing.flex_self_block_size_resolution_space;+ let resolution_space = self.own_automatic_block_size_resolution_space;if resolution_space.is_none() && !sizing.root_is_measured_for_its_content_block_size {return;}@@ -3799,7 +3796,7 @@ impl<'pass> FlexFormattingContext<'pass> {return;}let sizing = self.layout_input.as_ref().unwrap().sizing;- if let Some(resolution_space) = sizing.flex_self_block_size_resolution_space {+ if let Some(resolution_space) = self.own_automatic_block_size_resolution_space {self.resolve_own_auto_block_size(self.automatic_block_size_from_line_cross_sizes(), resolution_space);} else if sizing.root_is_measured_for_its_content_block_size {self.set_container_cross_size(self.automatic_block_size_from_line_cross_sizes());@@ -4022,13 +4019,7 @@ impl<'pass> FlexFormattingContext<'pass> {}if self.flex_item_details[index].inputs.preferred_aspect_ratio.is_some()|| self.has_definite_main_size(index)- || self- .layout_input- .as_ref()- .unwrap()- .sizing- .flex_self_block_size_resolution_space- .is_some()+ || self.own_automatic_block_size_resolution_space.is_some(){return true;}@@ -4206,7 +4197,13 @@ impl<'pass> FlexFormattingContext<'pass> {}}- pub(super) fn run(&mut self, run: &FormattingContextRun<'pass>, layout_input: LayoutInput) {+ pub(super) fn run(+ &mut self,+ run: &FormattingContextRun<'pass>,+ layout_input: LayoutInput,+ own_automatic_block_size_resolution_space: Option<AvailableSpace>,+ ) {+ self.own_automatic_block_size_resolution_space = own_automatic_block_size_resolution_space;self.prepare_items(layout_input);// 5. Collect flex items into flex lines:diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex ec9e98f73dd..a6a207431f5 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -1644,6 +1644,7 @@ fn eagerly_resolve_atomic_flex_root_auto_block_size(struct RootSizingOutcome {body_input: LayoutInput,+ flex_self_block_size_resolution_space: Option<AvailableSpace>,atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above: Option<CssPixels>,}@@ -1655,11 +1656,18 @@ fn apply_root_sizing_directives(root_used_insets: &Cell<run_state::UsedInsets>,) -> RootSizingOutcome {let mut atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above = None;+ let mut flex_self_resolution_space = None;let body_input = match &input.participation {ParticipationInParentFormattingContext::BlockLevel => {+ flex_self_resolution_space = size_block_level_root_ahead_of_its_contents(run, sized_root, input);dimension_block_level_root(run, sized_root, input, fc_type)}- ParticipationInParentFormattingContext::Float { .. } => {+ ParticipationInParentFormattingContext::Float {+ flex_container_resolves_its_own_automatic_block_size,+ ..+ } => {+ flex_self_resolution_space =+ (*flex_container_resolves_its_own_automatic_block_size).then_some(input.available_space);resolve_float_root_block_size_ahead_of_its_contents(run, sized_root, input);body_input_with_inner_available_space(sized_root.cells, input)}@@ -1672,12 +1680,12 @@ fn apply_root_sizing_directives(input.containing_block_constraints,run_cache_may_reuse_this_run_at_other_available_inline_sizes,);- let mut body_input = body_input_with_inner_available_space(sized_root.cells, input);+ let body_input = body_input_with_inner_available_space(sized_root.cells, input);let inline_definite_space = AvailableSpace {inline_size: AvailableSize::definite(sized_root.cells.content_inline_size()),block_size: AvailableSize::Indefinite,};- let flex_self_resolution_space = flex_self_block_size_resolution_space(+ flex_self_resolution_space = flex_self_block_size_resolution_space(&run.sizing(),sized_root.sized_box(),inline_definite_space,@@ -1686,11 +1694,10 @@ fn apply_root_sizing_directives(if flex_self_resolution_space.is_none() {eagerly_resolve_atomic_flex_root_auto_block_size(run, sized_root, input, inline_definite_space);}- body_input.sizing.flex_self_block_size_resolution_space = flex_self_resolution_space;body_input}ParticipationInParentFormattingContext::AbsolutelyPositioned(abspos_inputs) => {- let flex_self_resolution_space = abspos_engine::AbsposEngine::for_run(run).dimension_out_of_flow_root(+ flex_self_resolution_space = abspos_engine::AbsposEngine::for_run(run).dimension_out_of_flow_root(abspos_engine::OutOfFlowBox::new(sized_root,root_used_insets,@@ -1699,9 +1706,7 @@ fn apply_root_sizing_directives(abspos_inputs,true,);- let mut body_input = body_input_with_inner_available_space(sized_root.cells, input);- body_input.sizing.flex_self_block_size_resolution_space = flex_self_resolution_space;- body_input+ body_input_with_inner_available_space(sized_root.cells, input)}ParticipationInParentFormattingContext::Item => {if cfg!(debug_assertions) && run.fragments.is_some() {@@ -1716,6 +1721,7 @@ fn apply_root_sizing_directives(};RootSizingOutcome {body_input,+ flex_self_block_size_resolution_space: flex_self_resolution_space,atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above,}}@@ -1731,9 +1737,7 @@ fn dimension_block_level_root(let constraints = input.containing_block_constraints;let sizing = run.sizing();let style = StyleValues::for_node(&run.callbacks, node);- let flex_self_resolution_space = size_block_level_root_ahead_of_its_contents(run, sized_root, input);let mut body_input = body_input_with_inner_available_space(sized_root.cells, input);- body_input.sizing.flex_self_block_size_resolution_space = flex_self_resolution_space;if let Some(block_size) = input.sizing.forced_content_block_size.get() {let used = sized_root.cells;used.set_content_block_size(block_size);@@ -1807,6 +1811,16 @@ fn size_block_level_root_ahead_of_its_contents(flex_self_resolution_space}+fn float_flex_container_resolves_its_own_automatic_block_size(input: &LayoutInput) -> bool {+ matches!(+ input.participation,+ ParticipationInParentFormattingContext::Float {+ flex_container_resolves_its_own_automatic_block_size: true,+ ..+ }+ )+}+/// Resolves the block size of the root of a float's run where that follows without laying out its contents, in the/// inline size its block formatting context gave it.fn resolve_float_root_block_size_ahead_of_its_contents(@@ -1827,7 +1841,7 @@ fn resolve_float_root_block_size_ahead_of_its_contents(if facts.has_auto_content_box_size()|| block_size_is_definite_from_aspect_ratio|| (StyleValues::for_node(&run.callbacks, node).display().is_flex_inside()- && input.sizing.flex_self_block_size_resolution_space.is_none())+ && !float_flex_container_resolves_its_own_automatic_block_size(input)){sizing.resolve_used_block_size_if_treated_as_auto(sized_root, available_space, constraints, None, || {block_formatting_context::automatic_block_size_of_independent_root_ahead_of_its_contents(@@ -2286,6 +2300,7 @@ fn lay_out_run_root_and_contents<'pass>(let root_used_insets = Cell::new(run_state::UsedInsets::default());let RootSizingOutcome {body_input,+ mut flex_self_block_size_resolution_space,atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above,} = apply_root_sizing_directives(run, sized_root, input, fc_type, &root_used_insets);debug_assert!(@@ -2307,6 +2322,10 @@ fn lay_out_run_root_and_contents<'pass>(} else {body_input};+ if parent_frame.is_orthogonal_to(own_frame) {+ // NB: The space is one of the parent's frame, whose axes the root's contents do not share.+ flex_self_block_size_resolution_space = None;+ }let mut implementation = None;let mut root_lines = used_values::LineDataState::Empty;@@ -2362,7 +2381,7 @@ fn lay_out_run_root_and_contents<'pass>((result, root_after_body)}FormattingContextImplementation::Flex(context) => {- context.run(body_run, body_input);+ context.run(body_run, body_input, flex_self_block_size_resolution_space);let baselines = context.derived_baselines_of_root_box();let result = ChildLayoutResult {automatic_content_block_size: context.automatic_content_block_size(),@@ -2684,7 +2703,7 @@ pub(super) fn empty_root_needs_no_formatting_context_algorithm(match fc_type {FormattingContextType::Block => !NodeFacts::new(callbacks, box_).display().is_math_inside(),FormattingContextType::Flex => {- input.sizing.flex_self_block_size_resolution_space.is_none()+ !float_flex_container_resolves_its_own_automatic_block_size(input)&& !input.sizing.root_is_measured_for_its_content_block_size}_ => false,diff --git a/Libraries/LibWeb/Rust/src/layout/geometry.rs b/Libraries/LibWeb/Rust/src/layout/geometry.rsindex eb27afb2108..8ca7c9906c5 100644--- a/Libraries/LibWeb/Rust/src/layout/geometry.rs+++ b/Libraries/LibWeb/Rust/src/layout/geometry.rs@@ -368,7 +368,6 @@ pub(crate) struct RootSizingDirectives {// wrapper) can be placed in wrapper coordinates.pub(crate) table_box_content_block_offset_in_wrapper: PackedOptionalCssPixels,pub(crate) content_block_size_after_run: ContentBlockSizeAfterRun,- pub(crate) flex_self_block_size_resolution_space: Option<AvailableSpace>,pub(crate) outer_float_intrusion_before_list_item_children: inline_formatting_context::SpaceUsedByFloats,pub(crate) treat_block_axis_percentage_insets_as_auto_beyond_root: bool,pub(crate) root_is_measured_for_its_content_block_size: bool,@@ -412,6 +411,9 @@ pub(crate) enum ParticipationInParentFormattingContext {/// containing block, placed here.Float {containing_block_position_in_bfc_root: FfiCssPixelPoint,+ /// Whether the float is a flex container that resolves its own automatic block size in its available space,+ /// which its parent finds ahead of the float's inline size.+ flex_container_resolves_its_own_automatic_block_size: bool,},AtomicInline,AbsolutelyPositioned(std::sync::Arc<abspos_inputs::AbsposLayoutInputs>),