Skip to content

Truly independent web browser

LibWeb: Hand a table's inline layout to the run that reuses it

Aliaksandr Kalenikaf83c95a5e09
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index c10407bb2a2..3ea3545d916 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -2052,13 +2052,17 @@ impl AbsposEngine<'_> {
let style = self.style_of(sized);
let sizing = self.sizing();
let initial = if self.facts(node).is_table_wrapper() {
- Some(sizing.compute_table_box_inline_size_inside_wrapper(
- node,
- available_space,
- constraints,
- None,
- formatting_context::TableWrapperInlineSizeMode::ClampToAvailableInlineSize,
- ))
+ Some(
+ sizing
+ .compute_table_box_inline_size_inside_wrapper(
+ node,
+ available_space,
+ constraints,
+ None,
+ formatting_context::TableWrapperInlineSizeMode::ClampToAvailableInlineSize,
+ )
+ .0,
+ )
} else if style.width().is_auto() {
None
} else {
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 81e2fd57511..cac54b454be 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -539,6 +539,9 @@ pub(crate) struct BlockFormattingContext<'pass> {
trailing_collapsed_margin: Cell<Option<(usize, CssPixels)>>,
table_box_in_wrapper_border_box_inline_size: Cell<Option<CssPixels>>,
table_box_in_wrapper_border_box_block_size: Cell<Option<CssPixels>>,
+ /// The inline layout of the table box inside a table wrapper child, as sizing the wrapper last found it, which the
+ /// child's run can reuse.
+ table_inline_layout_of_child: Cell<Option<(Node, table_formatting_context::TableInlineLayout)>>,
is_line_clamp_container: bool,
clamped_content_is_scrollable_overflow: bool,
max_lines: Cell<Option<usize>>,
@@ -659,6 +662,7 @@ impl<'pass> BlockFormattingContext<'pass> {
trailing_collapsed_margin: Cell::new(None),
table_box_in_wrapper_border_box_inline_size: Cell::new(None),
table_box_in_wrapper_border_box_block_size: Cell::new(None),
+ table_inline_layout_of_child: Cell::new(None),
is_line_clamp_container,
clamped_content_is_scrollable_overflow,
// NB: Measurements at a definite inline size need the clamped block size, just like committed layout.
@@ -764,6 +768,19 @@ impl<'pass> BlockFormattingContext<'pass> {
self.run.records.used_values(node)
}
+ /// Keeps `layout`, the inline layout of the table box inside `child`, for the run of `child`.
+ pub(crate) fn keep_table_inline_layout_of(&self, child: Node, layout: table_formatting_context::TableInlineLayout) {
+ self.table_inline_layout_of_child.set(Some((child, layout)));
+ }
+
+ /// The inline layout of the table box inside `child` this formatting context keeps for the run of `child`, if any.
+ pub(crate) fn take_table_inline_layout_of(
+ &self,
+ child: Node,
+ ) -> Option<table_formatting_context::TableInlineLayout> {
+ take_table_inline_layout_kept_for(&self.table_inline_layout_of_child, child)
+ }
+
/// The run's root, as the block container of the boxes laid out in it.
fn root_block_container(&self) -> BlockContainer<'pass> {
BlockContainer::root_of(&self.run)
@@ -1252,13 +1269,17 @@ impl<'pass> BlockFormattingContext<'pass> {
// so clamp it against the containing block instead. That way a table which is too wide for the space
// next to floats reports its real size and gets placed below the floats.
let table_has_specified_inline_size = !self.style(sizing.table_box_inside_wrapper(node)).width().is_auto();
- Some(sizing.compute_table_box_inline_size_inside_wrapper(
+ let (inline_size, table_inline_layout) = sizing.compute_table_box_inline_size_inside_wrapper(
node,
remaining_available_space,
constraints,
table_has_specified_inline_size.then_some(available_inline_size),
formatting_context::TableWrapperInlineSizeMode::ClampToAvailableInlineSize,
- ))
+ );
+ if node != self.run.box_ {
+ self.keep_table_inline_layout_of(node, table_inline_layout);
+ }
+ Some(inline_size)
} else if facts.uses_button_layout() && style.width().is_auto() {
// https://html.spec.whatwg.org/multipage/rendering.html#button-layout
// If the computed value of 'inline-size' is 'auto', then the used value is the fit-content inline size.
@@ -5413,3 +5434,17 @@ pub(crate) fn floor_list_item_automatic_block_size_by_marker_line_height(
}
automatic_content_block_size.max(StyleValues::for_node(&callbacks, marker).line_height())
}
+
+/// The inline layout of the table box inside `child` that `kept` holds for the run of `child`, which stays kept for
+/// another child.
+pub(crate) fn take_table_inline_layout_kept_for(
+ kept: &Cell<Option<(Node, table_formatting_context::TableInlineLayout)>>,
+ child: Node,
+) -> Option<table_formatting_context::TableInlineLayout> {
+ let (kept_for, layout) = kept.take()?;
+ if kept_for != child {
+ kept.set(Some((kept_for, layout)));
+ return None;
+ }
+ Some(layout)
+}
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index c9ebd5b481a..1b8c6bb820d 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -2319,15 +2319,11 @@ fn lay_out_run_root_and_contents<'pass>(
} else {
run
};
- let mut inline_layout_of_root_table_box = None;
- if let Some(table_inline_layout) = table_inline_layout {
- if table_inline_layout.table_box() == box_ {
- inline_layout_of_root_table_box = Some(table_inline_layout);
- } else {
- run.records
- .store_table_inline_layout(table_inline_layout.table_box(), table_inline_layout);
- }
- }
+ // NB: A table wrapper's run hands the inline layout of its table box to the run of that box.
+ let (mut inline_layout_of_root_table_box, inline_layout_of_child_table_box) = match table_inline_layout {
+ Some(table_inline_layout) if table_inline_layout.table_box() == box_ => (Some(table_inline_layout), None),
+ table_inline_layout => (None, table_inline_layout),
+ };
let RootSizingOutcome {
body_input,
atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above,
@@ -2359,6 +2355,11 @@ fn lay_out_run_root_and_contents<'pass>(
} else {
let mut context_implementation =
create_formatting_context_implementation(body_run, parent_grid, fc_type, previous_line_data);
+ if let (Some(layout), FormattingContextImplementation::Block(context)) =
+ (inline_layout_of_child_table_box, &context_implementation)
+ {
+ context.keep_table_inline_layout_of(layout.table_box(), layout);
+ }
let result = match &mut context_implementation {
FormattingContextImplementation::Block(context) => {
context.run(body_run, body_input);
@@ -2927,7 +2928,9 @@ pub(crate) fn layout_inside_child_with_input_cells(
run.should_collect_devtools_layout_data,
run.callbacks,
input,
- run.records.take_table_inline_layout(child),
+ parent_block
+ .and_then(|block| block.take_table_inline_layout_of(child))
+ .or_else(|| parent_grid.and_then(|grid| grid.take_table_inline_layout_of(child))),
);
let ChildRunOutcome {
result,
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 108724bb242..63cbe4a6fa5 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -1095,6 +1095,9 @@ type SubgridBlockSizeMemo = std::rc::Rc<std::cell::RefCell<std::collections::Has
pub(crate) struct GridFormattingContext<'pass> {
run: FormattingContextRun<'pass>,
+ /// The inline layout of the table box inside a table wrapper item, as resolving the wrapper's inline size last
+ /// found it, which the item's run can reuse.
+ table_inline_layout_of_item: Cell<Option<(Node, table_formatting_context::TableInlineLayout)>>,
derived_baselines_of_root_box: DerivedBaselines,
parent_grid: Option<ParentGridData<'pass>>,
available_space: Option<AvailableSpace>,
@@ -1297,6 +1300,7 @@ impl<'pass> GridFormattingContext<'pass> {
next_item_lookup_start: Cell::new(0),
cells_of_items: HashMap::default(),
laid_out_items: RefCell::new(HashMap::default()),
+ table_inline_layout_of_item: Cell::new(None),
in_flow_items_margin_box_block_extent: None,
lanes: None,
}
@@ -3604,6 +3608,14 @@ impl<'pass> GridFormattingContext<'pass> {
)
}
+ /// The inline layout of the table box inside `item` this grid keeps for the run of `item`, if any.
+ pub(crate) fn take_table_inline_layout_of(
+ &self,
+ item: Node,
+ ) -> Option<table_formatting_context::TableInlineLayout> {
+ block_formatting_context::take_table_inline_layout_kept_for(&self.table_inline_layout_of_item, item)
+ }
+
fn resolve_table_wrapper_inline_size(&self, item: GridItem, containing_for_wrapper: CssPixels) -> ItemAlignment {
let containing_block = self.containing_block_size(item, Axis::Row);
let available = AvailableSpace {
@@ -3613,13 +3625,15 @@ impl<'pass> GridFormattingContext<'pass> {
let mut constraints = self.container_constraints();
constraints.percentage_basis_inline_size = Some(containing_for_wrapper);
- let mut wrapper_size = self.sizing().compute_table_box_inline_size_inside_wrapper(
+ let (mut wrapper_size, table_inline_layout) = self.sizing().compute_table_box_inline_size_inside_wrapper(
item.box_,
available,
constraints,
Some(containing_for_wrapper),
formatting_context::TableWrapperInlineSizeMode::UseTableUsedInlineSizeIfNotAuto,
);
+ self.table_inline_layout_of_item
+ .set(Some((item.box_, table_inline_layout)));
let wrapper_style = self.style(item.box_);
let table_box = self.sizing().table_box_inside_wrapper(item.box_);
let table_style = self.style(table_box);
diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rs
index 1aa6bac57d0..6dbf8a52689 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs
@@ -264,9 +264,6 @@ pub(crate) struct RunRecords<'arena> {
runs_in_progress_with_it: usize,
/// What the run keeps of each box it holds but its root.
held_boxes: RefCell<HashMap<Node, KeptBoxHolding>>,
- // Wrapper sizing hands this state to the wrapper's child run, which consumes it while laying
- // out the table box. Keeping it on the run prevents measurement state from escaping a pass.
- table_inline_layouts: RefCell<HashMap<Node, table_formatting_context::TableInlineLayout>>,
content_sizing: ContentSizing,
/// What the run found so far about the percentage block size dependency of its root and of the boxes whose
/// layout in it is in progress, the innermost last. Only these boxes have their dependency resolved.
@@ -461,7 +458,6 @@ impl<'arena> RunRecords<'arena> {
scratch,
runs_in_progress_with_it: scratch.begin_run(),
held_boxes: RefCell::new(HashMap::default()),
- table_inline_layouts: RefCell::new(HashMap::default()),
content_sizing,
percentage_block_size_dependencies_in_progress: RefCell::new(smallvec::smallvec![(
root,
@@ -704,14 +700,6 @@ impl<'arena> RunRecords<'arena> {
);
*kept = KeptBoxHolding::ChildHandedBack;
}
-
- pub(crate) fn store_table_inline_layout(&self, table: Node, layout: table_formatting_context::TableInlineLayout) {
- self.table_inline_layouts.borrow_mut().insert(table, layout);
- }
-
- pub(crate) fn take_table_inline_layout(&self, table: Node) -> Option<table_formatting_context::TableInlineLayout> {
- self.table_inline_layouts.borrow_mut().remove(&table)
- }
}
impl Drop for RunRecords<'_> {
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index d3f74bb11e0..87462414b8e 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -3593,6 +3593,8 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
// 17.5.2 Table width algorithms: the 'table-layout' property
// https://www.w3.org/TR/CSS22/tables.html#width-layout
+ /// Returns the inline size of the wrapper and the inline layout of the table box it found, which the run of the
+ /// table box can reuse.
pub(crate) fn compute_table_box_inline_size_inside_wrapper(
&self,
wrapper: Node,
@@ -3600,7 +3602,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
table_wrapper_constraints: ContainingBlockConstraints,
table_wrapper_containing_block_inline_size: Option<CssPixels>,
table_wrapper_inline_size_mode: formatting_context::TableWrapperInlineSizeMode,
- ) -> CssPixels {
+ ) -> (CssPixels, table_formatting_context::TableInlineLayout) {
// CSS 2 says the table wrapper inline size is the border-edge inline size of the table grid box inside it.
let style = self.style(wrapper);
@@ -3635,19 +3637,17 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
);
let table_used_inline_size = table_used.border_box_inline_size(table_used.uses_collapsing_borders_model());
- self.records
- .store_table_inline_layout(wrapper, table.take_inline_layout());
- if table_wrapper_inline_size_mode
+ let inline_size = if table_wrapper_inline_size_mode
== formatting_context::TableWrapperInlineSizeMode::UseTableUsedInlineSizeIfNotAuto
&& !self.style(table_run.box_).width().is_auto()
{
- return table_used_inline_size;
- }
- if matches!(available_space.inline_size, AvailableSize::Definite(_)) {
+ table_used_inline_size
+ } else if matches!(available_space.inline_size, AvailableSize::Definite(_)) {
table_used_inline_size.min(available_inline_size)
} else {
table_used_inline_size
- }
+ };
+ (inline_size, table.take_inline_layout())
})
}