Skip to content

Truly independent web browser

LibWeb: Return the extent of flex and grid items with their run's result

Aliaksandr Kalenik26d8d95f14a2
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index c58266be2ef..23b0abe4504 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -2287,11 +2287,19 @@ impl AbsposEngine<'_> {
}
}
+/// The block sizes the run of an absolutely positioned box found for its contents.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) struct InsideLayoutBlockSizes {
+ pub(crate) automatic_content_block_size: CssPixels,
+ /// For a box with a preferred aspect ratio, the block size of its laid out content.
+ pub(crate) content_block_size_for_aspect_ratio_minimum: Option<CssPixels>,
+}
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum BlockSizePass {
BeforeInsideLayout,
AfterInsideLayout {
- automatic_content_block_size_of_inside_layout: Option<CssPixels>,
+ inside_layout: Option<InsideLayoutBlockSizes>,
},
}
@@ -2366,10 +2374,10 @@ impl AbsposEngine<'_> {
return Some(size);
}
if let BlockSizePass::AfterInsideLayout {
- automatic_content_block_size_of_inside_layout: Some(automatic_content_block_size_of_inside_layout),
+ inside_layout: Some(inside_layout),
} = pass
{
- return Some(automatic_content_block_size_of_inside_layout);
+ return Some(inside_layout.automatic_content_block_size);
}
if self.facts(node).creates_block_formatting_context() {
return match pass {
@@ -2695,9 +2703,7 @@ impl AbsposEngine<'_> {
constraints,
)
};
- if let BlockSizePass::AfterInsideLayout {
- automatic_content_block_size_of_inside_layout,
- } = pass
+ if let BlockSizePass::AfterInsideLayout { inside_layout } = pass
&& self.sizing().block_size_is_ratio_dependent(
self.sized_record(node).sized_box(),
available_space,
@@ -2708,14 +2714,8 @@ impl AbsposEngine<'_> {
// NB: A box that is only sized like a replaced element through its aspect ratio grows to fit its content.
// INTEROP: CSS Positioned Layout says "The automatic minimum size of an absolutely-positioned box is always
// zero", but Chrome applies the aspect-ratio minimum to absolutely positioned boxes too.
- let content_block_size = automatic_content_block_size_of_inside_layout.and_then(|size| {
- formatting_context::content_block_size_for_aspect_ratio_minimum(
- self.records,
- &self.callbacks,
- node,
- size,
- )
- });
+ let content_block_size =
+ inside_layout.and_then(|inside_layout| inside_layout.content_block_size_for_aspect_ratio_minimum);
if let Some(minimum) = self.sizing().automatic_minimum_size_from_aspect_ratio(
self.sized_record(node).sized_box(),
SizingAxis::Block,
@@ -2905,7 +2905,7 @@ impl<'pass> AbsposEngine<'pass> {
&self,
node: Node,
inputs: abspos_inputs::AbsposLayoutInputs,
- automatic_content_block_size_of_inside_layout: Option<CssPixels>,
+ inside_layout: Option<InsideLayoutBlockSizes>,
) {
self.install_resolved_anchor_sizes(node, &inputs);
let (available_space, constraints) = out_of_flow_root_space(inputs);
@@ -2921,9 +2921,7 @@ impl<'pass> AbsposEngine<'pass> {
available_space,
constraints,
static_position_rect_for_sizing(&inputs),
- BlockSizePass::AfterInsideLayout {
- automatic_content_block_size_of_inside_layout,
- },
+ BlockSizePass::AfterInsideLayout { inside_layout },
resolved,
);
}
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 4f0a1012745..99c8df8b1a4 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -209,6 +209,7 @@ pub(super) struct FlexFormattingContext<'pass> {
item_percentage_bases: ContainingBlockConstraints,
item_runs_offered_to_block_size_measurements: Option<ItemRunsOfferedToBlockSizeMeasurements>,
next_flex_item_lookup_start: Cell<usize>,
+ in_flow_items_margin_box_block_extent: Option<CssPixels>,
}
impl formatting_context::ChildRunShapes for FlexFormattingContext<'_> {
@@ -254,6 +255,7 @@ impl<'pass> FlexFormattingContext<'pass> {
item_percentage_bases: ContainingBlockConstraints::default(),
item_runs_offered_to_block_size_measurements: None,
next_flex_item_lookup_start: Cell::new(0),
+ in_flow_items_margin_box_block_extent: None,
}
}
@@ -4002,6 +4004,9 @@ impl<'pass> FlexFormattingContext<'pass> {
fragment_arrival,
);
}
+ if self.facts(self.run.box_).has_preferred_aspect_ratio() {
+ self.in_flow_items_margin_box_block_extent = self.margin_box_block_extent_of_placed_in_flow_items();
+ }
self.derived_baselines_of_root_box = DerivedBaselines {
inline_alignment: self.flex_lines.len().checked_sub(1).and_then(|line_index| {
self.baseline_of_line_for_purpose(
@@ -4035,6 +4040,31 @@ impl<'pass> FlexFormattingContext<'pass> {
}
}
+ /// The block size of the margin boxes of the container's in-flow children, once its items are placed, unless a box
+ /// among them is not an item.
+ fn margin_box_block_extent_of_placed_in_flow_items(&self) -> Option<CssPixels> {
+ let mut block_start = CssPixels::default();
+ let mut block_end = CssPixels::default();
+ for child in self.run.callbacks.children(self.run.box_) {
+ let facts = self.facts(child);
+ if facts.is_box() && !facts.is_absolutely_positioned() {
+ let index = self.index_of_flex_item(child)?;
+ let metrics = &self.flex_items[index].cells;
+ let content_block_offset = self.item_content_offset(index).y;
+ let collapsed = metrics.uses_collapsing_borders_model();
+ block_start = block_start.min(content_block_offset - metrics.margin_box_top(collapsed));
+ block_end = block_end
+ .max(content_block_offset + metrics.content_block_size() + metrics.margin_box_bottom(collapsed));
+ }
+ }
+ Some(block_end - block_start)
+ }
+
+ /// For a container with a preferred aspect ratio, the block size of the margin boxes of its in-flow children.
+ pub(super) fn in_flow_items_margin_box_block_extent(&self) -> Option<CssPixels> {
+ self.in_flow_items_margin_box_block_extent
+ }
+
// https://drafts.csswg.org/css-flexbox-1/#flex-baselines
fn baseline_of_line(&self, line_index: usize, baseline_set: formatting_context::BaselineSet) -> Option<CssPixels> {
self.baseline_of_line_for_purpose(line_index, baseline_set, formatting_context::BaselinePurpose::Container)
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 19fb2c0f51a..84dddc4b8c6 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1197,6 +1197,9 @@ pub(crate) struct ChildLayoutResult {
pub table_box_in_wrapper_border_box_block_size: Option<CssPixels>,
/// For a box with a preferred aspect ratio, the block size of its laid out content.
pub content_block_size_for_aspect_ratio_minimum: Option<CssPixels>,
+ /// For a flex or grid container with a preferred aspect ratio, the block size of the margin boxes of its in-flow
+ /// children once they are placed, unless one of them is not an item.
+ pub in_flow_children_margin_box_block_extent: Option<CssPixels>,
pub depends_on_percentage_block_size: bool,
/// For an absolutely positioned box, the used insets its run gave it.
pub out_of_flow_root_used_insets: Option<run_records::UsedInsets>,
@@ -1990,11 +1993,11 @@ fn lays_out_content_against_min_block_size(facts: NodeFacts<'_>, fc_type: Format
/// The content block size of a laid out box, for growing a block size transferred through its aspect ratio. Chrome
/// derives it from the layout at the transferred size, so percentages inside resolve against that size.
pub(crate) fn content_block_size_for_aspect_ratio_minimum(
- records: &RunRecords<'_>,
callbacks: &LayoutPass<'_>,
node: Node,
- automatic_content_block_size: CssPixels,
+ result: &ChildLayoutResult,
) -> Option<CssPixels> {
+ let automatic_content_block_size = result.automatic_content_block_size;
let facts = NodeFacts::new(callbacks, node);
if facts.node_has_size_containment() {
// NB: The automatic content block size of a size containment box already ignores its content.
@@ -2008,33 +2011,12 @@ pub(crate) fn content_block_size_for_aspect_ratio_minimum(
Some(FormattingContextType::Block) => Some(automatic_content_block_size),
// NB: These report their own block size, so take the extent of their laid out children instead.
Some(FormattingContextType::Flex | FormattingContextType::Grid) => {
- in_flow_children_margin_box_block_extent(records, callbacks, node)
+ result.in_flow_children_margin_box_block_extent
}
_ => None,
}
}
-fn in_flow_children_margin_box_block_extent(
- records: &RunRecords<'_>,
- callbacks: &LayoutPass<'_>,
- node: Node,
-) -> Option<CssPixels> {
- let mut block_start = CssPixels::default();
- let mut block_end = CssPixels::default();
- for child in callbacks.children(node) {
- let facts = NodeFacts::new(callbacks, child);
- if facts.is_box() && !facts.is_absolutely_positioned() {
- let metrics = records.box_metrics_of_if_owned(child)?;
- let content_block_offset = records.placement_of(child)?.content_offset_in_containing_block.y;
- let collapsed = metrics.uses_collapsing_borders_model();
- block_start = block_start.min(content_block_offset - metrics.margin_box_top(collapsed));
- block_end = block_end
- .max(content_block_offset + metrics.content_block_size() + metrics.margin_box_bottom(collapsed));
- }
- }
- Some(block_end - block_start)
-}
-
fn finalize_block_level_root(run: &FormattingContextRun, input: &LayoutInput, body_result: &ChildLayoutResult) {
let sized_root = run.sized_root();
let node = run.box_;
@@ -2571,6 +2553,7 @@ fn lay_out_run_root_and_contents<'pass>(
ChildLayoutResult {
automatic_content_block_size: context.automatic_content_block_size(),
baselines,
+ in_flow_children_margin_box_block_extent: context.in_flow_items_margin_box_block_extent(),
..ChildLayoutResult::default()
}
}
@@ -2580,6 +2563,7 @@ fn lay_out_run_root_and_contents<'pass>(
ChildLayoutResult {
automatic_content_block_size: context.automatic_content_block_size(),
baselines,
+ in_flow_children_margin_box_block_extent: context.in_flow_items_margin_box_block_extent(),
..ChildLayoutResult::default()
}
}
@@ -2643,12 +2627,8 @@ fn lay_out_run_root_and_contents<'pass>(
}
if containment_facts.has_preferred_aspect_ratio() {
- result.content_block_size_for_aspect_ratio_minimum = content_block_size_for_aspect_ratio_minimum(
- run.records,
- &run.callbacks,
- run.box_,
- result.automatic_content_block_size,
- );
+ result.content_block_size_for_aspect_ratio_minimum =
+ content_block_size_for_aspect_ratio_minimum(&run.callbacks, run.box_, &result);
}
match input.participation {
@@ -2665,7 +2645,10 @@ fn lay_out_run_root_and_contents<'pass>(
abspos_engine::AbsposEngine::for_run(run).finalize_out_of_flow_root_after_inside_layout(
run.box_,
abspos_inputs,
- Some(result.automatic_content_block_size),
+ Some(abspos_engine::InsideLayoutBlockSizes {
+ automatic_content_block_size: result.automatic_content_block_size,
+ content_block_size_for_aspect_ratio_minimum: result.content_block_size_for_aspect_ratio_minimum,
+ }),
);
result.out_of_flow_root_used_insets = Some(run.records.used_insets_of(run.box_));
}
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index c39f5e4178b..d21928e9fac 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -1110,6 +1110,7 @@ pub(crate) struct GridFormattingContext<'pass> {
cells_of_items: HashMap<Node, Cell<used_values::RootInputCells>>,
/// What this grid keeps of each item it laid out.
laid_out_items: RefCell<HashMap<Node, LaidOutGridItem>>,
+ in_flow_items_margin_box_block_extent: Option<CssPixels>,
explicit_column_line_count: usize,
explicit_row_line_count: usize,
explicit_column_start: usize,
@@ -1297,6 +1298,7 @@ impl<'pass> GridFormattingContext<'pass> {
next_item_lookup_start: Cell::new(0),
cells_of_items: HashMap::default(),
laid_out_items: RefCell::new(HashMap::default()),
+ in_flow_items_margin_box_block_extent: None,
lanes: None,
}
}
@@ -4529,6 +4531,9 @@ impl<'pass> GridFormattingContext<'pass> {
self.place_item(item, placed_item, offset, fragment_arrival);
}
self.place_baseline_aligned_items(baseline_aligned_items);
+ if self.facts(self.run.box_).has_preferred_aspect_ratio() {
+ self.in_flow_items_margin_box_block_extent = self.margin_box_block_extent_of_placed_in_flow_items();
+ }
self.derived_baselines_of_root_box = DerivedBaselines {
inline_alignment: None,
first: self.baseline_of_items(formatting_context::BaselineSet::First),
@@ -4740,6 +4745,30 @@ impl<'pass> GridFormattingContext<'pass> {
)
}
+ /// The block size of the margin boxes of the container's in-flow children, once its items are placed, unless a box
+ /// among them is not an item.
+ fn margin_box_block_extent_of_placed_in_flow_items(&self) -> Option<CssPixels> {
+ let mut block_start = CssPixels::default();
+ let mut block_end = CssPixels::default();
+ for child in self.run.callbacks.children(self.run.box_) {
+ let facts = self.facts(child);
+ if facts.is_box() && !facts.is_absolutely_positioned() {
+ let metrics = self.cells_of_items.get(&child)?.get();
+ let content_block_offset = self.laid_out_items.borrow().get(&child)?.content_offset?.y;
+ let collapsed = metrics.uses_collapsing_borders_model();
+ block_start = block_start.min(content_block_offset - metrics.margin_box_top(collapsed));
+ block_end = block_end
+ .max(content_block_offset + metrics.content_block_size() + metrics.margin_box_bottom(collapsed));
+ }
+ }
+ Some(block_end - block_start)
+ }
+
+ /// For a container with a preferred aspect ratio, the block size of the margin boxes of its in-flow children.
+ pub(crate) fn in_flow_items_margin_box_block_extent(&self) -> Option<CssPixels> {
+ self.in_flow_items_margin_box_block_extent
+ }
+
// https://drafts.csswg.org/css-grid-2/#grid-baselines
fn baseline_of_items(&self, baseline_set: formatting_context::BaselineSet) -> Option<CssPixels> {
if let Some(lanes) = self.lanes