Skip to content

Truly independent web browser

LibWeb: Resolve auto block sizes of independent roots in their run

Aliaksandr Kalenikf3da86356a43
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index af06b692a8a..ae9ef913ac6 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -2337,14 +2337,17 @@ impl AbsposEngine<'_> {
};
return Some(size);
}
+ if let BlockSizePass::AfterInsideLayout {
+ automatic_content_block_size_of_inside_layout: Some(automatic_content_block_size_of_inside_layout),
+ } = pass
+ {
+ return Some(automatic_content_block_size_of_inside_layout);
+ }
if self.facts(node).creates_block_formatting_context() {
- let BlockSizePass::AfterInsideLayout {
- automatic_content_block_size_of_inside_layout,
- } = pass
- else {
- return None;
+ return match pass {
+ BlockSizePass::BeforeInsideLayout => None,
+ BlockSizePass::AfterInsideLayout { .. } => Some(CssPixels::default()),
};
- return Some(automatic_content_block_size_of_inside_layout.unwrap_or_default());
}
let inner = self
.used(node)
@@ -2842,13 +2845,8 @@ impl<'pass> AbsposEngine<'pass> {
}
}
- self.sizing().make_form_control_content_box_definite(
- node,
- LayoutMode::Normal,
- available_space,
- constraints,
- None,
- );
+ self.sizing()
+ .make_form_control_content_box_definite(node, available_space, constraints, None);
}
pub(crate) fn finalize_out_of_flow_root_after_inside_layout(
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 66b32dce7ec..8b8d798a239 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -3878,13 +3878,10 @@ impl<'pass> BlockFormattingContext<'pass> {
if input.sizing.finds_automatic_line_clamp_lines || run.fragments.is_none() {
self.automatic_line_clamp_block_size.set(automatic_block_size);
} else if automatic_block_size.is_some() {
- let measurement = formatting_context::MeasurementState::create(self.run.callbacks);
- let measurement_root =
- used_values::RootInputCells::capture(self.used(self.run.box_)).materialize_record();
- let measurement_result = measurement.run_with_layout_mode(
+ let measurement_result = formatting_context::lay_out_measurement_root_for_its_result(
+ self.run.callbacks,
self.run.box_,
- &measurement_root,
- self.run.layout_mode,
+ used_values::RootInputCells::capture(self.used(self.run.box_)),
LayoutInput {
sizing: RootSizingDirectives {
finds_automatic_line_clamp_lines: true,
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index 168429bb389..8f1385a59c5 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -783,9 +783,9 @@ pub(super) fn run_root_validity(callbacks: &LayoutPass<'_>, box_: Node) -> FcRun
/// the run executes. Uncacheable classes bypass entirely: subgridded grid
/// items copy the parent grid's mid-run track state, which the key cannot
/// see; anchor()-positioned roots keep their bypass while the resolved-inset
-/// side effects are audited; pass entries, measurement roots and internal
-/// runs are not spawned child runs; devtools collection emits per-run
-/// callbacks a replay would skip.
+/// side effects are audited; pass entries, measurement roots that do not
+/// keep their result and internal runs are not spawned child runs; devtools
+/// collection emits per-run callbacks a replay would skip.
pub(super) enum FcRunCacheAttempt {
Bypass,
Store {
@@ -821,6 +821,7 @@ impl FcRunCacheAttempt {
parent_grid_is_present: bool,
builds_fragments: bool,
should_collect_devtools_layout_data: bool,
+ measurement_root_keeps_its_result: bool,
callbacks: &LayoutPass<'_>,
key: &FcRunCacheKey,
) -> Result<Self, FcRunCacheHit> {
@@ -828,6 +829,7 @@ impl FcRunCacheAttempt {
box_,
parent_grid_is_present,
should_collect_devtools_layout_data,
+ measurement_root_keeps_its_result,
callbacks,
key,
) {
@@ -960,13 +962,14 @@ pub(super) fn can_describe_run(
box_: Node,
parent_grid_is_present: bool,
should_collect_devtools_layout_data: bool,
+ measurement_root_keeps_its_result: bool,
callbacks: &LayoutPass<'_>,
key: &FcRunCacheKey,
) -> bool {
let fc_type = key.fc_type;
let input = &key.input;
if should_collect_devtools_layout_data
- || input.participation == ParticipationInParentFormattingContext::Root
+ || (input.participation == ParticipationInParentFormattingContext::Root && !measurement_root_keeps_its_result)
|| matches!(
fc_type,
formatting_context::FormattingContextType::InternalReplaced | formatting_context::FormattingContextType::InternalDummy
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 0fe0d23e060..34dd023e4f5 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1510,21 +1510,18 @@ pub(crate) fn independent_root_automatic_block_size(
constraints: ContainingBlockConstraints,
automatic_content_block_size_of_completed_run: Option<CssPixels>,
) -> CssPixels {
- let facts = NodeFacts::new(callbacks, node);
- if facts.creates_block_formatting_context() {
- return automatic_content_block_size_of_completed_run.unwrap_or_default();
+ if let Some(automatic_content_block_size_reported_by_completed_run) = automatic_content_block_size_of_completed_run
+ {
+ return automatic_content_block_size_reported_by_completed_run;
+ }
+ if NodeFacts::new(callbacks, node).creates_block_formatting_context() {
+ return CssPixels::default();
}
let style = StyleValues::for_node(callbacks, node);
// https://w3c.github.io/mathml-core/#layout-algorithms
// After calculating the line-ascent and line-descent of a math content box, its block size is set to their sum.
if style.display().is_math_inside() {
- return automatic_content_block_size_of_completed_run.unwrap_or_default();
- }
- if style.display().is_flex_inside()
- && let Some(automatic_content_block_size_reported_by_completed_run) =
- automatic_content_block_size_of_completed_run
- {
- return automatic_content_block_size_reported_by_completed_run;
+ return CssPixels::default();
}
if style.display().is_flex_inside() || style.display().is_grid_inside() || style.display().is_table_inside() {
// The automatic block size of a flex, grid, or table container is its
@@ -1616,7 +1613,6 @@ fn apply_root_sizing_directives(
run.box_,
input.available_space,
input.containing_block_constraints,
- run.layout_mode,
run_cache_may_reuse_this_run_at_other_available_inline_sizes,
);
let mut body_input = body_input_with_inner_available_space(run, input);
@@ -1694,20 +1690,14 @@ fn dimension_block_level_root(
&& lays_out_content_against_min_block_size(NodeFacts::new(&run.callbacks, node), fc_type)
{
let content_block_size =
- sizing.measure_automatic_content_block_size(node, run.layout_mode, body_input.available_space, constraints);
+ sizing.measure_automatic_content_block_size(node, body_input.available_space, constraints);
measured_content_block_size = Some(content_block_size);
let min_block_size = sizing.calculate_inner_block_size(node, available_space, style.min_height(), constraints);
if content_block_size < min_block_size {
body_input.available_space.block_size = AvailableSize::definite(min_block_size);
}
}
- sizing.make_form_control_content_box_definite(
- node,
- run.layout_mode,
- available_space,
- constraints,
- measured_content_block_size,
- );
+ sizing.make_form_control_content_box_definite(node, available_space, constraints, measured_content_block_size);
body_input
}
@@ -1939,6 +1929,7 @@ pub(super) fn run_formatting_context(
parent_grid.is_some(),
builds_fragments,
should_collect_devtools_layout_data,
+ false,
&callbacks,
&cache_key,
) {
@@ -2887,6 +2878,24 @@ pub(crate) fn item_is_block_container_whose_committing_run_can_adopt_its_measure
&& !block_formatting_context::root_lays_out_content_against_its_own_block_size(sizing, item)
}
+impl ChildRunShape {
+ pub(crate) fn of_measurement_root(
+ callbacks: &LayoutPass<'_>,
+ node: Node,
+ root_cells: used_values::RootInputCells,
+ input: LayoutInput,
+ ) -> Self {
+ debug_assert_eq!(input.participation, ParticipationInParentFormattingContext::Root);
+ Self {
+ node,
+ fc_type: independent_formatting_context_type(node, callbacks),
+ input,
+ root_cells,
+ root_containing_block: callbacks.containing_block_for_child_run(node, &input.participation),
+ }
+ }
+}
+
pub(crate) struct ChildRunPlan {
pub(crate) fc_type: FormattingContextType,
pub(crate) root_containing_block: Node,
@@ -2927,6 +2936,19 @@ pub(crate) fn child_run_plan(
})
}
+pub(crate) fn lay_out_measurement_root_for_its_result(
+ callbacks: LayoutPass<'_>,
+ node: Node,
+ root_cells: used_values::RootInputCells,
+ input: LayoutInput,
+) -> ChildLayoutResult {
+ lay_out_shape_for_its_result(
+ callbacks,
+ ChildRunShape::of_measurement_root(&callbacks, node, root_cells, input),
+ LayoutMode::Normal,
+ )
+}
+
pub(crate) fn lay_out_shape_for_its_result(
callbacks: LayoutPass<'_>,
shape: ChildRunShape,
@@ -2968,22 +2990,29 @@ pub(crate) fn lay_out_shape_for_its_result(
let runs_ahead_of_intrinsic_block_padding =
builds_fragments && input.sizing.table_cell_intrinsic_block_padding.is_some();
let cache_key = fc_run_cache::FcRunCacheKey::new(fc_type, layout_mode, input, root_cells);
- let cache_attempt =
- match fc_run_cache::FcRunCacheAttempt::probe(node, false, builds_fragments, false, &callbacks, &cache_key) {
- Ok(attempt) => attempt,
- Err(hit) => {
- let action = if runs_ahead_of_intrinsic_block_padding {
- "READ RESULT AHEAD OF INTRINSIC BLOCK PADDING"
- } else {
- "READ RESULT"
- };
- let _trace = callbacks
- .arena()
- .layout_trace
- .run(node, fc_type, layout_mode, || action);
- return result_with_host_findings(&hit.entry.outputs);
- }
- };
+ let cache_attempt = match fc_run_cache::FcRunCacheAttempt::probe(
+ node,
+ false,
+ builds_fragments,
+ false,
+ true,
+ &callbacks,
+ &cache_key,
+ ) {
+ Ok(attempt) => attempt,
+ Err(hit) => {
+ let action = if runs_ahead_of_intrinsic_block_padding {
+ "READ RESULT AHEAD OF INTRINSIC BLOCK PADDING"
+ } else {
+ "READ RESULT"
+ };
+ let _trace = callbacks
+ .arena()
+ .layout_trace
+ .run(node, fc_type, layout_mode, || action);
+ return result_with_host_findings(&hit.entry.outputs);
+ }
+ };
let action = if runs_ahead_of_intrinsic_block_padding {
"RUN AHEAD OF INTRINSIC BLOCK PADDING"
} else {
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 73ce4b18c64..3e374f992f2 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -2006,7 +2006,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
constraints: ContainingBlockConstraints,
layout_mode: LayoutMode,
) {
- self.dimension_atomic_root(node, available_space, constraints, layout_mode, false);
+ self.dimension_atomic_root(node, available_space, constraints, false);
self.resolve_used_block_size_if_treated_as_auto(
node,
available_space,
@@ -2026,7 +2026,6 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
node: Node,
available_space: AvailableSpace,
constraints: ContainingBlockConstraints,
- layout_mode: LayoutMode,
report_for_run_cache_the_available_inline_sizes_this_sizing_repeats_for: bool,
) -> Option<CssPixels> {
let containing_inline_size = available_space.inline_size.to_px_or_zero();
@@ -2082,7 +2081,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
block_size: AvailableSize::Indefinite,
};
self.resolve_used_block_size_if_not_treated_as_auto(node, inline_definite_space, constraints);
- self.make_form_control_content_box_definite(node, layout_mode, available_space, constraints, None);
+ self.make_form_control_content_box_definite(node, available_space, constraints, None);
sizing_repeats_for_available_inline_sizes_at_or_above
}
@@ -3301,7 +3300,6 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
pub(crate) fn measure_automatic_content_block_size(
&self,
node: Node,
- layout_mode: LayoutMode,
inner_available_space: AvailableSpace,
constraints: ContainingBlockConstraints,
) -> CssPixels {
@@ -3314,21 +3312,17 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
constraints,
);
}
-
- let measurement = formatting_context::MeasurementState::create(self.callbacks);
- let node_used = measurement.create_used_values(node, constraints);
- measurement
- .run_with_layout_mode(
- node,
- &node_used,
- layout_mode,
- LayoutInput::new(
- inner_available_space,
- constraints,
- ParticipationInParentFormattingContext::Root,
- ),
- )
- .automatic_content_block_size
+ formatting_context::lay_out_measurement_root_for_its_result(
+ self.callbacks,
+ node,
+ used_values::RootInputCells::capture(&used_values::create_used_values(&self.callbacks, node, constraints)),
+ LayoutInput::new(
+ inner_available_space,
+ constraints,
+ ParticipationInParentFormattingContext::Root,
+ ),
+ )
+ .automatic_content_block_size
}
/// Buttons and text inputs center their content within their content box, which therefore has to be definite
@@ -3336,7 +3330,6 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
pub(crate) fn make_form_control_content_box_definite(
&self,
node: Node,
- layout_mode: LayoutMode,
available_space: AvailableSpace,
constraints: ContainingBlockConstraints,
measured_content_block_size: Option<CssPixels>,
@@ -3370,7 +3363,6 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
let natural = measured_content_block_size.unwrap_or_else(|| {
self.measure_automatic_content_block_size(
node,
- layout_mode,
self.used(node)
.available_inner_space_or_constraints_from(available_space),
constraints,
diff --git a/Tests/LibWeb/Text/expected/layout-inline-grid-block-size-from-its-run.txt b/Tests/LibWeb/Text/expected/layout-inline-grid-block-size-from-its-run.txt
new file mode 100644
index 00000000000..1d9caf0fd51
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/layout-inline-grid-block-size-from-its-run.txt
@@ -0,0 +1 @@
+PASS: the inline grid takes its automatic block size from its run instead of measuring itself again (work + checks)
diff --git a/Tests/LibWeb/Text/input/layout-inline-grid-block-size-from-its-run.html b/Tests/LibWeb/Text/input/layout-inline-grid-block-size-from-its-run.html
new file mode 100644
index 00000000000..d51b220d30f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/layout-inline-grid-block-size-from-its-run.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script src="layout-test.js"></script>
+<style>
+ body { margin: 0; }
+ #grid { display: inline-grid; width: 100px; }
+</style>
+<span>x</span><div id="grid"><div id="bar" style="height: 30px"></div></div>
+<script>
+ layoutTest({
+ steps: [
+ {
+ name: "the inline grid takes its automatic block size from its run instead of measuring itself again",
+ mutate: () => {
+ bar.style.height = "40px";
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#grid/grid RUN (cache=miss)
+ BlockContainer<DIV>#bar/block RUN (cache=miss)
+ `,
+ check: equal => {
+ equal(grid.getBoundingClientRect().height, 40, "grid height");
+ },
+ },
+ ],
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/layout-run-cache-measured-content-committed-by-replay.html b/Tests/LibWeb/Text/input/layout-run-cache-measured-content-committed-by-replay.html
index 360f779c60c..5d97af414d8 100644
--- a/Tests/LibWeb/Text/input/layout-run-cache-measured-content-committed-by-replay.html
+++ b/Tests/LibWeb/Text/input/layout-run-cache-measured-content-committed-by-replay.html
@@ -63,7 +63,7 @@
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)
BlockContainer<BUTTON>#button/block RUN (cache=miss)
- BlockContainer<BUTTON>#button/block RUN (cache=bypass)
+ BlockContainer<BUTTON>#button/block RUN (cache=miss)
BlockContainer(anonymous)/flex RUN (cache=miss)
BlockContainer(anonymous) MEASURE max-content block (cache=hit)
BlockContainer(anonymous) MEASURE max-content block (cache=hit)
@@ -86,7 +86,7 @@
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)
BlockContainer<BUTTON>#button/block RUN (cache=miss)
- BlockContainer<BUTTON>#button/block RUN (cache=bypass)
+ BlockContainer<BUTTON>#button/block RUN (cache=miss)
BlockContainer(anonymous)/flex RUN (cache=miss)
BlockContainer(anonymous) MEASURE max-content block (cache=hit)
BlockContainer(anonymous) MEASURE max-content block (cache=hit)
diff --git a/Tests/LibWeb/Text/input/layout-run-cache-measurement-replay-of-recommitted-subtree-misses.html b/Tests/LibWeb/Text/input/layout-run-cache-measurement-replay-of-recommitted-subtree-misses.html
index a3d471c6123..aef30f1b5e3 100644
--- a/Tests/LibWeb/Text/input/layout-run-cache-measurement-replay-of-recommitted-subtree-misses.html
+++ b/Tests/LibWeb/Text/input/layout-run-cache-measurement-replay-of-recommitted-subtree-misses.html
@@ -62,7 +62,7 @@
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)
BlockContainer<BUTTON>#button/block RUN (cache=miss)
- BlockContainer<BUTTON>#button/block RUN (cache=bypass)
+ BlockContainer<BUTTON>#button/block RUN (cache=miss)
BlockContainer(anonymous)/flex RUN (cache=miss)
BlockContainer(anonymous) MEASURE max-content block (cache=hit)
BlockContainer(anonymous) MEASURE max-content block (cache=hit)
@@ -84,7 +84,7 @@
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)
BlockContainer<BUTTON>#button/block RUN (cache=miss)
- BlockContainer<BUTTON>#button/block RUN (cache=bypass)
+ BlockContainer<BUTTON>#button/block RUN (cache=miss)
BlockContainer(anonymous)/flex RUN (cache=miss)
BlockContainer(anonymous) MEASURE max-content block (cache=miss)
BlockContainer(anonymous)/block (intrinsic) RUN (cache=bypass)
@@ -114,7 +114,7 @@
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)
BlockContainer<BUTTON>#button/block RUN (cache=miss)
- BlockContainer<BUTTON>#button/block RUN (cache=bypass)
+ BlockContainer<BUTTON>#button/block RUN (cache=miss)
BlockContainer(anonymous)/flex RUN (cache=miss)
BlockContainer(anonymous) MEASURE max-content block (cache=hit)
BlockContainer(anonymous) MEASURE max-content block (cache=hit)