Truly independent web browser
LibWeb: Adopt measured item runs that lay out contained abspos boxes
Aliaksandr Kalenikaf366f0be4c8
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rsindex c9b581f615d..9b1d5d9730f 100644--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs@@ -32,6 +32,21 @@ pub(crate) struct RunInputsTheStoredRunNeverObserved {}impl RunInputsTheStoredRunNeverObserved {+ /// What the run still never observed once it laid out the absolutely positioned boxes its root contains against the+ /// root's final sizes and box sides. Where the contents never observed the root's own block size, those boxes+ /// observed only the block size the run left the root with.+ pub(super) fn once_abspos_contained_by_root_are_laid_out(self) -> Self {+ if self.own_block_size != formatting_context::OwnBlockSizeObservation::Unobserved {+ return Self::default();+ }+ Self {+ percentage_block_basis_and_block_size_definiteness: self.percentage_block_basis_and_block_size_definiteness,+ own_block_size: formatting_context::OwnBlockSizeObservation::ObservedOnlyByAbsposContainedByRoot,+ content_block_size_adopted_after_run: self.content_block_size_adopted_after_run,+ ..Self::default()+ }+ }+fn accepted_when_reusing_stale_line_data_after_structural_inline_damage(self) -> Self {Self {definite_available_inline_sizes_at_or_above: None,@@ -85,7 +100,8 @@ fn root_input_cells_match_ignoring_unobserved(let probe = without_unobserved_inline_space(probe);match own_block_size {formatting_context::OwnBlockSizeObservation::Observed => stored == probe,- formatting_context::OwnBlockSizeObservation::Unobserved => {+ formatting_context::OwnBlockSizeObservation::Unobserved+ | formatting_context::OwnBlockSizeObservation::ObservedOnlyByAbsposContainedByRoot => {let without_own_block_size_and_block_margins = |cells| used_values::RootInputCells {content_block_size: CssPixels::default(),has_definite_block_size: false,@@ -372,17 +388,40 @@ impl FcRunCacheEntry {Some(cells) => (cells.content_inline_size, cells.content_block_size),None => (self.key.root_cells.content_inline_size,- formatting_context::content_block_size_adopted_after_run(+ self.content_block_size_run_leaves_root_whose_cells_its_parent_owns(&self.key.input,- self.outputs.result.automatic_content_block_size,- )- .map_or(self.key.root_cells.content_block_size, |block_size| {- clamp_to_max_dimension_value(block_size.max(CssPixels::default()))- }),+ self.key.root_cells,+ ),),}}+ /// The content block size a run with `input` and the parent-owned root input cells `root_cells` leaves its root+ /// with, where its contents are laid out as the stored run's were.+ fn content_block_size_run_leaves_root_whose_cells_its_parent_owns(+ &self,+ input: &LayoutInput,+ root_cells: used_values::RootInputCells,+ ) -> CssPixels {+ formatting_context::content_block_size_run_leaves_root_whose_cells_its_parent_owns(+ input,+ root_cells,+ self.outputs.result.automatic_content_block_size,+ )+ }++ /// Whether the absolutely positioned boxes the stored run laid out against its root would be laid out against the+ /// same root block size by the probed run, where they are all that observed the root's own block size.+ fn probe_leaves_root_with_block_size_its_abspos_were_laid_out_against(&self, probe: &FcRunCacheProbe<'_>) -> bool {+ self.outputs.inputs_never_observed.own_block_size+ != formatting_context::OwnBlockSizeObservation::ObservedOnlyByAbsposContainedByRoot+ || self.content_block_size_run_leaves_root_whose_cells_its_parent_owns(probe.input, probe.root_cells)+ == self.content_block_size_run_leaves_root_whose_cells_its_parent_owns(+ &self.key.input,+ self.key.root_cells,+ )+ }+fn last_used_at_layout_commit(&self) -> u64 {self.last_used_at_layout_commit.load(std::sync::atomic::Ordering::Relaxed)@@ -414,13 +453,14 @@ impl FcRunCacheEntry {fn answers_probe_for(&self, probe: &FcRunCacheProbe<'_>) -> bool {let unobserved = self.outputs.inputs_never_observed;- self.key.matches_probe(probe, unobserved)+ (self.key.matches_probe(probe, unobserved)|| (self.table_cell_block_paddings_unobserved && {let probe_key = probe.key().without_root_block_paddings();self.key.without_root_block_paddings().matches_probe(&probe_key.as_probe(), unobserved)- })+ }))+ && self.probe_leaves_root_with_block_size_its_abspos_were_laid_out_against(probe)}}diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rsindex 90c7468e383..090d74accc9 100644--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs@@ -3398,7 +3398,10 @@ impl<'pass> FlexFormattingContext<'pass> {main_size,cross_size,);- let input = self.item_layout_input(index, &cells, space_for_items);+ let mut input = self.item_layout_input(index, &cells, space_for_items);+ // NB: The measured item ends its run at its automatic block size, the one a content-sized item is handed, so+ // the absolutely positioned boxes it contains are laid out as a committing run at that size lays them out.+ input.sizing.content_block_size_after_run = geometry::ContentBlockSizeAfterRun::Automatic;let plan = formatting_context::child_run_plan(&self.run,node,diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 34d22a75d6e..a24459b9422 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -1015,6 +1015,9 @@ pub(crate) enum OwnBlockSizeObservation {#[default]Observed,Unobserved,+ /// The contents never observed the root's own block size, but the absolutely positioned boxes the root contains+ /// were laid out against the content block size the run left the root with.+ ObservedOnlyByAbsposContainedByRoot,}#[derive(Clone)]@@ -2696,7 +2699,7 @@ fn lay_out_run_root_and_contents<'pass>(let laid_out_abspos_contained_by_root = run.fragmentainer.is_none()&& lay_out_abspos_contained_by_run_root(body_run, root_in_own_frame_left_by_run, own_frame);let inputs_never_observed = if laid_out_abspos_contained_by_root {- fc_run_cache::RunInputsTheStoredRunNeverObserved::default()+ inputs_never_observed.once_abspos_contained_by_root_are_laid_out()} else {inputs_never_observed};@@ -3679,6 +3682,19 @@ pub(super) fn content_block_size_adopted_after_run(}}+/// The content block size a run with `input` leaves its root with, where the parent owns the root's input cells+/// `root_cells` and the run's automatic content block size is `automatic_content_block_size`.+pub(super) fn content_block_size_run_leaves_root_whose_cells_its_parent_owns(+ input: &LayoutInput,+ root_cells: used_values::RootInputCells,+ automatic_content_block_size: CssPixels,+) -> CssPixels {+ content_block_size_adopted_after_run(input, automatic_content_block_size)+ .map_or(root_cells.content_block_size, |block_size| {+ clamp_to_max_dimension_value(block_size.max(CssPixels::default()))+ })+}+fn child_layout_from_outputs(callbacks: &LayoutPass<'_>,parent_collects_fragments: bool,diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rsindex ef0c7b3276d..84f3f3c2084 100644--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs@@ -4891,7 +4891,10 @@ impl<'pass> GridFormattingContext<'pass> {fn item_run_shape(&self, item: GridItem) -> formatting_context::ChildRunShape {let mut cells = self.cells_of(item);self.prepare_item_cells(item, &mut cells, ItemRowArea::NotYetSized);- let input = self.item_layout_input(item, &cells, ItemRowArea::NotYetSized);+ let mut input = self.item_layout_input(item, &cells, ItemRowArea::NotYetSized);+ // NB: The measured item ends its run at its automatic block size, the one a content-sized item is handed, so+ // the absolutely positioned boxes it contains are laid out as a committing run at that size lays them out.+ input.sizing.content_block_size_after_run = geometry::ContentBlockSizeAfterRun::Automatic;let plan = formatting_context::child_run_plan(&self.run,item.box_,diff --git a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rsindex c058715b0e1..eb160043acd 100644--- a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs@@ -64,10 +64,12 @@ fn unobserved_own_block_size_verification_is_enabled() -> bool {*ENABLED.get_or_init(|| std::env::var_os("LIBWEB_VERIFY_UNOBSERVED_BLOCK_SIZE").is_some())}-fn run_is_verified_at_other_unobserved_root_inputs(- own_block_size: formatting_context::OwnBlockSizeObservation,-) -> bool {- own_block_size == formatting_context::OwnBlockSizeObservation::Unobserved+fn run_is_verified_at_other_block_margins(own_block_size: formatting_context::OwnBlockSizeObservation) -> bool {+ matches!(+ own_block_size,+ formatting_context::OwnBlockSizeObservation::Unobserved+ | formatting_context::OwnBlockSizeObservation::ObservedOnlyByAbsposContainedByRoot+ )}fn root_inputs_at_other_own_block_sizes(@@ -76,9 +78,28 @@ fn root_inputs_at_other_own_block_sizes(own_block_size: formatting_context::OwnBlockSizeObservation,automatic_content_block_size: CssPixels,) -> Vec<(used_values::RootInputCells, LayoutInput)> {- if !run_is_verified_at_other_unobserved_root_inputs(own_block_size) {- return Vec::new();+ match own_block_size {+ formatting_context::OwnBlockSizeObservation::Observed => Vec::new(),+ formatting_context::OwnBlockSizeObservation::Unobserved => {+ root_inputs_at_other_unobserved_own_block_sizes(root_cells, input, automatic_content_block_size)+ }+ formatting_context::OwnBlockSizeObservation::ObservedOnlyByAbsposContainedByRoot => {+ root_inputs_at_other_definiteness_leaving_root_with_same_block_size(+ root_cells,+ input,+ automatic_content_block_size,+ )+ .into_iter()+ .collect()+ }}+}++fn root_inputs_at_other_unobserved_own_block_sizes(+ root_cells: used_values::RootInputCells,+ input: LayoutInput,+ automatic_content_block_size: CssPixels,+) -> Vec<(used_values::RootInputCells, LayoutInput)> {let mut other_definite_own_block_sizes = Vec::new();if root_cells.has_definite_block_size {other_definite_own_block_sizes.push(None);@@ -106,11 +127,47 @@ fn root_inputs_at_other_own_block_sizes(.collect()}+// NB: The absolutely positioned boxes the root contains read the block size the run leaves the root with, so the run is+// verified only where it leaves the root with the same one: definite where the root adopted its automatic block+// size, and adopting it where the root was handed that size.+fn root_inputs_at_other_definiteness_leaving_root_with_same_block_size(+ root_cells: used_values::RootInputCells,+ mut input: LayoutInput,+ automatic_content_block_size: CssPixels,+) -> Option<(used_values::RootInputCells, LayoutInput)> {+ let block_size_left = formatting_context::content_block_size_run_leaves_root_whose_cells_its_parent_owns(+ &input,+ root_cells,+ automatic_content_block_size,+ );+ if root_cells.has_definite_block_size {+ if clamp_to_max_dimension_value(automatic_content_block_size.max(CssPixels::default())) != block_size_left {+ return None;+ }+ input.available_space.block_size = AvailableSize::Indefinite;+ input.sizing.content_block_size_after_run = geometry::ContentBlockSizeAfterRun::Automatic;+ let cells = used_values::RootInputCells {+ content_block_size: CssPixels::default(),+ has_definite_block_size: false,+ ..root_cells+ };+ return Some((cells, input));+ }+ input.available_space.block_size = AvailableSize::definite(block_size_left);+ input.sizing.content_block_size_after_run = geometry::ContentBlockSizeAfterRun::HandedOver;+ let cells = used_values::RootInputCells {+ content_block_size: block_size_left,+ has_definite_block_size: true,+ ..root_cells+ };+ Some((cells, input))+}+fn root_cells_at_other_block_margins(root_cells: used_values::RootInputCells,own_block_size: formatting_context::OwnBlockSizeObservation,) -> Option<used_values::RootInputCells> {- run_is_verified_at_other_unobserved_root_inputs(own_block_size).then(|| used_values::RootInputCells {+ run_is_verified_at_other_block_margins(own_block_size).then(|| used_values::RootInputCells {margin_top: root_cells.margin_top + CssPixels::from_integer(30),margin_bottom: root_cells.margin_bottom - CssPixels::from_integer(20),..root_cells@@ -432,6 +489,48 @@ mod tests {);}+ #[test]+ fn an_own_block_size_observed_only_by_abspos_is_verified_where_the_root_is_left_with_the_same_block_size() {+ let observed_only_by_abspos = formatting_context::OwnBlockSizeObservation::ObservedOnlyByAbsposContainedByRoot;+ let (measured_cells, mut measured_input) = item_root_inputs(0, false);+ measured_input.sizing.content_block_size_after_run = geometry::ContentBlockSizeAfterRun::Automatic;+ let references =+ root_inputs_at_other_own_block_sizes(measured_cells, measured_input, observed_only_by_abspos, px(40));+ let [(cells, input)] = references.as_slice() else {+ panic!("a measured run is verified once, at the block size it adopted");+ };+ assert!(cells.has_definite_block_size);+ assert_eq!(cells.content_block_size, px(40));+ assert_eq!(input.available_space.block_size, AvailableSize::definite(px(40)));+ assert_eq!(+ input.sizing.content_block_size_after_run,+ geometry::ContentBlockSizeAfterRun::HandedOver+ );++ let (stretched_cells, stretched_input) = item_root_inputs(40, true);+ let references = root_inputs_at_other_own_block_sizes(+ stretched_cells,+ stretched_input.clone(),+ observed_only_by_abspos,+ px(40),+ );+ let [(cells, input)] = references.as_slice() else {+ panic!("a run stretched to its automatic block size is verified once, adopting it");+ };+ assert!(!cells.has_definite_block_size);+ assert_eq!(input.available_space.block_size, AvailableSize::Indefinite);+ assert_eq!(+ input.sizing.content_block_size_after_run,+ geometry::ContentBlockSizeAfterRun::Automatic+ );+ assert!(+ root_inputs_at_other_own_block_sizes(stretched_cells, stretched_input, observed_only_by_abspos, px(30))+ .is_empty(),+ "a run stretched past its automatic block size leaves the root with no other block size"+ );+ assert!(root_cells_at_other_block_margins(stretched_cells, observed_only_by_abspos).is_some());+ }+#[test]fn unobserved_block_margins_are_verified_at_other_block_margins_only() {let (root_cells, _) = item_root_inputs(40, true);diff --git a/Tests/LibWeb/Text/expected/stretched-item-run-reuse-with-contained-abspos.txt b/Tests/LibWeb/Text/expected/stretched-item-run-reuse-with-contained-abspos.txtnew file mode 100644index 00000000000..5c0b391dc29--- /dev/null+++ b/Tests/LibWeb/Text/expected/stretched-item-run-reuse-with-contained-abspos.txt@@ -0,0 +1,9 @@+grid: item runs after the width change: 1+grid: geometry matches a fresh layout: true+grid: corner sits at the item's bottom: true+taller-row: item runs after the width change: 2+taller-row: geometry matches a fresh layout: true+taller-row: corner sits at the item's bottom: true+flex: item runs after the width change: 1+flex: geometry matches a fresh layout: true+flex: corner sits at the item's bottom: truediff --git a/Tests/LibWeb/Text/input/stretched-item-run-reuse-with-contained-abspos.html b/Tests/LibWeb/Text/input/stretched-item-run-reuse-with-contained-abspos.htmlnew file mode 100644index 00000000000..63e425981b9--- /dev/null+++ b/Tests/LibWeb/Text/input/stretched-item-run-reuse-with-contained-abspos.html@@ -0,0 +1,58 @@+<!DOCTYPE html>+<script src="include.js"></script>+<style>+ body { margin: 0; font: 16px/20px monospace; }+ .grid { display: grid; grid-template: min-content 1fr / minmax(0, 1fr) min-content; width: 600px; }+ .grid > header { grid-column: 1 / 3; }+ .flex { display: flex; width: 600px; }+ .flex > .item { flex: 1; min-width: 0; }+ .item { position: relative; }+ .label { position: absolute; top: -9999px; width: 1px; height: 1px; overflow: hidden; }+ .corner { position: absolute; right: 0; bottom: 0; width: 10px; height: 10px; }+ .end { width: 50px; }+ .tall { height: 2000px; }+</style>+<div class="grid" id="grid"><header>title</header><div class="item" id="grid-item"></div><div class="end">end</div></div>+<div class="grid" id="taller-row"><header>title</header><div class="item" id="taller-row-item"></div><div class="end tall">end</div></div>+<div class="flex" id="flex"><div class="item" id="flex-item"></div><div class="end">end</div></div>+<script>+ // A stretched grid or flex item is measured for its block size before it is laid out at that size. When the item+ // is the containing block of absolutely positioned boxes, its measured run still stands for the final one as long+ // as the item ends up with the block size it was measured at. Stretched past that size, it is laid out again.+ test(() => {+ const paragraphs = Array.from({ length: 4 }, (_, index) => `<p>Paragraph ${index} ${"word ".repeat(40)}</p>`).join("");+ for (const item of document.querySelectorAll(".item"))+ item.innerHTML = `<span class="label">label</span>${paragraphs}<div class="corner"></div>`;+ document.body.offsetHeight;++ const geometry = root => {+ const rootRect = root.getBoundingClientRect();+ return Array.from(root.querySelectorAll("*")).map(element => {+ const rect = element.getBoundingClientRect();+ return `${rect.left - rootRect.left},${rect.top - rootRect.top},${rect.width},${rect.height}`;+ }).join(" ");+ };+ const matchesFreshLayout = root => {+ const fresh = root.cloneNode(true);+ fresh.id = "";+ document.body.appendChild(fresh);+ const matches = geometry(root) === geometry(fresh);+ fresh.remove();+ document.body.offsetHeight;+ return matches;+ };++ for (const id of ["grid", "taller-row", "flex"]) {+ const container = document.getElementById(id);+ const item = document.getElementById(`${id}-item`);+ internals.beginLayoutTrace();+ container.style.width = "500px";+ document.body.offsetHeight;+ const runs = internals.takeLayoutTrace().split("\n").filter(line => line.includes(`#${id}-item`) && line.includes(" RUN (")).length;+ const corner = item.querySelector(".corner").getBoundingClientRect();+ println(`${id}: item runs after the width change: ${runs}`);+ println(`${id}: geometry matches a fresh layout: ${matchesFreshLayout(container)}`);+ println(`${id}: corner sits at the item's bottom: ${corner.bottom === item.getBoundingClientRect().bottom}`);+ }+ });+</script>