Skip to content

Truly independent web browser

LibWeb: Keep collapsed-through margins out of a BFC root's block size

Andreas Klingf7b436063b16
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 2ecb79fccf0..8893ad16e43 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -5039,9 +5039,14 @@ pub(crate) fn automatic_block_size_for_bfc_root(
let (first, last) = laid_out_children.unwrap_or_else(|| (callbacks.first_child(root), Node::INVALID));
callbacks.siblings_from(first, last).collect()
};
+ let participates = |child: Node| {
+ let child_facts = NodeFacts::new(&callbacks, child);
+ child_facts.is_flow_layout_participant() && !child_facts.is_floating()
+ };
+ let last_participant = flow_children.iter().copied().rev().find(|&child| participates(child));
for child in flow_children {
let child_facts = NodeFacts::new(&callbacks, child);
- if !child_facts.is_flow_layout_participant() || child_facts.is_floating() {
+ if !participates(child) {
continue;
}
let Some(child_used) = records.used_values_if_owned(child) else {
@@ -5053,13 +5058,22 @@ pub(crate) fn automatic_block_size_for_bfc_root(
// Margins cannot collapse out of a BFC root: below the last real in-flow
// child, the run's trailing collapsed margin (which folds in any trailing
// collapse-through siblings) replaces that child's own bottom margin.
+ let collapsed = child_used.uses_collapsing_borders_model.get();
let margin_bottom = match trailing_collapsed_margin {
Some((last_real_child, aggregate)) if last_real_child == child => aggregate,
+ // NB: The margins of an empty box collapse through it into those of the boxes after it, which
+ // are placed past them, so its own bottom margin reaches no further than theirs.
+ _ if Some(child) != last_participant
+ && child_used.border_box_block_size(collapsed) == CssPixels::default()
+ && formatting_context::formatting_context_type_created_by_box(child_facts).is_none() =>
+ {
+ CssPixels::default()
+ }
_ => child_used.margin_bottom.get(),
};
let child_bottom = child_used.content_offset.get().y
+ child_used.content_block_size.get()
- + child_used.border_box_bottom(child_used.uses_collapsing_borders_model.get())
+ + child_used.border_box_bottom(collapsed)
+ margin_bottom;
bottom = Some(bottom.map_or(child_bottom, |value: CssPixels| value.max(child_bottom)));
}
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vlr-025.xht b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vlr-025.xht
new file mode 100644
index 00000000000..73cafde1519
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vlr-025.xht
@@ -0,0 +1,60 @@
+<!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 Writing Modes Test: 'vertical-lr' and margin collapsing - collapse through empty siblings</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
+ <link rel="help" href="http://www.w3.org/TR/css-writing-modes-3/#logical-direction-layout" title="7.4 Flow-Relative Mappings" />
+ <link rel="match" href="../../../../expected/wpt-import/css/css-writing-modes/../reference/ref-filled-green-100px-square.xht" />
+
+ <!--
+ Test adapted from
+ http://test.csswg.org/suites/css2.1/nightly-unstable/html4/margin-collapse-016.htm
+
+ "
+ The margin collapsing rules apply exactly with the block-start margin substituted for the top margin and the block-end margin substituted for the bottom margin.
+ "
+ -->
+
+ <meta content="ahem image" name="flags" />
+ <meta name="assert" content="This test checks that margins can collapse through empty elements in 'vertical-lr' writing-mode." />
+
+ <link rel="stylesheet" type="text/css" href="../../fonts/ahem.css" />
+ <style type="text/css"><![CDATA[
+ div
+ {
+ font: 25px/1em Ahem;
+ height: 4em;
+ }
+
+ div#wrapper
+ {
+ background-image: url("support/margin-collapse-2em-space-wm-vert.png");
+ background-position: -1em top;
+ border-left: green solid 1em;
+ border-right: green solid 1em;
+ writing-mode: vertical-lr;
+ }
+
+ div.widthless
+ {
+ margin-right: 2em;
+ }
+ ]]></style>
+
+ </head>
+
+ <body>
+
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+ <div id="wrapper">
+ <div class="widthless"></div>
+ <div class="widthless"></div>
+ </div>
+
+ </body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vlr-031.xht b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vlr-031.xht
new file mode 100644
index 00000000000..5e2e7a2362a
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vlr-031.xht
@@ -0,0 +1,64 @@
+<!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 Writing Modes Test: 'vertical-lr' and margin collapsing - collapse through sibling</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
+ <link rel="help" href="http://www.w3.org/TR/css-writing-modes-3/#logical-direction-layout" title="7.4 Flow-Relative Mappings" />
+ <link rel="match" href="../../../../expected/wpt-import/css/css-writing-modes/../reference/ref-filled-green-100px-square.xht" />
+
+ <meta content="ahem image" name="flags" />
+ <meta name="assert" content="This test checks that horizontal margins of boxes can collapse through a sibling box whose left and right margins collapse in 'vertical-lr' writing-mode." />
+
+ <link rel="stylesheet" type="text/css" href="../../fonts/ahem.css" />
+ <style type="text/css"><![CDATA[
+ div
+ {
+ font: 25px/1em Ahem;
+ height: 4em;
+ }
+
+ div#wrapper
+ {
+ background-image: url("support/margin-collapse-2em-space-wm-vert.png");
+ writing-mode: vertical-lr;
+ }
+
+ div#left-most-sibling
+ {
+ background-color: green;
+ margin-right: 2em;
+ width: 1em;
+ }
+
+ div#middle-sibling
+ {
+ margin-left: 2em;
+ margin-right: 2em;
+ }
+
+ div#right-most-sibling
+ {
+ background-color: green;
+ margin-left: 2em;
+ width: 1em;
+ }
+ ]]></style>
+
+ </head>
+
+ <body>
+
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+ <div id="wrapper">
+ <div id="left-most-sibling"></div>
+ <div id="middle-sibling"></div>
+ <div id="right-most-sibling"></div>
+ </div>
+
+ </body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vrl-024.xht b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vrl-024.xht
new file mode 100644
index 00000000000..cdb8020e5fc
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vrl-024.xht
@@ -0,0 +1,60 @@
+<!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 Writing Modes Test: 'vertical-rl' and margin collapsing - collapse through empty siblings</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
+ <link rel="help" href="http://www.w3.org/TR/css-writing-modes-3/#logical-direction-layout" title="7.4 Flow-Relative Mappings" />
+ <link rel="match" href="../../../../expected/wpt-import/css/css-writing-modes/../reference/ref-filled-green-100px-square.xht" />
+
+ <!--
+ Test adapted from
+ http://test.csswg.org/suites/css2.1/nightly-unstable/html4/margin-collapse-016.htm
+
+ "
+ The margin collapsing rules apply exactly with the block-start margin substituted for the top margin and the block-end margin substituted for the bottom margin.
+ "
+ -->
+
+ <meta content="ahem image" name="flags" />
+ <meta name="assert" content="This test checks that margins can collapse through empty elements in 'vertical-rl' writing-mode." />
+
+ <link rel="stylesheet" type="text/css" href="../../fonts/ahem.css" />
+ <style type="text/css"><![CDATA[
+ div
+ {
+ font: 25px/1em Ahem;
+ height: 4em;
+ }
+
+ div#wrapper
+ {
+ background-image: url("support/margin-collapse-2em-space-wm-vert.png");
+ background-position: -1em top;
+ border-left: green solid 1em;
+ border-right: green solid 1em;
+ writing-mode: vertical-rl;
+ }
+
+ div.widthless
+ {
+ margin-left: 2em;
+ }
+ ]]></style>
+
+ </head>
+
+ <body>
+
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+ <div id="wrapper">
+ <div class="widthless"></div>
+ <div class="widthless"></div>
+ </div>
+
+ </body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vrl-030.xht b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vrl-030.xht
new file mode 100644
index 00000000000..a0fcbf2e352
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/margin-collapse-vrl-030.xht
@@ -0,0 +1,64 @@
+<!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 Writing Modes Test: 'vertical-rl' and margin collapsing - collapse through sibling</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
+ <link rel="help" href="http://www.w3.org/TR/css-writing-modes-3/#logical-direction-layout" title="7.4 Flow-Relative Mappings" />
+ <link rel="match" href="../../../../expected/wpt-import/css/css-writing-modes/../reference/ref-filled-green-100px-square.xht" />
+
+ <meta content="ahem image" name="flags" />
+ <meta name="assert" content="This test checks that horizontal margins of boxes can collapse through a sibling box whose left and right margins collapse in 'vertical-rl' writing-mode." />
+
+ <link rel="stylesheet" type="text/css" href="../../fonts/ahem.css" />
+ <style type="text/css"><![CDATA[
+ div
+ {
+ font: 25px/1em Ahem;
+ height: 4em;
+ }
+
+ div#wrapper
+ {
+ background-image: url("support/margin-collapse-2em-space-wm-vert.png");
+ writing-mode: vertical-rl;
+ }
+
+ div#right-most-sibling
+ {
+ background-color: green;
+ margin-left: 2em;
+ width: 1em;
+ }
+
+ div#middle-sibling
+ {
+ margin-left: 2em;
+ margin-right: 2em;
+ }
+
+ div#left-most-sibling
+ {
+ background-color: green;
+ margin-right: 2em;
+ width: 1em;
+ }
+ ]]></style>
+
+ </head>
+
+ <body>
+
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+ <div id="wrapper">
+ <div id="right-most-sibling"></div>
+ <div id="middle-sibling"></div>
+ <div id="left-most-sibling"></div>
+ </div>
+
+ </body>
+</html>