Skip to content

Truly independent web browser

LibWeb: Leave margins collapsed through a box's top out of its height

Aliaksandr Kalenikf6a7af76403a
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 6ded8b8e440..839c0657a1c 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -269,6 +269,9 @@ pub(crate) struct ChildrenSummary {
/// Whether margins do not collapse through one of the children laid out, which keeps them from collapsing through
/// the container.
pub(crate) has_child_whose_margins_do_not_collapse_through: bool,
+ /// Whether no content of the container closed the top margin group its children collapse into, so the margins
+ /// still pending after them collapse through the container's block-start edge.
+ pub(crate) pending_margins_collapse_through_block_start: bool,
}
#[derive(Clone, Copy)]
@@ -3175,6 +3178,8 @@ impl<'pass> BlockFormattingContext<'pass> {
if let Some(outside_marker) = &outside_marker_laid_out_before_contents {
self.finish_contents_of_list_item(node, outside_marker, &mut children_summary);
}
+ children_summary.pending_margins_collapse_through_block_start =
+ self.margin_state.borrow().has_open_top_margin_group();
baselines_of_contents_laid_out_in_this_flow = Some(children_summary.baselines);
summary_of_children_laid_out_in_this_flow = Some(children_summary);
if is_in_flow_candidate {
@@ -5601,7 +5606,12 @@ impl<'pass> BlockFormattingContext<'pass> {
let mut block_size = last_line.physical_vertical_end();
if last_line.has_block_level_box {
let mut margin_state = self.margin_state.borrow_mut();
- let mut margin_bottom = margin_state.current_collapsed_margin();
+ let mut margin_bottom =
+ if children.is_some_and(|children| children.pending_margins_collapse_through_block_start) {
+ CssPixels::default()
+ } else {
+ margin_state.current_collapsed_margin()
+ };
let metrics = self.metrics_of(sized_child);
if metrics.padding_bottom() == CssPixels::default() && metrics.border_bottom() == CssPixels::default() {
margin_state.box_last_in_flow_child_margin_bottom_collapsed = true;
diff --git a/Tests/LibWeb/Layout/expected/block-and-inline/margins-collapsed-through-top-of-box-with-block-in-inline.txt b/Tests/LibWeb/Layout/expected/block-and-inline/margins-collapsed-through-top-of-box-with-block-in-inline.txt
new file mode 100644
index 00000000000..816ecd75921
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/block-and-inline/margins-collapsed-through-top-of-box-with-block-in-inline.txt
@@ -0,0 +1,20 @@
+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 423 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [0,0] [0+0+0 800 0+0+0] [0+0+0 323 0+0+0] children: not-inline
+ BlockContainer <div.scenario> at [0,0] [0+0+0 200 0+0+600] [0+0+0 31 0+0+100] [BFC] children: not-inline
+ BlockContainer <div#in-inline.box> at [0,30] [0+0+0 200 0+0+0] [30+0+0 0 0+1+0] children: inline
+ InlineNode <span> at [0,30] [0+0+0 0 0+0+0] [0+0+0 0 0+0+0]
+ frag 0 from BlockContainer start: 0, length: 0, rect: [0,30 200x0] baseline: 0
+ BlockContainer <div.empty> at [0,30] [0+0+0 200 0+0+0] [10+0+0 0 0+0+10] children: not-inline
+ BlockContainer <div.scenario> at [0,131] [0+0+0 200 0+0+600] [0+0+0 61 0+0+100] [BFC] children: not-inline
+ BlockContainer <div.before> at [0,131] [0+0+0 200 0+0+0] [0+0+0 10 0+0+50] children: not-inline
+ BlockContainer <div#in-inline-after-sibling.box> at [0,191] [0+0+0 200 0+0+0] [30+0+0 0 0+1+0] children: inline
+ InlineNode <span> at [0,191] [0+0+0 0 0+0+0] [0+0+0 0 0+0+0]
+ frag 0 from BlockContainer start: 0, length: 0, rect: [0,191 200x0] baseline: 0
+ BlockContainer <div.empty> at [0,191] [0+0+0 200 0+0+0] [10+0+0 0 0+0+10] children: not-inline
+ BlockContainer <div.scenario> at [0,292] [0+0+0 200 0+0+600] [0+0+0 31 0+0+100] [BFC] children: not-inline
+ BlockContainer <div#block-child.box> at [0,322] [0+0+0 200 0+0+0] [30+0+0 0 0+1+0] children: not-inline
+ BlockContainer <div.empty> at [0,322] [0+0+0 200 0+0+0] [10+0+0 0 0+0+10] children: not-inline
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x423] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/input/block-and-inline/margins-collapsed-through-top-of-box-with-block-in-inline.html b/Tests/LibWeb/Layout/input/block-and-inline/margins-collapsed-through-top-of-box-with-block-in-inline.html
new file mode 100644
index 00000000000..3e6cf3b10a5
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/block-and-inline/margins-collapsed-through-top-of-box-with-block-in-inline.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<style>
+* { margin: 0; }
+body { font: 10px/20px SerenitySans; }
+.scenario { display: flow-root; width: 200px; margin-bottom: 100px; }
+.box { margin-top: 30px; border-bottom: 1px solid black; }
+.empty { margin: 10px 0; }
+.before { height: 10px; margin-bottom: 50px; }
+</style>
+<div class="scenario"><div class="box" id="in-inline"><span><div class="empty"></div></span></div></div>
+<div class="scenario"><div class="before"></div><div class="box" id="in-inline-after-sibling"><span><div class="empty"></div></span></div></div>
+<div class="scenario"><div class="box" id="block-child"><div class="empty"></div></div></div>