Truly independent web browser
LibWeb: Keep a float-avoiding box’s top margin out of its parent
sideshowbarker6bc66e0bd4a7
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex cf4b5c4af5f..00255b9071f 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -230,6 +230,8 @@ struct BlockLevelBoxLayoutPrelude {/// Where placing the box introduced clearance, the margins between the clearance and the box: its block-start/// margin, collapsed with those of its first children.margins_after_clearance: Option<CollapsedMargins>,+ /// The open top margin group before the child adds its block-start margin.+ top_margins_before_child: Option<CollapsedMargins>,}/// A block-level box its flow laid out and has yet to place.@@ -2959,10 +2961,60 @@ impl<'pass> BlockFormattingContext<'pass> {(metrics.border_box_block_size(false), !metrics.has_definite_block_size())}+ /// Places the box past floats, removing its margin from the parent's top margin group if it has to move down.+ #[allow(clippy::too_many_arguments)]+ fn place_in_flow_box_past_floats(+ &self,+ sized_child: ChildSizedHere<'_>,+ block_container: BlockContainer,+ input: &LayoutInput,+ content_block_offset: CssPixels,+ containing_block_rect_in_root_now: &mut CssPixelRect,+ top_margins_before_child: &mut Option<CollapsedMargins>,+ ) -> InFlowBoxPlacement {+ let placement = self.compute_in_flow_box_placement(+ sized_child,+ block_container,+ input,+ content_block_offset,+ *containing_block_rect_in_root_now,+ );+ if placement.content_block_offset <= content_block_offset {+ return placement;+ }+ let Some(margins) = top_margins_before_child.take() else {+ return placement;+ };++ // A box pushed down by floats stops collapsing its top margin into its parent. Keep the margins that joined+ // the group before this child, and place the box past floats from the parent's position without its margin.+ let node = sized_child.node;+ let previous_adjustment = self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node);+ {+ let mut margin_state = self.margin_state_mut();+ margin_state+ .top_margin_group+ .as_mut()+ .expect("the child's margin joined a top margin group")+ .margins = margins;+ margin_state.reset();+ }+ containing_block_rect_in_root_now.y +=+ self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node) - previous_adjustment;+ self.rebuild_float_bands();+ self.compute_in_flow_box_placement(+ sized_child,+ block_container,+ input,+ content_block_offset,+ *containing_block_rect_in_root_now,+ )+ }+/// Finds where `sized_child`, a block-level box of this flow, goes from `content_block_offset` on, past the floats/// it avoids, and the inline size it takes there, which its cells take.#[allow(clippy::too_many_arguments)]- fn place_in_flow_box_past_floats(+ fn compute_in_flow_box_placement(&self,sized_child: ChildSizedHere<'_>,block_container: BlockContainer,@@ -4283,6 +4335,12 @@ impl<'pass> BlockFormattingContext<'pass> {let is_in_flow_candidate = self.run.fragmentainer.is_some() && self.is_first_or_last_in_flow_candidate(node);let discards_block_start_margin =is_in_flow_candidate && self.discard_block_start_margin_of_next_child.replace(false);+ let top_margins_before_child = {+ let margin_state = self.margin_state();+ margin_state+ .has_open_top_margin_group()+ .then_some(margin_state.margins_since_reset)+ };// A quirky block-start margin that collapses into the block start of a quirks-mode body or table cell is left// out, by clearance too.let block_start_margin_is_quirky = self.facts(node).has_quirky_block_start_margin();@@ -4375,6 +4433,7 @@ impl<'pass> BlockFormattingContext<'pass> {discards_block_start_margin,introduced_clearance,margins_after_clearance,+ top_margins_before_child: top_margins_before_child.filter(|_| !introduced_clearance),},);Some(self.place_laid_out_block_level_box(sized_child, unplaced, in_flow_reuse, interrupted_inline_content))@@ -4396,7 +4455,8 @@ impl<'pass> BlockFormattingContext<'pass> {definite_content_block_size_of_fieldset_to_fill: Option<CssPixels>,inline_space_used_before_children_formatted: inline_formatting_context::SpaceUsedByFloats,content_block_offset_before_floats: CssPixels,- containing_block_rect_in_root_now: CssPixelRect,+ mut containing_block_rect_in_root_now: CssPixelRect,+ top_margins_before_child: &mut Option<CollapsedMargins>,placement: &mut InFlowBoxPlacement,flow_places_box: bool,) -> (formatting_context::ChildLayout, Option<(CssPixels, CssPixels)>) {@@ -4486,7 +4546,8 @@ impl<'pass> BlockFormattingContext<'pass> {block_container,input,content_block_offset_before_floats,- containing_block_rect_in_root_now,+ &mut containing_block_rect_in_root_now,+ top_margins_before_child,);if placement_beside_all_floats == *placement {break (layout, pre_run_table_border_box);@@ -4517,6 +4578,7 @@ impl<'pass> BlockFormattingContext<'pass> {discards_block_start_margin,introduced_clearance,margins_after_clearance,+ mut top_margins_before_child,} = prelude;let node = sized_child.node;let available_space = input.available_space;@@ -4549,22 +4611,23 @@ impl<'pass> BlockFormattingContext<'pass> {block_offset + margin_top + self.metrics_of(sized_child).border_box_top(false);let containing_block_rect_in_root =self.containing_block_rect(block_container, containing_block_position_in_root);- let containing_block_rect_in_root_now = containing_block_rect_in_root.translated(+ let mut containing_block_rect_in_root_now = containing_block_rect_in_root.translated(CssPixels::default(),self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node),);- let content_position_in_root_now = |content_block_offset: CssPixels| FfiCssPixelPoint {- x: containing_block_rect_in_root_now.x,- y: containing_block_rect_in_root_now.y + content_block_offset,- };let mut placement = self.place_in_flow_box_past_floats(sized_child,block_container,&input,content_block_offset_before_floats,- containing_block_rect_in_root_now,+ &mut containing_block_rect_in_root_now,+ &mut top_margins_before_child,);+ let content_position_in_root_now = |content_block_offset: CssPixels| FfiCssPixelPoint {+ x: containing_block_rect_in_root_now.x,+ y: containing_block_rect_in_root_now.y + content_block_offset,+ };let InFlowBoxPlacement {content_inline_offset,content_block_offset,@@ -4673,6 +4736,7 @@ impl<'pass> BlockFormattingContext<'pass> {inline_space_used_before_children_formatted,content_block_offset_before_floats,containing_block_rect_in_root_now,+ &mut top_margins_before_child,&mut placement,pending_position.is_some(),);diff --git a/Tests/LibWeb/Text/expected/bfc-root-pushed-past-float-keeps-its-negative-margin-out-of-its-parent.txt b/Tests/LibWeb/Text/expected/bfc-root-pushed-past-float-keeps-its-negative-margin-out-of-its-parent.txtnew file mode 100644index 00000000000..2e200e5060d--- /dev/null+++ b/Tests/LibWeb/Text/expected/bfc-root-pushed-past-float-keeps-its-negative-margin-out-of-its-parent.txt@@ -0,0 +1,12 @@+flex (auto height): section y=40 height=60; child y=40 height=60+flow-root (auto height): section y=40 height=60; child y=40 height=60+ordinary-block (auto height): section y=-36 height=60; child y=-36 height=60+positive-margin (auto height): section y=40 height=120; child y=100 height=60+fits-beside-float (auto height): section y=-36 height=60; child y=-36 height=60+earlier-empty-sibling (auto height): section y=44 height=60; child y=44 height=60+flex (definite height): section y=40 height=60; child y=40 height=60+flow-root (definite height): section y=40 height=60; child y=40 height=60+ordinary-block (definite height): section y=-36 height=60; child y=-36 height=60+positive-margin (definite height): section y=40 height=120; child y=100 height=60+fits-beside-float (definite height): section y=-36 height=60; child y=-36 height=60+earlier-empty-sibling (definite height): section y=44 height=60; child y=44 height=60diff --git a/Tests/LibWeb/Text/input/bfc-root-pushed-past-float-keeps-its-negative-margin-out-of-its-parent.html b/Tests/LibWeb/Text/input/bfc-root-pushed-past-float-keeps-its-negative-margin-out-of-its-parent.htmlnew file mode 100644index 00000000000..975aa27a997--- /dev/null+++ b/Tests/LibWeb/Text/input/bfc-root-pushed-past-float-keeps-its-negative-margin-out-of-its-parent.html@@ -0,0 +1,52 @@+<!doctype html>+<meta charset="utf-8">+<style>+ body { margin: 0; }+ .fixture { position: absolute; top: 0; left: 0; width: 200px; }+ .before { padding: 20px 0; }+ .float { float: left; width: 100%; height: 1px; }+ .child { margin-top: -76px; width: 80%; }+ .content { height: 60px; }+ .empty { margin-top: 12px; margin-bottom: -8px; }+</style>+<script src="include.js"></script>+<script>+test(() => {+ const cases = [+ { name: "flex", display: "flex" },+ { name: "flow-root", display: "flow-root" },+ { name: "ordinary-block", display: "block" },+ { name: "positive-margin", display: "flow-root", margin: "20px", floatHeight: "80px" },+ { name: "fits-beside-float", display: "flow-root", floatWidth: "20%" },+ { name: "earlier-empty-sibling", display: "flow-root", emptySibling: true },+ ];+ for (const definiteHeight of [false, true]) {+ for (const entry of cases) {+ const fixture = document.createElement("div");+ fixture.className = "fixture";+ fixture.innerHTML = `+ <div class="before"><div class="float"></div></div>+ <section>+ ${entry.emptySibling ? '<div class="empty"></div>' : ""}+ <div class="child"><div class="content"></div></div>+ </section>`;+ const float = fixture.querySelector(".float");+ float.style.width = entry.floatWidth ?? "100%";+ float.style.height = entry.floatHeight ?? "1px";+ const section = fixture.querySelector("section");+ const child = fixture.querySelector(".child");+ child.style.display = entry.display;+ child.style.marginTop = entry.margin ?? "-76px";+ if (definiteHeight)+ child.style.height = "60px";+ document.body.appendChild(fixture);+ const sectionRect = section.getBoundingClientRect();+ const childRect = child.getBoundingClientRect();+ println(`${entry.name} (${definiteHeight ? "definite" : "auto"} height): `+ + `section y=${sectionRect.y} height=${sectionRect.height}; `+ + `child y=${childRect.y} height=${childRect.height}`);+ fixture.remove();+ }+ }+});+</script>