Skip to content

Truly independent web browser

LibWeb: Reuse table wrapper runs when only the space around them changes

Aliaksandr Kalenik30397b3eb202
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index e99c44d7193..20ba4f04dd3 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -26,6 +26,7 @@ pub(crate) struct RunInputsTheStoredRunNeverObserved {
pub(super) definite_available_inline_sizes_at_or_above: Option<CssPixels>,
pub(super) percentage_inline_basis: bool,
pub(super) own_block_size: formatting_context::OwnBlockSizeObservation,
+ pub(super) inline_space_around_root: bool,
}
impl RunInputsTheStoredRunNeverObserved {
@@ -34,6 +35,7 @@ impl RunInputsTheStoredRunNeverObserved {
definite_available_inline_sizes_at_or_above: None,
percentage_inline_basis: false,
own_block_size: formatting_context::OwnBlockSizeObservation::Observed,
+ inline_space_around_root: false,
..self
}
}
@@ -44,11 +46,13 @@ impl RunInputsTheStoredRunNeverObserved {
definite_available_inline_sizes_at_or_above,
percentage_inline_basis,
own_block_size,
+ inline_space_around_root,
} = self;
percentage_block_basis_and_block_size_definiteness
|| definite_available_inline_sizes_at_or_above.is_some()
|| percentage_inline_basis
|| own_block_size != formatting_context::OwnBlockSizeObservation::Observed
+ || inline_space_around_root
}
fn available_block_size_definiteness(self) -> bool {
@@ -57,11 +61,25 @@ impl RunInputsTheStoredRunNeverObserved {
}
}
-fn root_input_cells_match_ignoring_unobserved_own_block_size_and_block_margins(
+fn root_input_cells_match_ignoring_unobserved(
stored: used_values::RootInputCells,
probe: used_values::RootInputCells,
own_block_size: formatting_context::OwnBlockSizeObservation,
+ inline_space_around_root_unobserved: bool,
) -> bool {
+ let without_unobserved_inline_space = |cells: used_values::RootInputCells| {
+ if !inline_space_around_root_unobserved {
+ return cells;
+ }
+ used_values::RootInputCells {
+ content_inline_size: CssPixels::default(),
+ margin_left: CssPixels::default(),
+ margin_right: CssPixels::default(),
+ ..cells
+ }
+ };
+ let stored = without_unobserved_inline_space(stored);
+ let probe = without_unobserved_inline_space(probe);
match own_block_size {
formatting_context::OwnBlockSizeObservation::Observed => stored == probe,
formatting_context::OwnBlockSizeObservation::Unobserved => {
@@ -129,10 +147,11 @@ impl FcRunCacheKey {
return false;
}
self.fc_type == probe.fc_type
- && root_input_cells_match_ignoring_unobserved_own_block_size_and_block_margins(
+ && root_input_cells_match_ignoring_unobserved(
self.root_cells,
probe.root_cells,
unobserved.own_block_size,
+ unobserved.inline_space_around_root,
)
&& layout_inputs_match_ignoring_unobserved(&self.input, &probe.input, unobserved)
}
@@ -164,6 +183,7 @@ fn layout_inputs_match_ignoring_unobserved(
return false;
}
let available_inline_sizes_match = available_space.inline_size == b.available_space.inline_size
+ || unobserved.inline_space_around_root
|| match (
unobserved.definite_available_inline_sizes_at_or_above,
available_space.inline_size,
@@ -191,6 +211,7 @@ fn layout_inputs_match_ignoring_unobserved(
&sizing,
&b.sizing,
unobserved.available_block_size_definiteness(),
+ unobserved.inline_space_around_root,
)
}
@@ -198,6 +219,7 @@ fn sizing_directives_match_ignoring_unobserved(
a: &RootSizingDirectives,
b: &RootSizingDirectives,
block_size_definiteness_unobserved: bool,
+ float_avoidance_inline_size_unobserved: bool,
) -> bool {
let RootSizingDirectives {
forced_content_inline_size,
@@ -227,7 +249,7 @@ fn sizing_directives_match_ignoring_unobserved(
&& adopt_automatic_content_block_size == b.adopt_automatic_content_block_size
&& finds_automatic_line_clamp_lines == b.finds_automatic_line_clamp_lines
&& content_block_size_after_inside_layout == b.content_block_size_after_inside_layout
- && float_avoidance_inline_size == b.float_avoidance_inline_size
+ && (float_avoidance_inline_size == b.float_avoidance_inline_size || float_avoidance_inline_size_unobserved)
&& outer_float_intrusion_before_list_item_children == b.outer_float_intrusion_before_list_item_children
&& treat_block_axis_percentage_insets_as_auto_beyond_root
== b.treat_block_axis_percentage_insets_as_auto_beyond_root
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 7bf556e0a2c..ad369d43a3d 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -2020,7 +2020,16 @@ pub(super) fn run_formatting_context(
table_inline_layout,
None,
);
- return child_run_outcome_from_outputs(&callbacks, parent_fragments, box_, &input, root_cells, outputs, false);
+ return child_run_outcome_from_outputs(
+ &callbacks,
+ parent_fragments,
+ box_,
+ &input,
+ root_cells,
+ outputs,
+ false,
+ false,
+ );
}
let cache_key = fc_run_cache::FcRunCacheKey::new(fc_type, input, root_cells);
let cache_attempt = match fc_run_cache::FcRunCacheAttempt::probe(
@@ -2067,6 +2076,7 @@ pub(super) fn run_formatting_context(
root_cells,
outputs,
reuses_committed_subtree,
+ true,
);
}
};
@@ -2133,7 +2143,16 @@ pub(super) fn run_formatting_context(
);
}
cache_attempt.conclude(&callbacks, box_, cache_key, &input, &outputs);
- child_run_outcome_from_outputs(&callbacks, parent_fragments, box_, &input, root_cells, outputs, false)
+ child_run_outcome_from_outputs(
+ &callbacks,
+ parent_fragments,
+ box_,
+ &input,
+ root_cells,
+ outputs,
+ false,
+ false,
+ )
}
/// Takes a committing run of `box_` from the cache hit `hit` with nothing replayed: the box gets what the stored run
@@ -2167,11 +2186,16 @@ fn reuse_committed_subtree_as_it_is(
.flatten()?;
let content_block_size_adopted_after_run =
content_block_size_adopted_after_run(input, entry.outputs.result.automatic_content_block_size);
- let laid_out_box = entry.outputs.root_outcome.laid_out_child_box(
+ let mut laid_out_box = entry.outputs.root_outcome.laid_out_child_box(
root_cells,
&entry.outputs.result,
content_block_size_adopted_after_run,
);
+ keep_inline_margins_resolved_now_where_unobserved(
+ &mut laid_out_box,
+ root_cells,
+ entry.outputs.inputs_never_observed,
+ );
let geometry = fragment_tree::FragmentGeometry {
cells: laid_out_box.cells,
line_data: laid_out_box.line_data.clone(),
@@ -2193,6 +2217,17 @@ fn reuse_committed_subtree_as_it_is(
})
}
+fn keep_inline_margins_resolved_now_where_unobserved(
+ laid_out_box: &mut run_state::LaidOutChildBox,
+ root_cells: used_values::RootInputCells,
+ inputs_never_observed: fc_run_cache::RunInputsTheStoredRunNeverObserved,
+) {
+ if inputs_never_observed.inline_space_around_root {
+ laid_out_box.cells.margin_left = root_cells.margin_left;
+ laid_out_box.cells.margin_right = root_cells.margin_right;
+ }
+}
+
pub(crate) fn explicit_intrinsic_inner_inline_size(facts: &NodeFacts<'_>) -> CssPixels {
let style = facts.style();
if style.contain_intrinsic_width_has_length() {
@@ -2586,17 +2621,21 @@ fn lay_out_run_root_and_contents<'pass>(
);
result.depends_on_percentage_block_size = run.sizing().resolve_percentage_block_size_dependency(run.box_);
let item_run_observations_are_known = !run_is_handed_table_inline_layout_outside_its_key;
+ let table_wrapper_sized_by_its_parent = is_run_of_table_wrapper_sized_by_its_parent(run, fc_type, &input);
let inputs_never_observed = fc_run_cache::RunInputsTheStoredRunNeverObserved {
percentage_block_basis_and_block_size_definiteness: !result.depends_on_percentage_block_size,
definite_available_inline_sizes_at_or_above: atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above,
percentage_inline_basis: atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above.is_some()
|| (item_run_observations_are_known
- && item_run_never_observes_percentage_inline_basis(run, fc_type, &input)),
+ && item_run_never_observes_percentage_inline_basis(run, fc_type, &input))
+ || (table_wrapper_sized_by_its_parent
+ && table_in_wrapper_never_observes_percentage_inline_basis(run, run.box_)),
own_block_size: if item_run_observations_are_known {
own_block_size_observation_of_item_run(run, fc_type, &input, &result)
} else {
OwnBlockSizeObservation::Observed
},
+ inline_space_around_root: table_wrapper_sized_by_its_parent,
};
// NB: The root's descendants are laid out in the frame the root establishes.
@@ -3294,6 +3333,49 @@ fn root_content_includes_an_orthogonal_flow_sized_by_the_root_block_size(
false
}
+fn is_run_of_table_wrapper_sized_by_its_parent(
+ run: &FormattingContextRun,
+ fc_type: FormattingContextType,
+ input: &LayoutInput,
+) -> bool {
+ fc_type == FormattingContextType::Block
+ && input.participation == ParticipationInParentFormattingContext::BlockLevel
+ && input.sizing.block_parent_resolved_content_inline_size.is_some()
+ && NodeFacts::new(&run.callbacks, run.box_).is_table_wrapper()
+}
+
+fn table_in_wrapper_never_observes_percentage_inline_basis(run: &FormattingContextRun, wrapper: Node) -> bool {
+ let mut children_of_wrapper = run.callbacks.children(wrapper);
+ let (Some(table_box), None) = (children_of_wrapper.next(), children_of_wrapper.next()) else {
+ return false;
+ };
+ let resolves_percentage_against_inline_basis = |node| {
+ sizing_context::SizingContext::own_box_resolves_percentage_against_inline_basis(StyleValues::for_node(
+ &run.callbacks,
+ node,
+ ))
+ };
+ if resolves_percentage_against_inline_basis(table_box) {
+ return false;
+ }
+ let mut boxes_whose_children_are_internal_table_boxes = vec![table_box];
+ while let Some(parent) = boxes_whose_children_are_internal_table_boxes.pop() {
+ for child in run.callbacks.children(parent) {
+ let facts = NodeFacts::new(&run.callbacks, child);
+ if facts.is_text_node() {
+ continue;
+ }
+ if resolves_percentage_against_inline_basis(child) {
+ return false;
+ }
+ if facts.is_table_row_group_kind() || facts.is_table_row() || facts.is_table_column_group() {
+ boxes_whose_children_are_internal_table_boxes.push(child);
+ }
+ }
+ }
+ true
+}
+
/// Whether the run is of an item block container whose content resolves percentages against the container itself.
fn item_run_never_observes_percentage_inline_basis(
run: &FormattingContextRun,
@@ -3326,6 +3408,7 @@ pub(super) fn run_reads_content_block_size_adopted_after_it(
&& fc_type.registers_abspos_children_against_its_final_block_size()
}
+#[expect(clippy::too_many_arguments)]
fn child_run_outcome_from_outputs(
callbacks: &LayoutPass<'_>,
parent_fragments: Option<&fragment_tree::RunFragmentBuilder>,
@@ -3334,18 +3417,22 @@ fn child_run_outcome_from_outputs(
root_cells: used_values::RootInputCells,
outputs: RunOutputs,
reuses_committed_subtree: bool,
+ outputs_are_stored_ones: bool,
) -> ChildRunOutcome {
let RunOutputs {
result,
root,
root_outcome,
- inputs_never_observed: _,
+ inputs_never_observed,
host_findings,
} = outputs;
callbacks.receive_host_findings(host_findings);
let content_block_size_adopted_after_run =
content_block_size_adopted_after_run(input, result.automatic_content_block_size);
- let laid_out_box = root_outcome.laid_out_child_box(root_cells, &result, content_block_size_adopted_after_run);
+ let mut laid_out_box = root_outcome.laid_out_child_box(root_cells, &result, content_block_size_adopted_after_run);
+ if outputs_are_stored_ones {
+ keep_inline_margins_resolved_now_where_unobserved(&mut laid_out_box, root_cells, inputs_never_observed);
+ }
let fragment_arrival = parent_fragments.and(root).map(|root| {
debug_assert!(root.node == child, "a child run returned a root for a different box");
fragment_tree::ChildFragmentArrival::RunRoot {
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index e492fe8b9dc..298c36d4b61 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -1452,7 +1452,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
Self::own_box_resolves_percentage_against_inline_basis(self.style(node))
}
- fn own_box_resolves_percentage_against_inline_basis(style: StyleValues<'_>) -> bool {
+ pub(crate) fn own_box_resolves_percentage_against_inline_basis(style: StyleValues<'_>) -> bool {
style.width().contains_percentage()
|| style.min_width().contains_percentage()
|| style.max_width().contains_percentage()
diff --git a/Tests/LibWeb/Text/expected/table-wrapper-run-reuse-across-widths.txt b/Tests/LibWeb/Text/expected/table-wrapper-run-reuse-across-widths.txt
new file mode 100644
index 00000000000..c86828fc005
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/table-wrapper-run-reuse-across-widths.txt
@@ -0,0 +1,3 @@
+table wrapper runs after the width change: 1
+geometry matches a fresh layout: true
+centered table is centered: true
diff --git a/Tests/LibWeb/Text/input/table-wrapper-run-reuse-across-widths.html b/Tests/LibWeb/Text/input/table-wrapper-run-reuse-across-widths.html
new file mode 100644
index 00000000000..f0cb383e6eb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/table-wrapper-run-reuse-across-widths.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body { margin: 0; font: 16px/20px monospace; }
+ .container { width: 600px; }
+ .line { padding-left: 20px; }
+ .code { display: table-cell; }
+ .centered { margin: 0 auto; }
+ .percentage-padding td { padding: 0 5%; }
+</style>
+<div class="container" id="container"></div>
+<script>
+ // A table wrapper whose inline size its block parent resolved lays out its table in that size, so its run is reused
+ // when only the space around it changes, unless the table resolves percentages against the wrapper's containing
+ // block. Only the table with percentage cell padding is laid out again here.
+ test(() => {
+ let lines = "";
+ for (let index = 0; index < 20; ++index)
+ lines += `<div class="line"><span class="code">let value_${index} = ${index};</span></div>`;
+ container.innerHTML = `${lines}
+ <table class="centered" id="centered"><tr><td>centered</td></tr></table>
+ <table class="percentage-padding" id="padded"><tr><td>padded</td></tr></table>
+ <table id="captioned"><caption>caption</caption><tr><td>captioned</td></tr></table>`;
+ document.body.offsetHeight;
+
+ internals.beginLayoutTrace();
+ container.style.width = "700px";
+ document.body.offsetHeight;
+ const wrapperRuns = internals.takeLayoutTrace().split("\n").filter(line => line.includes("TableWrapper(anonymous)/block RUN"));
+ println(`table wrapper runs after the width change: ${wrapperRuns.length}`);
+
+ const fresh = container.cloneNode(true);
+ fresh.id = "";
+ document.body.appendChild(fresh);
+ const geometry = root => {
+ const rootRect = root.getBoundingClientRect();
+ return Array.from(root.querySelectorAll("div, span, table, caption, td")).map(element => {
+ const rect = element.getBoundingClientRect();
+ return `${rect.left - rootRect.left},${rect.top - rootRect.top},${rect.width},${rect.height}`;
+ }).join(" ");
+ };
+ println(`geometry matches a fresh layout: ${geometry(container) === geometry(fresh)}`);
+ const centered = document.getElementById("centered").getBoundingClientRect();
+ println(`centered table is centered: ${Math.abs(centered.left - (700 - centered.right)) < 1}`);
+ });
+</script>