Truly independent web browser
LibWeb: Reuse in-flow children that margins collapse through
Andreas Kling78c9095fe378
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex 44765ce71ea..5b03aae5fea 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -711,6 +711,7 @@ struct ReusedInFlowChild {content_offset: FfiCssPixelPoint,margin_box_block_end: CssPixels,introduced_clearance: bool,+ collapses_through: bool,placed: Option<fragment_tree::PlacedFragment>,}@@ -863,6 +864,8 @@ fn lay_out_in_flow_child_ahead(pass: LayoutPass<'_>, job: InFlowChildJob) -> Opt),outgoing_outside_marker_line: outcome.outgoing_outside_marker_line,depends_on_percentage_block_size: outcome.depends_on_percentage_block_size,+ // NB: Margins collapse through no box whose border box has a block size.+ margins_collapse_through_without_clearance: false,fragment_identity: root.fragment.identity,};Some(InFlowChildLaidOutAhead {@@ -3440,6 +3443,7 @@ impl<'pass> BlockFormattingContext<'pass> {return Some(LaidOutBlockLevelBox {content_offset: Some(reused.content_offset),margin_box_block_end: Some(reused.margin_box_block_end),+ collapses_through: reused.collapses_through,..LaidOutBlockLevelBox::of_box(Self::laid_out_box_of_box_laid_out_here(sized_child, entry.baselines),fragment_facts,@@ -4133,12 +4137,10 @@ impl<'pass> BlockFormattingContext<'pass> {}}- let collapses_through = !has_independent_formatting_context- && self.margins_collapse_through(- sized_child,- introduced_clearance,- summary_of_children_laid_out_in_this_flow.as_ref(),- );+ let margins_collapse_through_without_clearance = !has_independent_formatting_context+ && self.margins_collapse_through(sized_child, false, summary_of_children_laid_out_in_this_flow.as_ref());+ // NB: Clearance separates the margins of a box, whatever its contents left of theirs.+ let collapses_through = !introduced_clearance && margins_collapse_through_without_clearance;// NB: A child its own run laid out has the metrics that run handed back, and any other those of its cells here.let metrics = match &laid_out_box_handed_back_by_its_run {Some(handed_back) => handed_back.cells,@@ -4174,6 +4176,7 @@ impl<'pass> BlockFormattingContext<'pass> {depends_on_percentage_block_size,margins_left_by_contents,broken_token_of_box.is_some(),+ margins_collapse_through_without_clearance,);let broken_inside = broken_token_of_box.map(|token| BrokenInside {token,@@ -4716,14 +4719,18 @@ impl<'pass> BlockFormattingContext<'pass> {}).collect(),);- self.block_offset_of_current_block_container.set(Some(- content_offset.y- + metrics.content_block_size()- + metrics.border_box_bottom(metrics.uses_collapsing_borders_model()),- ));+ // NB: Clearance separates the margins of a box, whatever its contents left of theirs.+ let collapses_through = !introduces_clearance && entry.margins_collapse_through_without_clearance;+ if !collapses_through {+ self.block_offset_of_current_block_container.set(Some(+ content_offset.y+ + metrics.content_block_size()+ + metrics.border_box_bottom(metrics.uses_collapsing_borders_model()),+ ));+ }self.collapse_margins_after_block_level_box(Some(&margins_left_by_contents),- false,+ collapses_through,false,metrics.margin_bottom(),);@@ -4733,6 +4740,7 @@ impl<'pass> BlockFormattingContext<'pass> {+ metrics.content_block_size()+ metrics.margin_box_bottom(metrics.uses_collapsing_borders_model()),introduced_clearance: introduces_clearance,+ collapses_through,placed,}}@@ -4796,6 +4804,7 @@ impl<'pass> BlockFormattingContext<'pass> {depends_on_percentage_block_size: bool,margins_left_by_contents: Option<BlockMarginState>,broke_inside: bool,+ margins_collapse_through_without_clearance: bool,) {let node = sized_child.node;let InFlowReuse::Store {@@ -4829,13 +4838,10 @@ impl<'pass> BlockFormattingContext<'pass> {return;};let used = self.cells_laid_out_here(sized_child);- // NB: A reused child tells its parent that margins do not collapse through it, which holds only for a child- // whose border box has a block size.let reusable = !placed.exports_payloads&& self.abspos_containing_block_info_contributions_received()== abspos_containing_block_info_contributions_received- && self.run.pass.host_findings_noted_so_far() == host_findings_noted_so_far- && used.border_box_block_size(false) != CssPixels::default();+ && self.run.pass.host_findings_noted_so_far() == host_findings_noted_so_far;if !reusable {return;}@@ -4871,6 +4877,7 @@ impl<'pass> BlockFormattingContext<'pass> {),outgoing_outside_marker_line: self.outside_marker_line.get(),depends_on_percentage_block_size,+ margins_collapse_through_without_clearance,fragment_identity: placed.fragment.identity,};if let Some(fragmentainer) = self.run.fragmentainer.as_ref() {@@ -5203,6 +5210,7 @@ impl<'pass> BlockFormattingContext<'pass> {Some(LaidOutBlockLevelBox {content_offset: Some(reused.content_offset),margin_box_block_end: Some(reused.margin_box_block_end),+ collapses_through: reused.collapses_through,..LaidOutBlockLevelBox::of_box(Self::laid_out_box_of_box_laid_out_here(sized_child, baselines),fragment_facts,diff --git a/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs b/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rsindex e679ee7df98..6d8342f4eea 100644--- a/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs+++ b/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs@@ -93,6 +93,8 @@ pub(super) struct InFlowBlockEntry {pub(super) floats_placed_by_contents: Vec<block_formatting_context::FloatingBox>,pub(super) outgoing_outside_marker_line: Option<block_formatting_context::OutsideMarkerLine>,pub(super) depends_on_percentage_block_size: bool,+ /// Whether margins collapse through the child where placing it introduces no clearance.+ pub(super) margins_collapse_through_without_clearance: bool,/// The fragment the stored layout placed. The committed subtree is reused only while it is still that fragment.pub(super) fragment_identity: u64,}diff --git a/Tests/LibWeb/Text/expected/Layout/relayout-with-margins-collapsing-through-a-box-that-stops-clearing.txt b/Tests/LibWeb/Text/expected/Layout/relayout-with-margins-collapsing-through-a-box-that-stops-clearing.txtnew file mode 100644index 00000000000..937fbebc6a9--- /dev/null+++ b/Tests/LibWeb/Text/expected/Layout/relayout-with-margins-collapsing-through-a-box-that-stops-clearing.txt@@ -0,0 +1,5 @@+initial: float=0 target=0 clearing=50 last=70 height=90+BlockContainer<DIV>#clearing REUSE IN-FLOW SUBTREE+after change: float=0 target=0 clearing=100 last=100 height=120+after rebuild: float=0 target=0 clearing=100 last=100 height=120+PASSdiff --git a/Tests/LibWeb/Text/expected/Layout/relayout-with-margins-collapsing-through-siblings.txt b/Tests/LibWeb/Text/expected/Layout/relayout-with-margins-collapsing-through-siblings.txtnew file mode 100644index 00000000000..c594fd5f0ab--- /dev/null+++ b/Tests/LibWeb/Text/expected/Layout/relayout-with-margins-collapsing-through-siblings.txt@@ -0,0 +1,11 @@+initial: target=0 empty=50 float-only=50 nested=70 clearing=90 negative=94 last=102 height=122+BlockContainer<DIV>#empty REUSE IN-FLOW SUBTREE+BlockContainer<DIV>#float-only REUSE IN-FLOW SUBTREE+BlockContainer<DIV>#nested REUSE IN-FLOW SUBTREE+BlockContainer<DIV>#clearing REUSE IN-FLOW SUBTREE+BlockContainer<DIV>#negative REUSE IN-FLOW SUBTREE+BlockContainer<DIV>#last REUSE IN-FLOW SUBTREE+after change: target=0 empty=67 float-only=67 nested=87 clearing=107 negative=111 last=119 height=139+after rebuild: target=0 empty=67 float-only=67 nested=87 clearing=107 negative=111 last=119 height=139+PASS+changed back: target=0 empty=50 float-only=50 nested=70 clearing=90 negative=94 last=102 height=122diff --git a/Tests/LibWeb/Text/expected/layout-tree-update/append-leaves-other-anonymous-wrappers-clean.txt b/Tests/LibWeb/Text/expected/layout-tree-update/append-leaves-other-anonymous-wrappers-clean.txtindex 55a0e89b873..862deb5d00f 100644--- a/Tests/LibWeb/Text/expected/layout-tree-update/append-leaves-other-anonymous-wrappers-clean.txt+++ b/Tests/LibWeb/Text/expected/layout-tree-update/append-leaves-other-anonymous-wrappers-clean.txt@@ -6,5 +6,6 @@ BlockContainer(anonymous) REUSE IN-FLOW SUBTREEBlockContainer<DIV> REUSE IN-FLOW SUBTREEBlockContainer(anonymous) REUSE IN-FLOW SUBTREEBlockContainer<DIV> REUSE IN-FLOW SUBTREE+BlockContainer<PRE>#out REUSE IN-FLOW SUBTREEgeometry matches a fresh layout: truetree matches a full rebuild: truediff --git a/Tests/LibWeb/Text/input/Layout/relayout-with-margins-collapsing-through-a-box-that-stops-clearing.html b/Tests/LibWeb/Text/input/Layout/relayout-with-margins-collapsing-through-a-box-that-stops-clearing.htmlnew file mode 100644index 00000000000..c3dcce6b628--- /dev/null+++ b/Tests/LibWeb/Text/input/Layout/relayout-with-margins-collapsing-through-a-box-that-stops-clearing.html@@ -0,0 +1,46 @@+<!DOCTYPE html>+<style>+ body { margin: 0; }+ #container { width: 400px; }+ #target { height: 10px; }+ #float { float: left; width: 40px; height: 50px; }+ #clearing { clear: left; margin: 20px 0; }+ #last { height: 20px; margin-top: 5px; }+</style>+<script src="../include.js"></script>+<div id="container">+ <div id="float"></div>+ <div id="target"></div>+ <div id="clearing"></div>+ <div id="last"></div>+</div>+<script>+ test(() => {+ const container = document.getElementById("container");+ const target = document.getElementById("target");+ const positions = () =>+ [...container.children].map(child => `${child.id}=${child.offsetTop}`).join(" ") ++ ` height=${container.offsetHeight}`;++ println(`initial: ${positions()}`);++ // The box beside the float grows past it, so the box that margins collapse through no longer clears it. The box+ // keeps its own layout, which was stored with clearance and is now placed without it.+ internals.beginLayoutTrace();+ target.style.height = "80px";+ const afterChange = positions();+ for (const line of internals.takeLayoutTrace().split("\n")) {+ if (line.includes("#clearing") && line.includes("REUSE"))+ println(line.trim());+ }+ println(`after change: ${afterChange}`);++ // A rebuilt layout tree has nothing kept from the layout before.+ container.style.display = "none";+ container.offsetHeight;+ container.style.display = "";+ const afterRebuild = positions();+ println(`after rebuild: ${afterRebuild}`);+ println(afterChange === afterRebuild ? "PASS" : "FAIL");+ });+</script>diff --git a/Tests/LibWeb/Text/input/Layout/relayout-with-margins-collapsing-through-siblings.html b/Tests/LibWeb/Text/input/Layout/relayout-with-margins-collapsing-through-siblings.htmlnew file mode 100644index 00000000000..d090e4465bf--- /dev/null+++ b/Tests/LibWeb/Text/input/Layout/relayout-with-margins-collapsing-through-siblings.html@@ -0,0 +1,55 @@+<!DOCTYPE html>+<style>+ body { margin: 0; }+ #container { width: 400px; }+ #target { height: 20px; margin-bottom: 10px; }+ #empty { margin: 30px 0; }+ #float-only { margin-top: 15px; margin-bottom: 25px; }+ #float-only > div { float: left; width: 50px; height: 40px; }+ #nested > div { margin: 50px 0; }+ #clearing { clear: both; margin: 12px 0; }+ #negative { margin-top: -8px; margin-bottom: 20px; }+ #last { height: 20px; margin-top: 5px; }+</style>+<script src="../include.js"></script>+<div id="container">+ <div id="target"></div>+ <div id="empty"></div>+ <div id="float-only"><div></div></div>+ <div id="nested"><div></div></div>+ <div id="clearing"></div>+ <div id="negative"></div>+ <div id="last"></div>+</div>+<script>+ test(() => {+ const container = document.getElementById("container");+ const target = document.getElementById("target");+ const positions = () =>+ [...container.children].map(child => `${child.id}=${child.offsetTop}`).join(" ") ++ ` height=${container.offsetHeight}`;++ println(`initial: ${positions()}`);++ // Only the first child changes, so the siblings after it keep their own layout and only move.+ internals.beginLayoutTrace();+ target.style.height = "37px";+ const afterChange = positions();+ for (const line of internals.takeLayoutTrace().split("\n")) {+ if (line.includes("REUSE"))+ println(line.trim());+ }+ println(`after change: ${afterChange}`);++ // A rebuilt layout tree has nothing kept from the layout before.+ container.style.display = "none";+ container.offsetHeight;+ container.style.display = "";+ const afterRebuild = positions();+ println(`after rebuild: ${afterRebuild}`);+ println(afterChange === afterRebuild ? "PASS" : "FAIL");++ target.style.height = "20px";+ println(`changed back: ${positions()}`);+ });+</script>