Skip to content

Truly independent web browser

LibWeb: Keep the margin of empty blocks in a box padded at both ends

sideshowbarker801b9892adda
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 551592c72d8..cf4b5c4af5f 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -8457,6 +8457,20 @@ impl<'pass> BlockFormattingContext<'pass> {
}
return (last_in_flow_child.border_box_block_end + margin_bottom).max(CssPixels::default());
}
+ // NB: Where margins collapse through every in-flow child, and that collapsed margin can't collapse with this
+ // box's top margin either, it's the margin of the last of those children: Unless it collapses with this
+ // box's bottom margin as well, the box runs to its bottom edge, from the block-start content edge.
+ if !facts.children_are_inline()
+ && children.is_some_and(|children| !children.pending_margins_collapse_through_block_start)
+ {
+ let margin_bottom = self.margin_state_mut().collapsed_margin_at_block_end_of(&facts);
+ let used = self.cells_laid_out_here(sized_child);
+ if margin_bottom != CssPixels::default()
+ && (used.padding_bottom() != CssPixels::default() || used.border_bottom() != CssPixels::default())
+ {
+ return margin_bottom.max(CssPixels::default());
+ }
+ }
// AD-HOC: Contenteditable elements must have a minimum block size (line-height) when empty, to remain clickable
// and usable for text input, even though this is not specified.
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-002-ref.xht b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-002-ref.xht
new file mode 100644
index 00000000000..e85f0a0bdab
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-002-ref.xht
@@ -0,0 +1,30 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+
+ <head>
+
+ <title>CSS Reftest Reference</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
+
+ <style type="text/css"><![CDATA[
+ div
+ {
+ border-bottom: green solid 20px;
+ border-top: green solid 20px;
+ height: 40px;
+ width: 100px;
+ }
+ ]]></style>
+
+ </head>
+
+ <body>
+
+ <p>Test passes if there is <strong>no red</strong>.</p>
+
+ <div></div>
+
+ </body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-016.xht b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-016.xht
new file mode 100644
index 00000000000..454f764524a
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-016.xht
@@ -0,0 +1,52 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>CSS Test: Margin collapsing - top/bottom margins adjoining collapse through</title>
+ <link rel="author" title="Microsoft" href="http://www.microsoft.com/" />
+ <link rel="reviewer" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" /> <!-- 2012-08-15 -->
+ <link rel="help" href="http://www.w3.org/TR/CSS21/box.html#collapsing-margins" />
+ <link rel="match" href="../../../../../expected/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-002-ref.xht" />
+
+ <meta name="flags" content="ahem image" />
+ <meta name="assert" content="When top and bottom margins are adjoining margins collapse through that element." />
+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+ <style type="text/css">
+ div
+ {
+ font: 20px/1em Ahem;
+ width: 5em;
+ }
+ #div1
+ {
+ background: url('support/margin-collapse-2em-space.png');
+ height: 4em;
+ }
+ #div2, #div4
+ {
+ background:green;
+ height: 1em;
+ }
+ #div3
+ {
+ background: red;
+ height: 0;
+ }
+ #div2, #div3
+ {
+ margin-bottom: 2em;
+ }
+ #div3, #div4
+ {
+ margin-top: 2em;
+ }
+ </style>
+ </head>
+ <body>
+ <p>Test passes if there is <strong>no red</strong>.</p>
+ <div id="div1">
+ <div id="div2"></div>
+ <div id="div3"></div>
+ <div id="div4"></div>
+ </div>
+ </body>
+</html>
\ No newline at end of file
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-019.xht b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-019.xht
new file mode 100644
index 00000000000..c730a622cac
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-019.xht
@@ -0,0 +1,55 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>CSS Test: Margin collapsing - elements that collapse margins have to effect on positions of other elements that also have margins that are collapsing</title>
+ <link rel="author" title="Microsoft" href="http://www.microsoft.com/" />
+ <link rel="reviewer" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" /> <!-- 2012-08-15 -->
+ <link rel="help" href="http://www.w3.org/TR/CSS21/box.html#collapsing-margins" />
+ <link rel="match" href="../../../../../expected/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-002-ref.xht" />
+
+ <meta name="flags" content="ahem image" />
+ <meta name="assert" content="Positions of elements that have been collapsed through (empty elements) have no effect on positions of other elements whose margins are being collapsed." />
+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+ <style type="text/css">
+ div
+ {
+ font: 20px/1em Ahem;
+ width: 5em;
+ }
+ div div
+ {
+ height: 1em;
+ }
+ #div1
+ {
+ background: url('support/margin-collapse-2em-space.png') left -1em;
+ border-top: 1em solid green;
+ }
+ #div2
+ {
+ background: red;
+ }
+ #div3
+ {
+ background: red;
+ height: 0;
+ margin-top: 2em;
+ margin-bottom: 2em;
+ }
+ #div4
+ {
+ background: green;
+ margin-top: 2em;
+ }
+ </style>
+ </head>
+ <body>
+ <p>Test passes if there is <strong>no red</strong>.</p>
+ <div id="div1">
+ <div id="div2">
+ <div id="div3"></div>
+ <div id="div4"></div>
+ </div>
+ </div>
+ </body>
+</html>
\ No newline at end of file
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-026.xht b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-026.xht
new file mode 100644
index 00000000000..32bee1ac5f1
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-026.xht
@@ -0,0 +1,51 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>CSS Test: Margin collapsing - in-flow block-level margins with an element 'height' of 'auto' and 'min-height' of zero</title>
+ <link rel="author" title="Microsoft" href="http://www.microsoft.com/" />
+ <link rel="help" href="http://www.w3.org/TR/CSS21/box.html#collapsing-margins" />
+ <link rel="match" href="../../../../../expected/wpt-import/css/CSS2/margin-padding-clear/margin-collapse-002-ref.xht" />
+
+ <meta name="flags" content="ahem image" />
+ <meta name="assert" content="An element's margins are adjoining and can collapse if the 'min-height' is zero, if it has no top or bottom borders and no top or bottom padding, if it has a 'height' of zero or 'auto', if it has no line boxes and if all of its in-flow children's margins are adjoining." />
+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+ <style type="text/css">
+ div
+ {
+ font: 20px/1em Ahem;
+ width: 5em;
+ }
+ #div1
+ {
+ background: url('support/margin-collapse-2em-space.png');
+ height: 4em;
+ }
+ #div3
+ {
+ background: red;
+ height: auto;
+ min-height: 0;
+ }
+ #div2, #div5
+ {
+ background: green;
+ height: 1em;
+ }
+ #div3, #div4
+ {
+ margin-bottom: 2em;
+ margin-top: 2em;
+ }
+ </style>
+ </head>
+ <body>
+ <p>Test passes if there is <strong>no red</strong>.</p>
+ <div id="div1">
+ <div id="div2"></div>
+ <div id="div3">
+ <div id="div4"></div>
+ </div>
+ <div id="div5"></div>
+ </div>
+ </body>
+</html>
\ No newline at end of file
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/support/margin-collapse-2em-space.png b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/support/margin-collapse-2em-space.png
new file mode 100644
index 00000000000..2c381ef4dc8
Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/margin-padding-clear/support/margin-collapse-2em-space.png differ
diff --git a/Tests/LibWeb/Text/expected/margin-collapsed-through-children-stays-in-a-padded-box.txt b/Tests/LibWeb/Text/expected/margin-collapsed-through-children-stays-in-a-padded-box.txt
new file mode 100644
index 00000000000..35c82ecaa8a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/margin-collapsed-through-children-stays-in-a-padded-box.txt
@@ -0,0 +1,13 @@
+padding: 90
+padding-both-margins: 90
+border: 32
+two-children: 90
+mixed-signs: 80
+negative: 60
+nested: 90
+padding-bottom-only: 30
+padding-top-only: 30
+border-bottom-only: 1
+after-a-box: 100
+flow-root: 90
+flow-root-no-padding: 30
diff --git a/Tests/LibWeb/Text/input/margin-collapsed-through-children-stays-in-a-padded-box.html b/Tests/LibWeb/Text/input/margin-collapsed-through-children-stays-in-a-padded-box.html
new file mode 100644
index 00000000000..36c3a26e1b0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/margin-collapsed-through-children-stays-in-a-padded-box.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ .t { margin-bottom: 20px; width: 300px; }
+</style>
+<!-- The margin that collapses through a box's only children counts toward its height where it can collapse with
+ neither its top nor its bottom margin. -->
+<div class="t" id="padding" style="padding: 30px 0"><div style="margin-top: 30px"></div></div>
+<div class="t" id="padding-both-margins" style="padding: 30px 0"><div style="margin: 30px 0"></div></div>
+<div class="t" id="border" style="border: 1px solid; border-width: 1px 0"><div style="margin-top: 30px"></div></div>
+<div class="t" id="two-children" style="padding: 30px 0"><div style="margin-top: 30px"></div><div style="margin-bottom: 20px"></div></div>
+<div class="t" id="mixed-signs" style="padding: 30px 0"><div style="margin-top: 30px"></div><div style="margin-top: -10px"></div></div>
+<div class="t" id="negative" style="padding: 30px 0"><div style="margin-top: -30px"></div></div>
+<div class="t" id="nested" style="padding: 30px 0"><div><div style="margin-top: 30px"></div></div></div>
+<!-- Where it can collapse with the box's top or bottom margin, it doesn't. -->
+<div class="t" id="padding-bottom-only" style="padding-bottom: 30px"><div style="margin-top: 30px"></div></div>
+<div class="t" id="padding-top-only" style="padding-top: 30px"><div style="margin-top: 30px"></div></div>
+<div class="t" id="border-bottom-only" style="border-bottom: 1px solid"><div style="margin-top: 30px"></div></div>
+<!-- A box before the empty one, and a flow root, count it as before. -->
+<div class="t" id="after-a-box" style="padding: 30px 0"><div style="height: 10px"></div><div style="margin-top: 30px"></div></div>
+<div class="t" id="flow-root" style="display: flow-root; padding: 30px 0"><div style="margin-top: 30px"></div></div>
+<div class="t" id="flow-root-no-padding" style="display: flow-root"><div style="margin-top: 30px"></div></div>
+<script>
+ test(() => {
+ for (const box of document.querySelectorAll(".t"))
+ println(`${box.id}: ${box.getBoundingClientRect().height}`);
+ });
+</script>