Skip to content

Truly independent web browser

LibWeb: Adopt flex item block size measurements in item layout

Aliaksandr Kaleniked0802d0bca0
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index fec25c676e1..af06b692a8a 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -245,7 +245,7 @@ impl<'pass> AbsposEngine<'pass> {
}
#[inline]
- fn sizing(&self) -> sizing_context::SizingContext<'pass> {
+ fn sizing(&self) -> sizing_context::SizingContext<'pass, 'static> {
sizing_context::SizingContext::new(self.records, self.callbacks)
}
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 368f8950fe4..66b32dce7ec 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -327,7 +327,7 @@ impl ColumnEarlyBreaks {
}
pub(crate) fn root_lays_out_content_against_its_own_block_size(
- sizing: &sizing_context::SizingContext<'_>,
+ sizing: &sizing_context::SizingContext<'_, '_>,
root: Node,
) -> bool {
let facts = sizing.facts(root);
@@ -533,7 +533,7 @@ impl<'pass> BlockFormattingContext<'pass> {
}
#[inline]
- pub(crate) fn sizing(&self) -> sizing_context::SizingContext<'pass> {
+ pub(crate) fn sizing(&self) -> sizing_context::SizingContext<'pass, 'static> {
sizing_context::SizingContext::new(self.run.records, self.run.callbacks)
}
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index 57ca05fe221..ddac6744212 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -36,7 +36,9 @@ impl RunInputsTheStoredRunNeverObserved {
.atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above,
percentage_inline_basis: outputs
.atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above
- .is_some(),
+ .is_some()
+ || outputs.result.percentage_inline_basis_observation
+ == formatting_context::PercentageInlineBasisObservation::Unobserved,
own_block_size: outputs.result.own_block_size_observation,
}
}
@@ -1530,6 +1532,37 @@ mod tests {
assert!(!item_key(40, false).matches(&button_aligned, unobserved));
}
+ #[test]
+ fn an_item_run_whose_root_never_observed_its_percentage_inline_basis_matches_at_another_basis() {
+ let at_percentage_inline_basis = |basis: i64| {
+ let mut key = item_key(40, false);
+ key.input.containing_block_constraints.percentage_basis_inline_size = Some(px(basis));
+ key
+ };
+ let mut outputs = entry(item_key(40, false), 1, None, 0).outputs;
+ assert!(!at_percentage_inline_basis(400).matches(
+ &at_percentage_inline_basis(300),
+ RunInputsTheStoredRunNeverObserved::proven_by_stored_run(&outputs)
+ ));
+ outputs.result.percentage_inline_basis_observation =
+ formatting_context::PercentageInlineBasisObservation::Unobserved;
+ let unobserved = RunInputsTheStoredRunNeverObserved::proven_by_stored_run(&outputs);
+ assert!(at_percentage_inline_basis(400).matches(&at_percentage_inline_basis(300), unobserved));
+ let mut wider = at_percentage_inline_basis(300);
+ wider.root_cells.content_inline_size = px(320);
+ wider.input.available_space.inline_size = AvailableSize::definite(px(320));
+ assert!(
+ !at_percentage_inline_basis(400).matches(&wider, unobserved),
+ "only the percentage inline basis is unobserved"
+ );
+ assert!(!at_percentage_inline_basis(400).matches(
+ &at_percentage_inline_basis(300),
+ RunInputsTheStoredRunNeverObserved::accepted_when_reusing_stale_line_data_after_structural_inline_damage(
+ &outputs
+ )
+ ));
+ }
+
#[test]
fn stale_line_data_after_structural_inline_damage_is_taken_only_at_the_same_own_block_size() {
let mut outputs = entry(item_key(40, false), 1, None, 0).outputs;
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index c11a1cdeb81..170422a5aab 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -178,6 +178,18 @@ struct AxisAgnosticAvailableSpace {
space: AvailableSpace,
}
+#[derive(Clone, Copy)]
+enum ItemBlockSizeMeasuredAt {
+ UsedMainSize,
+ IntrinsicBlockInlineSize,
+}
+
+#[derive(Clone, Copy)]
+struct ItemRunsOfferedToBlockSizeMeasurements {
+ measured_at: ItemBlockSizeMeasuredAt,
+ space_for_items: AvailableSpace,
+}
+
pub(super) struct FlexFormattingContext<'pass> {
run: FormattingContextRun<'pass>,
flex_lines: Vec<FlexLine>,
@@ -188,6 +200,36 @@ pub(super) struct FlexFormattingContext<'pass> {
available_space: Option<AvailableSpace>,
layout_input: Option<LayoutInput>,
item_percentage_bases: ContainingBlockConstraints,
+ item_runs_offered_to_block_size_measurements: Option<ItemRunsOfferedToBlockSizeMeasurements>,
+ next_flex_item_lookup_start: Cell<usize>,
+}
+
+impl formatting_context::ChildRunShapes for FlexFormattingContext<'_> {
+ fn child_run_shape_at(
+ &self,
+ node: Node,
+ inline_size: CssPixels,
+ constraints: ContainingBlockConstraints,
+ ) -> Option<formatting_context::ChildRunShape> {
+ let offered = self.item_runs_offered_to_block_size_measurements?;
+ let index = self.index_of_flex_item(node)?;
+ let measured_inline_size = match offered.measured_at {
+ ItemBlockSizeMeasuredAt::UsedMainSize => {
+ if self.has_definite_cross_size(index) {
+ return None;
+ }
+ clamp_to_max_dimension_value(self.flex_items[index].main_size?)
+ }
+ ItemBlockSizeMeasuredAt::IntrinsicBlockInlineSize => {
+ if self.has_definite_main_size(index) {
+ return None;
+ }
+ self.intrinsic_block_inline_size(index)
+ }
+ };
+ self.item_run_shape(index, measured_inline_size, offered.space_for_items)
+ .filter(|shape| shape.lays_out_at(inline_size, constraints))
+ }
}
impl<'pass> FlexFormattingContext<'pass> {
@@ -206,6 +248,8 @@ impl<'pass> FlexFormattingContext<'pass> {
available_space: None,
layout_input: None,
item_percentage_bases: ContainingBlockConstraints::default(),
+ item_runs_offered_to_block_size_measurements: None,
+ next_flex_item_lookup_start: Cell::new(0),
}
}
@@ -227,8 +271,8 @@ impl<'pass> FlexFormattingContext<'pass> {
}
#[inline]
- fn sizing(&self) -> sizing_context::SizingContext<'pass> {
- sizing_context::SizingContext::new(self.run.records, self.run.callbacks)
+ fn sizing(&self) -> sizing_context::SizingContext<'pass, '_> {
+ sizing_context::SizingContext::new(self.run.records, self.run.callbacks).with_child_run_shapes(self)
}
fn create_used_values(&self, node: Node) -> &'pass UsedValues {
@@ -2728,21 +2772,7 @@ impl<'pass> FlexFormattingContext<'pass> {
}
fn copy_dimensions_from_flex_items_to_boxes(&mut self) {
- let reference = self.container_used().content_inline_size.get();
for index in 0..self.flex_items.len() {
- let style = self.style(self.flex_items[index].box_);
- {
- let used = self.item_used(index);
- used.margin_left.set(style.margin_left().to_px(reference));
- used.margin_right.set(style.margin_right().to_px(reference));
- used.margin_top.set(style.margin_top().to_px(reference));
- used.margin_bottom.set(style.margin_bottom().to_px(reference));
- used.border_left.set(style.border_left_width());
- used.border_right.set(style.border_right_width());
- used.border_top.set(style.border_top_width());
- used.border_bottom.set(style.border_bottom_width());
- }
- self.set_main_size(index, self.flex_items[index].main_size.unwrap());
// NB: An item whose transferred cross size grows to fit its content lays out that content against the
// transferred size, and grows to the used cross size in its own layout.
let cross_size = if self.flex_items[index].cross_size_was_resolved_from_aspect_ratio
@@ -2754,28 +2784,44 @@ impl<'pass> FlexFormattingContext<'pass> {
} else {
self.flex_items[index].cross_size.unwrap()
};
- self.set_cross_size(index, cross_size);
+ self.prepare_item_record(
+ index,
+ self.item_used(index),
+ self.flex_items[index].main_size,
+ Some(cross_size),
+ );
}
}
- fn measure_table_wrapper_block_sizes(&self, index: usize) -> sizing_context::TableWrapperBlockSizes {
- let mut intrinsic_space = self
- .item_used(index)
- .available_inner_space_or_constraints_from(self.available_space_for_items.unwrap().space);
- intrinsic_space.block_size = AvailableSize::Indefinite;
- self.sizing().measure_table_box_block_size_inside_wrapper(
- self.flex_items[index].box_,
- intrinsic_space,
- self.item_containing_block_constraints(),
- )
+ fn prepare_item_record(
+ &self,
+ index: usize,
+ used: &UsedValues,
+ main_size: Option<CssPixels>,
+ cross_size: Option<CssPixels>,
+ ) {
+ let reference = self.container_used().content_inline_size.get();
+ let style = self.style(self.flex_items[index].box_);
+ used.margin_left.set(style.margin_left().to_px(reference));
+ used.margin_right.set(style.margin_right().to_px(reference));
+ used.margin_top.set(style.margin_top().to_px(reference));
+ used.margin_bottom.set(style.margin_bottom().to_px(reference));
+ used.border_left.set(style.border_left_width());
+ used.border_right.set(style.border_right_width());
+ used.border_top.set(style.border_top_width());
+ used.border_bottom.set(style.border_bottom_width());
+ if let Some(main_size) = main_size {
+ self.set_main_size_used(used, main_size);
+ }
+ if let Some(cross_size) = cross_size {
+ self.set_cross_size_used(used, cross_size);
+ }
}
- fn layout_inside_item(&mut self, run: &FormattingContextRun<'pass>, index: usize) {
+ fn item_layout_input(&self, index: usize, used: &UsedValues, space_for_items: AvailableSpace) -> LayoutInput {
let node = self.flex_items[index].box_;
let mut input = LayoutInput {
- available_space: self
- .item_used(index)
- .available_inner_space_or_constraints_from(self.available_space_for_items.unwrap().space),
+ available_space: used.available_inner_space_or_constraints_from(space_for_items),
containing_block_constraints: self.item_containing_block_constraints(),
content_box_position_in_bfc_root: None,
sizing: RootSizingDirectives::default(),
@@ -2813,7 +2859,113 @@ impl<'pass> FlexFormattingContext<'pass> {
{
input.sizing.content_block_size_after_inside_layout = self.flex_items[index].cross_size;
}
+ input
+ }
+ fn offers_item_runs_to_block_size_measurements(&self) -> bool {
+ let space = self.available_space_for_items.unwrap().space;
+ self.run.layout_mode == LayoutMode::Normal
+ && !self.run.should_collect_devtools_layout_data
+ && !space.inline_size.is_intrinsic_sizing_constraint()
+ && !space.block_size.is_intrinsic_sizing_constraint()
+ && self.inline_axis_is_horizontal(self.run.box_)
+ }
+
+ fn item_is_block_container_whose_committing_run_can_adopt_its_measurement(&self, index: usize) -> bool {
+ let node = self.flex_items[index].box_;
+ let facts = self.facts(node);
+ let sizing = self.sizing();
+ !facts.document_in_quirks_mode()
+ && !facts.is_table_wrapper()
+ && !facts.is_replaced_box()
+ && !facts.has_preferred_aspect_ratio()
+ && self.style(node).writing_mode() == writing_mode::HORIZONTAL_TB
+ && self.inline_axis_is_horizontal(node)
+ && formatting_context::formatting_context_type_created_by_box(facts)
+ == Some(formatting_context::FormattingContextType::Block)
+ && !sizing.own_style_depends_on_percentage_block_size(node)
+ && !block_formatting_context::root_lays_out_content_against_its_own_block_size(&sizing, node)
+ }
+
+ fn offer_item_runs_to_block_size_measurements(&mut self, measured_at: ItemBlockSizeMeasuredAt) {
+ if self.offers_item_runs_to_block_size_measurements() {
+ self.item_runs_offered_to_block_size_measurements = Some(ItemRunsOfferedToBlockSizeMeasurements {
+ measured_at,
+ space_for_items: self.available_space_for_items.unwrap().space,
+ });
+ }
+ }
+
+ fn offer_runs_of_items_measured_at_their_main_size(&mut self) {
+ if self.main_axis_is_horizontal() {
+ self.offer_item_runs_to_block_size_measurements(ItemBlockSizeMeasuredAt::UsedMainSize);
+ }
+ }
+
+ fn offer_runs_of_items_measured_in_their_main_axis(&mut self) {
+ if !self.main_axis_is_horizontal() {
+ self.offer_item_runs_to_block_size_measurements(ItemBlockSizeMeasuredAt::IntrinsicBlockInlineSize);
+ }
+ }
+
+ fn index_of_flex_item(&self, node: Node) -> Option<usize> {
+ let item_count = self.flex_items.len();
+ let start = self.next_flex_item_lookup_start.get().min(item_count);
+ let index = (start..item_count)
+ .chain(0..start)
+ .find(|&index| self.flex_items[index].box_ == node)?;
+ self.next_flex_item_lookup_start.set(index + 1);
+ Some(index)
+ }
+
+ fn item_run_shape(
+ &self,
+ index: usize,
+ measured_inline_size: CssPixels,
+ space_for_items: AvailableSpace,
+ ) -> Option<formatting_context::ChildRunShape> {
+ let node = self.flex_items[index].box_;
+ if !self.item_is_block_container_whose_committing_run_can_adopt_its_measurement(index) {
+ return None;
+ }
+ let (main_size, cross_size) = if self.main_axis_is_horizontal() {
+ (Some(measured_inline_size), None)
+ } else {
+ (None, Some(measured_inline_size))
+ };
+ let record = used_values::RootInputCells::capture(self.item_used(index)).materialize_record();
+ self.prepare_item_record(index, &record, main_size, cross_size);
+ let input = self.item_layout_input(index, &record, space_for_items);
+ let plan = formatting_context::child_run_plan(&self.run, node, input, false)?;
+ debug_assert_eq!(plan.fc_type, formatting_context::FormattingContextType::Block);
+ Some(formatting_context::ChildRunShape {
+ node,
+ fc_type: plan.fc_type,
+ input: plan.input,
+ root_cells: used_values::RootInputCells::capture(&record),
+ root_containing_block: plan.root_containing_block,
+ })
+ }
+
+ fn measure_table_wrapper_block_sizes(&self, index: usize) -> sizing_context::TableWrapperBlockSizes {
+ let mut intrinsic_space = self
+ .item_used(index)
+ .available_inner_space_or_constraints_from(self.available_space_for_items.unwrap().space);
+ intrinsic_space.block_size = AvailableSize::Indefinite;
+ self.sizing().measure_table_box_block_size_inside_wrapper(
+ self.flex_items[index].box_,
+ intrinsic_space,
+ self.item_containing_block_constraints(),
+ )
+ }
+
+ fn layout_inside_item(&mut self, run: &FormattingContextRun<'pass>, index: usize) {
+ let node = self.flex_items[index].box_;
+ let input = self.item_layout_input(
+ index,
+ self.item_used(index),
+ self.available_space_for_items.unwrap().space,
+ );
self.flex_items[index].content_baselines =
match formatting_context::layout_inside_child(run, None, None, node, LayoutMode::Normal, input, false) {
ChildLayoutOutcome::Created(result) => result.baselines,
@@ -3505,6 +3657,7 @@ impl<'pass> FlexFormattingContext<'pass> {
self.available_space = Some(available_space);
self.layout_input = Some(layout_input);
+ self.item_runs_offered_to_block_size_measurements = None;
self.item_percentage_bases =
self.constraints_for_child_context(self.run.box_, layout_input.containing_block_constraints);
// 1. Generate anonymous flex items
@@ -3549,6 +3702,8 @@ impl<'pass> FlexFormattingContext<'pass> {
}
}
+ self.offer_runs_of_items_measured_in_their_main_axis();
+
// 4. Determine the main size of the flex container
// Determine the main size of the flex container using the rules of the formatting context in which it participates.
// NOTE: The automatic block size of a block-level flex container is its max-content size.
@@ -3615,6 +3770,7 @@ impl<'pass> FlexFormattingContext<'pass> {
// 6. Resolve the flexible lengths
self.resolve_flexible_lengths();
// Cross Size Determination
+ self.offer_runs_of_items_measured_at_their_main_size();
// 7. Determine the hypothetical cross size of each item
for index in 0..self.flex_items.len() {
self.determine_hypothetical_cross_size_of_item(index);
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 16ee05e29b9..94cd7de4820 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1024,6 +1024,7 @@ pub(crate) struct ChildLayoutResult {
pub content_block_size_for_aspect_ratio_minimum: Option<CssPixels>,
pub depends_on_percentage_block_size: bool,
pub own_block_size_observation: OwnBlockSizeObservation,
+ pub percentage_inline_basis_observation: PercentageInlineBasisObservation,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -1033,6 +1034,13 @@ pub(crate) enum OwnBlockSizeObservation {
Unobserved,
}
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub(crate) enum PercentageInlineBasisObservation {
+ #[default]
+ Observed,
+ Unobserved,
+}
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum RootCellsOutcome {
AllCells(used_values::UsedValuesCellState),
@@ -1296,7 +1304,7 @@ impl<'pass> FormattingContextRun<'pass> {
}
#[inline]
- pub(crate) fn sizing(&self) -> sizing_context::SizingContext<'pass> {
+ pub(crate) fn sizing(&self) -> sizing_context::SizingContext<'pass, 'static> {
sizing_context::SizingContext::new(self.records, self.callbacks)
}
@@ -1534,15 +1542,11 @@ pub(crate) fn independent_root_automatic_block_size(
}
fn flex_self_block_size_resolution_space(
- child_run_builds_fragments: bool,
sizing: &sizing_context::SizingContext,
node: Node,
resolution_space: AvailableSpace,
constraints: ContainingBlockConstraints,
) -> Option<AvailableSpace> {
- if !child_run_builds_fragments {
- return None;
- }
let style = sizing.style(node);
if !style.display().is_flex_inside() || !crate::layout::sizing_context::is_plain_auto(style.min_height()) {
return None;
@@ -1621,7 +1625,6 @@ fn apply_root_sizing_directives(
block_size: AvailableSize::Indefinite,
};
let flex_self_resolution_space = flex_self_block_size_resolution_space(
- run.fragments.is_some(),
&run.sizing(),
run.box_,
inline_definite_space,
@@ -1815,31 +1818,31 @@ fn dimension_root_in_parent_scope(
parent_block: Option<&block_formatting_context::BlockFormattingContext>,
child: Node,
input: &LayoutInput,
- child_run_builds_fragments: bool,
+ child_run_follows: bool,
) -> Option<AvailableSpace> {
+ let flex_self_resolution_space_of_following_run = |parent: &block_formatting_context::BlockFormattingContext| {
+ child_run_follows
+ .then(|| {
+ flex_self_block_size_resolution_space(
+ &parent.sizing(),
+ child,
+ input.available_space,
+ input.containing_block_constraints,
+ )
+ })
+ .flatten()
+ };
match input.participation {
ParticipationInParentFormattingContext::BlockLevel => {
let parent = parent_block.expect("a block-level run requires an enclosing block formatting context");
parent.commit_block_level_root_inline_size(child, input);
- let flex_self_resolution_space = flex_self_block_size_resolution_space(
- child_run_builds_fragments,
- &parent.sizing(),
- child,
- input.available_space,
- input.containing_block_constraints,
- );
+ let flex_self_resolution_space = flex_self_resolution_space_of_following_run(parent);
parent.resolve_block_level_root_block_size_before_body(child, input, flex_self_resolution_space.is_some());
flex_self_resolution_space
}
ParticipationInParentFormattingContext::Float => {
let parent = parent_block.expect("a floating run requires an enclosing block formatting context");
- let flex_self_resolution_space = flex_self_block_size_resolution_space(
- child_run_builds_fragments,
- &parent.sizing(),
- child,
- input.available_space,
- input.containing_block_constraints,
- );
+ let flex_self_resolution_space = flex_self_resolution_space_of_following_run(parent);
parent.dimension_float_root(child, input, flex_self_resolution_space.is_some());
flex_self_resolution_space
}
@@ -2392,6 +2395,8 @@ fn execute_formatting_context_run(
if !run_is_handed_table_inline_layout_outside_its_key {
result.own_block_size_observation =
own_block_size_observation_of_item_run(run, fc_type, &input, &result);
+ result.percentage_inline_basis_observation =
+ percentage_inline_basis_observation_of_item_run(run, fc_type, &input);
}
// NB: The root's descendants are laid out in the frame the root establishes.
@@ -2794,7 +2799,7 @@ pub(crate) fn layout_inside_child(
return ChildLayoutOutcome::ReenterCurrent;
};
input.sizing.flex_self_block_size_resolution_space =
- dimension_root_in_parent_scope(parent_block, child, &input, child_run_builds_fragments);
+ dimension_root_in_parent_scope(parent_block, child, &input, true);
let result = run_formatting_context(
run.fragments.as_deref(),
used,
@@ -2851,6 +2856,21 @@ pub(crate) struct ChildRunShape {
pub(crate) root_containing_block: Node,
}
+impl ChildRunShape {
+ pub(crate) fn lays_out_at(&self, inline_size: CssPixels, constraints: ContainingBlockConstraints) -> bool {
+ self.root_cells.content_inline_size == inline_size && self.input.containing_block_constraints == constraints
+ }
+}
+
+pub(crate) trait ChildRunShapes {
+ fn child_run_shape_at(
+ &self,
+ node: Node,
+ inline_size: CssPixels,
+ constraints: ContainingBlockConstraints,
+ ) -> Option<ChildRunShape>;
+}
+
pub(crate) struct ChildRunPlan {
pub(crate) fc_type: FormattingContextType,
pub(crate) root_containing_block: Node,
@@ -3032,6 +3052,23 @@ fn root_content_includes_an_orthogonal_flow_sized_by_the_root_block_size(
false
}
+fn percentage_inline_basis_observation_of_item_run(
+ run: &FormattingContextRun,
+ fc_type: FormattingContextType,
+ input: &LayoutInput,
+) -> PercentageInlineBasisObservation {
+ let item_is_block_container_whose_content_resolves_percentages_against_itself = fc_type
+ == FormattingContextType::Block
+ && run.layout_mode == LayoutMode::Normal
+ && parent_owns_root_input_cells(&run.callbacks, run.box_, input)
+ && !run.sizing().measurement_root_observes_percentage_inline_basis(run.box_);
+ if item_is_block_container_whose_content_resolves_percentages_against_itself {
+ PercentageInlineBasisObservation::Unobserved
+ } else {
+ PercentageInlineBasisObservation::Observed
+ }
+}
+
pub(super) fn content_block_size_adopted_after_run(
input: &LayoutInput,
automatic_content_block_size: CssPixels,
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 08974cea165..c966ae0c753 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -1316,7 +1316,7 @@ impl<'pass> GridFormattingContext<'pass> {
}
#[inline]
- fn sizing(&self) -> sizing_context::SizingContext<'pass> {
+ fn sizing(&self) -> sizing_context::SizingContext<'pass, 'static> {
sizing_context::SizingContext::new(self.run.records, self.run.callbacks)
}
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 2a2f3ea7caa..997be0079af 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -1306,7 +1306,7 @@ impl<'context> InlineFormattingContext<'context> {
}
#[inline]
- pub(crate) fn sizing(&self) -> sizing_context::SizingContext<'context> {
+ pub(crate) fn sizing(&self) -> sizing_context::SizingContext<'context, 'static> {
self.parent.sizing()
}
diff --git a/Libraries/LibWeb/Rust/src/layout/intrinsic_sizing.rs b/Libraries/LibWeb/Rust/src/layout/intrinsic_sizing.rs
index 6f04fd409e9..43eb164f7f9 100644
--- a/Libraries/LibWeb/Rust/src/layout/intrinsic_sizing.rs
+++ b/Libraries/LibWeb/Rust/src/layout/intrinsic_sizing.rs
@@ -263,7 +263,7 @@ impl BlockLevelContributions {
fn place_floats_among_children_of(
&mut self,
callbacks: &LayoutPass<'_>,
- sizing: &sizing_context::SizingContext<'_>,
+ sizing: &sizing_context::SizingContext<'_, '_>,
container: Node,
available_block_size: AvailableSize,
constraints: ContainingBlockConstraints,
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 33b2dfd8515..d88c8c30991 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -110,10 +110,11 @@ pub(crate) struct TableWrapperBlockSizes {
pub(crate) wrapper_content_block_size: CssPixels,
}
-pub(crate) struct SizingContext<'pass> {
+pub(crate) struct SizingContext<'pass, 'container> {
records: &'pass RunRecords<'pass>,
callbacks: LayoutPass<'pass>,
style_cache: Cell<Option<(Node, &'pass FfiStylePayloads)>>,
+ child_run_shapes: Option<&'container dyn formatting_context::ChildRunShapes>,
}
/// Whether a size is `auto` itself, rather than a calc-size() with an auto basis.
@@ -162,13 +163,24 @@ pub(crate) fn stretch_fit_block_margins(
(margin_top, margin_bottom)
}
-impl<'pass> SizingContext<'pass> {
+impl<'pass, 'container> SizingContext<'pass, 'container> {
#[inline]
pub(crate) fn new(records: &'pass RunRecords<'pass>, callbacks: LayoutPass<'pass>) -> Self {
Self {
records,
callbacks,
style_cache: Cell::new(None),
+ child_run_shapes: None,
+ }
+ }
+
+ pub(crate) fn with_child_run_shapes(
+ self,
+ child_run_shapes: &'container dyn formatting_context::ChildRunShapes,
+ ) -> Self {
+ Self {
+ child_run_shapes: Some(child_run_shapes),
+ ..self
}
}
@@ -3092,24 +3104,31 @@ impl<'pass> SizingContext<'pass> {
return cached.size;
}
- let _trace = self.callbacks.arena().layout_trace.intrinsic_size_miss(node, kind);
- let measurement = formatting_context::MeasurementState::create(self.callbacks);
- let root = measurement.create_used_values(node, constraints);
- root.block_size_constraint.set(size_constraint);
- root.has_definite_block_size.set(false);
- root.set_content_inline_size(inline_size);
- let result = measurement.run(
- node,
- &root,
- LayoutInput::new(
- AvailableSpace {
- inline_size: AvailableSize::definite(inline_size),
- block_size: available_block_size,
- },
- constraints,
- ParticipationInParentFormattingContext::Root,
- ),
- );
+ let result = if kind == IntrinsicSizeCacheKind::MaxContentBlock
+ && let Some(child_run_shapes) = self.child_run_shapes
+ && let Some(shape) = child_run_shapes.child_run_shape_at(node, inline_size, constraints)
+ {
+ formatting_context::lay_out_shape_for_its_result(self.callbacks, shape, LayoutMode::Normal)
+ } else {
+ let _trace = self.callbacks.arena().layout_trace.intrinsic_size_miss(node, kind);
+ let measurement = formatting_context::MeasurementState::create(self.callbacks);
+ let root = measurement.create_used_values(node, constraints);
+ root.block_size_constraint.set(size_constraint);
+ root.has_definite_block_size.set(false);
+ root.set_content_inline_size(inline_size);
+ measurement.run(
+ node,
+ &root,
+ LayoutInput::new(
+ AvailableSpace {
+ inline_size: AvailableSize::definite(inline_size),
+ block_size: available_block_size,
+ },
+ constraints,
+ ParticipationInParentFormattingContext::Root,
+ ),
+ )
+ };
let value = clamp_to_max_dimension_value(result.automatic_content_block_size);
self.charge_measurement_dependency_to_measured_box_and_containing_block(
node,
diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
index e19eac5e265..0709751bec8 100644
--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
@@ -1109,7 +1109,7 @@ impl<'pass> TableFormattingContext<'pass> {
}
#[inline]
- fn sizing(&self) -> sizing_context::SizingContext<'pass> {
+ fn sizing(&self) -> sizing_context::SizingContext<'pass, 'static> {
sizing_context::SizingContext::new(self.run.records, self.run.callbacks)
}
diff --git a/Tests/LibWeb/Text/expected/flex-item-measurement-adoption.txt b/Tests/LibWeb/Text/expected/flex-item-measurement-adoption.txt
new file mode 100644
index 00000000000..c8ba895a3c6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/flex-item-measurement-adoption.txt
@@ -0,0 +1,7 @@
+PASS: items that are not stretched commit the run their measurement laid out (work + checks)
+PASS: stretched block items commit the run their measurement laid out (work + checks)
+PASS: a stretched item with a percentage-height child lays it out again at its stretched size (work + checks)
+PASS: the next pass takes the block size of the stretched item with a percentage-height child from the size cache (work)
+PASS: a nested flex row is measured without layout runs to adopt, so its items are laid out when it commits (work + checks)
+PASS: a flex row laid out before its width changes (work)
+PASS: the next pass at another width reuses the item whose width did not change and adopts the one that did (work + checks)
diff --git a/Tests/LibWeb/Text/expected/flex-item-measurement-quirks-mode.txt b/Tests/LibWeb/Text/expected/flex-item-measurement-quirks-mode.txt
new file mode 100644
index 00000000000..d231f6904c5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/flex-item-measurement-quirks-mode.txt
@@ -0,0 +1,3 @@
+quirks mode: true
+column flex item and its min-height: 50% child: 20 20
+stretched row flex item and its min-height: 50% child: 300 150
diff --git a/Tests/LibWeb/Text/input/flex-item-measurement-adoption.html b/Tests/LibWeb/Text/input/flex-item-measurement-adoption.html
new file mode 100644
index 00000000000..153f12886f9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/flex-item-measurement-adoption.html
@@ -0,0 +1,160 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script src="layout-test.js"></script>
+<style>
+ body { margin: 0; font: 20px/20px SerenitySans; }
+ #host { width: 400px; }
+ .row { display: flex; }
+ .start { align-items: flex-start; }
+ .fixed { width: 120px; }
+ .flexible { flex: 1; min-width: 0; }
+ .half { height: 50%; }
+</style>
+<div id="host"></div>
+<script>
+ const height = id => document.getElementById(id).getBoundingClientRect().height;
+ layoutTest({
+ steps: [
+ {
+ name: "items that are not stretched commit the run their measurement laid out",
+ mutate: () => {
+ host.innerHTML = `<div class="row start" id="notStretched"><div class="fixed" id="shortItem">one line</div><div class="fixed" id="tallItem">some words that wrap into lines</div></div>`;
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#notStretched.row.start/flex RUN (cache=miss)
+ BlockContainer<DIV>#shortItem.fixed/block RUN (cache=miss)
+ BlockContainer<DIV>#tallItem.fixed/block RUN (cache=miss)
+ BlockContainer<DIV>#shortItem.fixed/block REPLAY FRAGMENTS
+ BlockContainer<DIV>#tallItem.fixed/block REPLAY FRAGMENTS
+ `,
+ check: equal => {
+ equal(height("shortItem"), 20, "short item height");
+ equal(height("tallItem"), 80, "tall item height");
+ },
+ },
+ {
+ name: "stretched block items commit the run their measurement laid out",
+ mutate: () => {
+ host.innerHTML = `<div class="row" id="stretched"><div class="fixed" id="stretchedShort">one line</div><div class="fixed" id="stretchedTall">some words that wrap into lines</div></div>`;
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#stretched.row/flex RUN (cache=miss)
+ BlockContainer<DIV>#stretchedShort.fixed/block RUN (cache=miss)
+ BlockContainer<DIV>#stretchedTall.fixed/block RUN (cache=miss)
+ BlockContainer<DIV>#stretchedShort.fixed/block REPLAY FRAGMENTS
+ BlockContainer<DIV>#stretchedTall.fixed/block REPLAY FRAGMENTS
+ `,
+ check: equal => {
+ equal(height("stretchedShort"), 80, "stretched short item height");
+ equal(height("stretchedTall"), 80, "stretched tall item height");
+ },
+ },
+ {
+ name: "a stretched item with a percentage-height child lays it out again at its stretched size",
+ mutate: () => {
+ host.innerHTML = `<div class="row" id="percentage"><div class="fixed" id="percentageItem"><div class="half" id="halfChild">one line</div></div><div class="fixed" id="percentageSibling">some words that wrap into lines</div></div>`;
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#percentage.row/flex RUN (cache=miss)
+ BlockContainer<DIV>#percentageItem.fixed/block RUN (cache=miss)
+ BlockContainer<DIV>#percentageSibling.fixed/block RUN (cache=miss)
+ BlockContainer<DIV>#percentageItem.fixed/block RUN (cache=miss)
+ BlockContainer<DIV>#percentageSibling.fixed/block REPLAY FRAGMENTS
+ `,
+ check: equal => {
+ equal(height("percentageItem"), 80, "percentage item height");
+ equal(height("halfChild"), 40, "percentage child height");
+ },
+ },
+ {
+ name: "the next pass takes the block size of the stretched item with a percentage-height child from the size cache",
+ mutate: () => {
+ document.getElementById("percentage").style.paddingTop = "1px";
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#percentage.row/flex RUN (cache=miss)
+ BlockContainer<DIV>#percentageItem.fixed MEASURE max-content block (cache=hit)
+ BlockContainer<DIV>#percentageSibling.fixed MEASURE max-content block (cache=hit)
+ BlockContainer<DIV>#percentageItem.fixed/block REUSE SUBTREE
+ BlockContainer<DIV>#percentageSibling.fixed/block REUSE SUBTREE
+ `,
+ },
+ {
+ name: "a nested flex row is measured without layout runs to adopt, so its items are laid out when it commits",
+ mutate: () => {
+ host.innerHTML = `<div class="row" id="outer"><div class="row" id="inner" style="width: 250px"><div class="fixed" id="innerShort">one line</div><div class="fixed" id="innerTall">some words that wrap into lines</div></div></div>`;
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#outer.row/flex RUN (cache=miss)
+ Box<DIV>#inner.row MEASURE max-content block (cache=miss)
+ Box<DIV>#inner.row/flex (intrinsic) RUN (cache=bypass)
+ BlockContainer<DIV>#innerShort.fixed MEASURE max-content block (cache=miss)
+ BlockContainer<DIV>#innerShort.fixed/block (intrinsic) RUN (cache=bypass)
+ BlockContainer<DIV>#innerTall.fixed MEASURE max-content block (cache=miss)
+ BlockContainer<DIV>#innerTall.fixed/block (intrinsic) RUN (cache=bypass)
+ BlockContainer<DIV>#innerShort.fixed MEASURE max-content block (cache=hit)
+ BlockContainer<DIV>#innerTall.fixed MEASURE max-content block (cache=hit)
+ BlockContainer<DIV>#innerShort.fixed MEASURE max-content block (cache=hit)
+ BlockContainer<DIV>#innerTall.fixed MEASURE max-content block (cache=hit)
+ Box<DIV>#inner.row/flex RUN (cache=miss)
+ BlockContainer<DIV>#innerShort.fixed/block RUN (cache=miss)
+ BlockContainer<DIV>#innerTall.fixed/block RUN (cache=miss)
+ `,
+ check: equal => {
+ equal(height("inner"), 80, "inner flex row height");
+ equal(height("innerShort"), 80, "inner short item height");
+ },
+ },
+ {
+ name: "a flex row laid out before its width changes",
+ mutate: () => {
+ host.innerHTML = `<div class="row start" id="resized"><div class="fixed" id="resizedFixed">one line</div><div class="flexible" id="resizedFlexible">some words that wrap into lines</div></div>`;
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#resized.row.start/flex RUN (cache=miss)
+ BlockContainer<DIV>#resizedFixed.fixed/block RUN (cache=miss)
+ BlockContainer<DIV>#resizedFlexible.flexible/block RUN (cache=miss)
+ BlockContainer<DIV>#resizedFixed.fixed/block REPLAY FRAGMENTS
+ BlockContainer<DIV>#resizedFlexible.flexible/block REPLAY FRAGMENTS
+ `,
+ },
+ {
+ name: "the next pass at another width reuses the item whose width did not change and adopts the one that did",
+ mutate: () => {
+ host.style.width = "300px";
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#resized.row.start/flex RUN (cache=miss)
+ BlockContainer<DIV>#resizedFixed.fixed MEASURE max-content block (cache=hit)
+ BlockContainer<DIV>#resizedFlexible.flexible/block RUN (cache=miss)
+ BlockContainer<DIV>#resizedFixed.fixed/block REUSE SUBTREE
+ BlockContainer<DIV>#resizedFlexible.flexible/block REPLAY FRAGMENTS
+ `,
+ check: equal => {
+ equal(height("resizedFixed"), 20, "fixed item height");
+ },
+ },
+ ],
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/flex-item-measurement-quirks-mode.html b/Tests/LibWeb/Text/input/flex-item-measurement-quirks-mode.html
new file mode 100644
index 00000000000..eb0d248356c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/flex-item-measurement-quirks-mode.html
@@ -0,0 +1,18 @@
+<!DOCTYPE quirks>
+<script src="include.js"></script>
+<style>
+ body { margin: 0; font: 20px/20px SerenitySans; }
+ .column { display: flex; flex-direction: column; height: 300px; width: 400px; }
+ .row { display: flex; height: 300px; width: 400px; }
+ .line { height: 20px; }
+</style>
+<div class="column"><div id="columnItem"><div id="columnItemChild" style="min-height: 50%"><div class="line"></div></div></div></div>
+<div class="row"><div id="stretchedItem"><div id="stretchedItemChild" style="min-height: 50%"><div class="line"></div></div></div></div>
+<script>
+ test(() => {
+ const rect = id => document.getElementById(id).getBoundingClientRect();
+ println(`quirks mode: ${document.compatMode === "BackCompat"}`);
+ println(`column flex item and its min-height: 50% child: ${rect("columnItem").height} ${rect("columnItemChild").height}`);
+ println(`stretched row flex item and its min-height: 50% child: ${rect("stretchedItem").height} ${rect("stretchedItemChild").height}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/layout-run-cache-anonymous-flex-item-forwards-percentage-height.html b/Tests/LibWeb/Text/input/layout-run-cache-anonymous-flex-item-forwards-percentage-height.html
index 82cf1119012..1abd2b6fdfe 100644
--- a/Tests/LibWeb/Text/input/layout-run-cache-anonymous-flex-item-forwards-percentage-height.html
+++ b/Tests/LibWeb/Text/input/layout-run-cache-anonymous-flex-item-forwards-percentage-height.html
@@ -29,10 +29,9 @@
Box<DIV>#flex/flex RUN (cache=miss)
BlockContainer(anonymous) MEASURE max-content inline (cache=miss) FROM ITEMS
BlockContainer(anonymous) MEASURE min-content inline (cache=hit)
- BlockContainer(anonymous) MEASURE max-content block (cache=miss)
- BlockContainer(anonymous)/block (intrinsic) RUN (cache=bypass)
- BlockContainer<DIV>#plain MEASURE max-content block (cache=hit)
BlockContainer(anonymous)/block RUN (cache=miss)
+ BlockContainer<DIV>#plain MEASURE max-content block (cache=hit)
+ BlockContainer(anonymous)/block REPLAY FRAGMENTS
BlockContainer<SPAN>#inline-block/block RUN (cache=miss)
BlockContainer<DIV>#plain/block REUSE SUBTREE
`,