Skip to content

Truly independent web browser

LibWeb: Derive a block container's baselines as its children return

Aliaksandr Kalenik9d8b64dab642
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index b64389c32c9..1f41ea80276 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -49,6 +49,8 @@ struct BlockLevelBoxOutcome {
collapses_through: bool,
can_break_inside: bool,
broken_inside: Option<BrokenInside>,
+ /// The baselines the box gives its containing block, relative to the containing block's content box.
+ baselines_in_containing_block: DerivedBaselines,
}
/// A block-level box that continues in the next fragmentainer.
@@ -708,8 +710,22 @@ impl<'pass> BlockFormattingContext<'pass> {
);
}
- fn derive_baselines_of_laid_out_contents(&self, block_container: Node) -> DerivedBaselines {
- formatting_context::derive_baselines(self.run.records, &self.run.callbacks, block_container)
+ /// Folds what `block_container` takes from `child` into the summary of the children laid out before it.
+ fn fold_in_child(
+ &self,
+ summary: &mut ChildrenSummary,
+ block_container: Node,
+ child: Node,
+ outcome: Option<&BlockLevelBoxOutcome>,
+ ) {
+ let Some(outcome) = outcome else {
+ return;
+ };
+ if formatting_context::child_gives_container_baselines(&self.run.callbacks, block_container, child) {
+ summary
+ .baselines
+ .fold_in_later_child(outcome.baselines_in_containing_block);
+ }
}
fn record_derived_baselines_of_root_box(&self, baselines: DerivedBaselines) {
@@ -2027,6 +2043,11 @@ impl<'pass> BlockFormattingContext<'pass> {
token,
block_end_of_content: self.block_end_of_content(node),
}),
+ baselines_in_containing_block: formatting_context::baselines_child_gives_container(
+ self.run.records,
+ &self.run.callbacks,
+ node,
+ ),
})
}
@@ -2954,6 +2975,7 @@ impl<'pass> BlockFormattingContext<'pass> {
.and_then(|state| state.resume.as_ref()?.resume_child())
.unwrap_or_else(|| self.run.callbacks.first_child(block_container));
let mut laid_out_children: Option<(Node, Node)> = None;
+ let mut summary = ChildrenSummary::default();
for child in self
.run
.callbacks
@@ -2983,6 +3005,7 @@ impl<'pass> BlockFormattingContext<'pass> {
if invisible {
self.laying_out_invisible_line_clamp_content.set(false);
}
+ self.fold_in_child(&mut summary, block_container, child, outcome.as_ref());
if let (Some(fragmentainer), Some(state)) = (fragmentainer.as_deref(), fragmentation_state.as_mut())
&& self.fragmentation_step_after_child(fragmentainer, state, block_container, child, outcome.as_ref())
== FragmentationStep::Stop
@@ -3001,11 +3024,8 @@ impl<'pass> BlockFormattingContext<'pass> {
.remove(&block_container);
}
self.block_offset_of_current_block_container.set(saved);
- ChildrenSummary {
- baselines: self.derive_baselines_of_laid_out_contents(block_container),
- fragmentainer_stop_token: fragmentation_state.and_then(|state| state.container_stop_token),
- lines_are_unbreakable: false,
- }
+ summary.fragmentainer_stop_token = fragmentation_state.and_then(|state| state.container_stop_token);
+ summary
}
fn fragmentation_step_before_child(
@@ -3960,7 +3980,7 @@ impl<'pass> BlockFormattingContext<'pass> {
run: &FormattingContextRun<'pass>,
input: LayoutInput,
available_space_for_children: AvailableSpace,
- ) -> DerivedBaselines {
+ ) -> ChildrenSummary {
let wrapper = self.run.box_;
// The table wrapper is invisible to percentage resolution: percentages on the table root
// resolve against the wrapper's containing block, so the wrapper's own constraints pass
@@ -3974,6 +3994,7 @@ impl<'pass> BlockFormattingContext<'pass> {
let saved = self
.block_offset_of_current_block_container
.replace(Some(CssPixels::default()));
+ let mut summary = ChildrenSummary::default();
for child in table_wrapper_flow_children(self.run.callbacks, wrapper) {
let child_input = if self.style(child).display().is_table_inside() {
table_box_input
@@ -3984,7 +4005,7 @@ impl<'pass> BlockFormattingContext<'pass> {
if invisible {
self.laying_out_invisible_line_clamp_content.set(true);
}
- self.layout_block_level_box(
+ let outcome = self.layout_block_level_box(
run,
child,
wrapper,
@@ -3996,9 +4017,10 @@ impl<'pass> BlockFormattingContext<'pass> {
if invisible {
self.laying_out_invisible_line_clamp_content.set(false);
}
+ self.fold_in_child(&mut summary, wrapper, child, outcome.as_ref());
}
self.block_offset_of_current_block_container.set(saved);
- self.derive_baselines_of_laid_out_contents(wrapper)
+ summary
}
// https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements
@@ -4007,7 +4029,7 @@ impl<'pass> BlockFormattingContext<'pass> {
run: &FormattingContextRun<'pass>,
fieldset: Node,
input: LayoutInput,
- ) -> DerivedBaselines {
+ ) -> ChildrenSummary {
let available_space = input.available_space;
let child_input = self.child_layout_input(fieldset, input, available_space);
let legend = self.facts(fieldset).rendered_legend();
@@ -4043,6 +4065,7 @@ impl<'pass> BlockFormattingContext<'pass> {
// Lay out non-legend children below the legend accommodation.
self.margin_state.borrow_mut().reset();
+ let mut summary = ChildrenSummary::default();
{
let saved = self.block_offset_of_current_block_container.replace(Some(extra_top));
for child in self.children(fieldset) {
@@ -4051,7 +4074,7 @@ impl<'pass> BlockFormattingContext<'pass> {
if invisible {
self.laying_out_invisible_line_clamp_content.set(true);
}
- self.layout_block_level_box(
+ let outcome = self.layout_block_level_box(
run,
child,
fieldset,
@@ -4063,6 +4086,7 @@ impl<'pass> BlockFormattingContext<'pass> {
if invisible {
self.laying_out_invisible_line_clamp_content.set(false);
}
+ self.fold_in_child(&mut summary, fieldset, child, outcome.as_ref());
}
}
self.block_offset_of_current_block_container.set(saved);
@@ -4096,7 +4120,7 @@ impl<'pass> BlockFormattingContext<'pass> {
},
);
}
- self.derive_baselines_of_laid_out_contents(fieldset)
+ summary
}
pub(crate) fn run(&self, run: &FormattingContextRun<'pass>, input: LayoutInput) {
@@ -4152,11 +4176,13 @@ impl<'pass> BlockFormattingContext<'pass> {
let root_input = input;
let root_facts = self.facts(self.run.box_);
if root_facts.is_fieldset_box() && !root_facts.rendered_legend().is_invalid() {
- let baselines = self.layout_fieldset_with_rendered_legend(run, self.run.box_, root_input);
+ let baselines = self
+ .layout_fieldset_with_rendered_legend(run, self.run.box_, root_input)
+ .baselines;
self.record_derived_baselines_of_root_box(baselines);
return;
}
- let baselines = if root_facts.is_table_wrapper() {
+ let children_summary = if root_facts.is_table_wrapper() {
self.layout_table_wrapper_children(run, root_input, available_space)
} else if root_facts.children_are_inline() {
self.layout_inline_children(
@@ -4166,7 +4192,6 @@ impl<'pass> BlockFormattingContext<'pass> {
FfiCssPixelPoint::default(),
available_space,
)
- .baselines
} else {
self.layout_block_level_children(
run,
@@ -4175,8 +4200,8 @@ impl<'pass> BlockFormattingContext<'pass> {
FfiCssPixelPoint::default(),
available_space,
)
- .baselines
};
+ let baselines = children_summary.baselines;
self.record_derived_baselines_of_root_box(baselines);
// Fieldsets without a rendered legend skip collapsed margin assignment.
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index e943a2f6a52..3d614219b93 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -805,8 +805,12 @@ pub(crate) fn store_derived_baselines(used: &UsedValues, baselines: DerivedBasel
}
}
-pub(crate) fn derive_baselines(records: &RunRecords, callbacks: &LayoutPass<'_>, box_: Node) -> DerivedBaselines {
- let facts = NodeFacts::new(callbacks, box_);
+/// The baselines of the lines laid out in `box_`, relative to its content box.
+pub(crate) fn derive_baselines_from_lines(
+ records: &RunRecords,
+ callbacks: &LayoutPass<'_>,
+ box_: Node,
+) -> DerivedBaselines {
let own_used = records.used_values(box_);
let own_line_data = own_used.line_data_ref();
let mut lines = own_line_data.iter().flat_map(|data| data.lines());
@@ -856,57 +860,55 @@ pub(crate) fn derive_baselines(records: &RunRecords, callbacks: &LayoutPass<'_>,
};
}
- if callbacks.first_child(box_).is_invalid() || facts.children_are_inline() {
- return DerivedBaselines::default();
+ DerivedBaselines::default()
+}
+
+impl DerivedBaselines {
+ /// Folds in the baselines a child laid out after the children folded in so far gives its container. A block
+ /// container with block-level children takes its first baseline from the first in-flow child that has one, and its
+ /// last baselines from the last.
+ pub(crate) fn fold_in_later_child(&mut self, child: DerivedBaselines) {
+ self.first = self.first.or(child.first);
+ self.last = child.last.or(self.last);
+ self.inline_alignment = child.inline_alignment.or(self.inline_alignment);
}
+}
- // Derive baselines from the first/last in-flow child that has a baseline set of its own.
+/// Whether `container`, a block container with block-level children, derives its baselines from those of `child`.
+pub(crate) fn child_gives_container_baselines(callbacks: &LayoutPass<'_>, container: Node, child: Node) -> bool {
+ let facts = NodeFacts::new(callbacks, container);
+ let child_facts = NodeFacts::new(callbacks, child);
// NB: Flex and grid containers derive their baselines from their items instead.
- let container_display = facts.display();
- let baseline_from_children = |baseline_set: BaselineSet, purpose: BaselinePurpose| -> Option<CssPixels> {
- let mut next_child = match baseline_set {
- BaselineSet::First => callbacks.first_child(box_),
- BaselineSet::Last => callbacks.last_child(box_),
- };
- while !next_child.is_invalid() {
- let child = next_child;
- next_child = match baseline_set {
- BaselineSet::First => callbacks.next_sibling(child),
- BaselineSet::Last => callbacks.previous_sibling(child),
- };
- let child_facts = NodeFacts::new(callbacks, child);
- if !child_facts.is_flow_layout_participant() || child_facts.is_floating() {
- continue;
- }
- if facts.is_fieldset_box() && facts.rendered_legend() == child {
- continue;
- }
- if !table_formatting_context::child_participates_in_table_run(container_display, &child_facts) {
- continue;
- }
- // A table wrapper box has the baselines of its table box, not of a caption above it: "The baseline of an
- // 'inline-table' is the baseline of the first row of the table."
- // https://www.w3.org/TR/CSS22/tables.html#height-layout
- if facts.is_table_wrapper() && !child_facts.display().is_table_inside() {
- continue;
- }
- if let Some(baseline) = baseline_of_child_for_purpose(
- records,
- callbacks,
- child,
- baseline_set,
- ChildBaselineSource::Own,
- purpose,
- ) {
- return Some(baseline);
- }
- }
- None
+ child_facts.is_flow_layout_participant()
+ && !child_facts.is_floating()
+ && !(facts.is_fieldset_box() && facts.rendered_legend() == child)
+ && table_formatting_context::child_participates_in_table_run(facts.display(), &child_facts)
+ // A table wrapper box has the baselines of its table box, not of a caption above it: "The baseline of an
+ // 'inline-table' is the baseline of the first row of the table."
+ // https://www.w3.org/TR/CSS22/tables.html#height-layout
+ && !(facts.is_table_wrapper() && !child_facts.display().is_table_inside())
+}
+
+/// The baselines a laid out child gives its container, relative to the container's content box.
+pub(crate) fn baselines_child_gives_container(
+ records: &RunRecords,
+ callbacks: &LayoutPass<'_>,
+ child: Node,
+) -> DerivedBaselines {
+ let baseline = |baseline_set, purpose| {
+ baseline_of_child_for_purpose(
+ records,
+ callbacks,
+ child,
+ baseline_set,
+ ChildBaselineSource::Own,
+ purpose,
+ )
};
DerivedBaselines {
- first: baseline_from_children(BaselineSet::First, BaselinePurpose::Container),
- last: baseline_from_children(BaselineSet::Last, BaselinePurpose::Container),
- inline_alignment: baseline_from_children(BaselineSet::Last, BaselinePurpose::InlineAlignment),
+ first: baseline(BaselineSet::First, BaselinePurpose::Container),
+ last: baseline(BaselineSet::Last, BaselinePurpose::Container),
+ inline_alignment: baseline(BaselineSet::Last, BaselinePurpose::InlineAlignment),
}
}
@@ -3795,7 +3797,9 @@ mod baseline_tests {
inline_alignment: None,
},
);
- let baselines = derive_baselines(records, &callbacks, parent);
+ assert!(child_gives_container_baselines(&callbacks, parent, child));
+ let mut baselines = DerivedBaselines::default();
+ baselines.fold_in_later_child(baselines_child_gives_container(records, &callbacks, child));
assert_eq!(baselines.first, Some(CssPixels::from_integer(10)));
assert_eq!(baselines.last, Some(CssPixels::from_integer(30)));
assert_eq!(baselines.inline_alignment, Some(CssPixels::from_integer(10)));
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index b4fafd9e938..9375f50b398 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -2270,7 +2270,11 @@ impl<'context> InlineFormattingContext<'context> {
self.leave_atomic_inlines_without_a_line_unplaced();
containing_used.set_building_line_data(std::mem::take(self.line_data.get_mut()));
block_formatting_context::ChildrenSummary {
- baselines: formatting_context::derive_baselines(self.run.records, &self.callbacks, self.containing_block),
+ baselines: formatting_context::derive_baselines_from_lines(
+ self.run.records,
+ &self.callbacks,
+ self.containing_block,
+ ),
fragmentainer_stop_token: self.fragmentainer_stop_token.take(),
lines_are_unbreakable: self.lines_are_unbreakable.get(),
}
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
index 3f99de2b25b..1be9f33a2cd 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
@@ -3968,7 +3968,7 @@ impl<'a> TreeBuilderHost<'a> {
self.attach_child(parent, wrapper, nearest_sibling);
// The wrapper takes the place of the run in its parent. A table-row, table-cell or table box is block-level,
// so a parent whose inline content it replaced entirely (a table-row-group or table around text, a table-row
- // around text or inline boxes) no longer has inline children. A stale flag would make derive_baselines() look
+ // around text or inline boxes) no longer has inline children. A stale flag would make baseline derivation look
// for line boxes the parent does not have, leaving it and every box that derives its baseline from it (a cell
// around a nested table, an inline-table in a line) without a baseline. A parent that keeps inline-level
// children next to the wrapper (text around an anonymous table generated for out-of-flow row groups, which