Skip to content

Truly independent web browser

LibWeb: End a block formatting context root with its last child

sideshowbarker38352a7cda15
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 8c77aad84c4..e2dfee0bcff 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -350,7 +350,6 @@ pub(crate) struct RootFlowChild {
pub(crate) node: Node,
/// The block end of the child's border box, in the root's content box.
pub(crate) border_box_block_end: CssPixels,
- pub(crate) border_box_block_size: CssPixels,
pub(crate) margin_bottom: CssPixels,
pub(crate) collapses_through: bool,
pub(crate) is_invisible_for_line_clamp: bool,
@@ -361,7 +360,6 @@ impl RootFlowChild {
Self {
node,
border_box_block_end: outcome.border_box_block_end(),
- border_box_block_size: outcome.border_box_block_size(),
margin_bottom: outcome.margin_bottom(),
collapses_through: outcome.collapses_through,
is_invisible_for_line_clamp: outcome.is_invisible_for_line_clamp,
@@ -8372,38 +8370,27 @@ pub(crate) fn automatic_block_size_for_bfc_root(
let child_facts = NodeFacts::new(&pass, child.node);
child_facts.is_flow_layout_participant() && !child_facts.is_floating()
};
- let last_participant = flow_children.iter().rposition(participates);
- for (index, child) in flow_children.iter().enumerate() {
- if !participates(child) || child.is_invisible_for_line_clamp {
- continue;
- }
- // https://drafts.csswg.org/css2/#collapsing-margins
- // NB: The margins of a box that margins collapse through collapse with those of the boxes around it, so
- // the box reaches no further than the collapsed margin they all end with, which the box before it
- // accounts for, or the run's start where there's no such box.
- if child.collapses_through {
- continue;
- }
+ // AD-HOC: The spec ends the root at "the bottom margin-edge of the bottommost block-level child box". The
+ // root ends at the bottom margin edge of its last in-flow child instead, which is where its flow
+ // ends — and what pages are written to: A negative margin that pulls the last child up, over the
+ // child before it, pulls the end of the root up with it, and the child before it overflows the root.
+ // https://drafts.csswg.org/css2/#collapsing-margins
+ // NB: The margins of a box that margins collapse through collapse with those of the boxes around it, so the
+ // box reaches no further than the collapsed margin they all end with, which the box before it accounts
+ // for, or the run's start where there's no such box.
+ let last_child =
+ flow_children.iter().enumerate().rev().find(|(_, child)| {
+ participates(child) && !child.is_invisible_for_line_clamp && !child.collapses_through
+ });
+ if let Some((index, child)) = last_child {
// 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 margin_bottom = match trailing_collapsed_margin {
Some((Some(index_of_last_real_child), aggregate)) if index_of_last_real_child == index => 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(index) != last_participant
- && child.border_box_block_size == CssPixels::default()
- && formatting_context::formatting_context_type_created_by_box(NodeFacts::new(
- &pass, child.node,
- ))
- .is_none() =>
- {
- CssPixels::default()
- }
_ => child.margin_bottom,
};
- let child_bottom = child.border_box_block_end + margin_bottom;
- bottom = Some(bottom.map_or(child_bottom, |value: CssPixels| value.max(child_bottom)));
+ bottom = Some(child.border_box_block_end + margin_bottom);
}
// Where margins collapse through every in-flow child, their collapsed margin starts at the top content edge.
if let Some((None, aggregate)) = trailing_collapsed_margin {
diff --git a/Tests/LibWeb/Text/expected/bfc-root-height-ends-with-last-in-flow-child.txt b/Tests/LibWeb/Text/expected/bfc-root-height-ends-with-last-in-flow-child.txt
new file mode 100644
index 00000000000..324e1bf7704
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/bfc-root-height-ends-with-last-in-flow-child.txt
@@ -0,0 +1,27 @@
+pulled-up: 100
+pulled-up-to-the-top: 0
+pulled-up-above-the-top: 0
+tall-child-in-the-middle: 40
+then-empty-with-margins: 105
+with-bottom-margin: 115
+negative-bottom-margin: 80
+negative-bottom-margin-past-the-top: 0
+relatively-positioned: 130
+pulled-up-wrapper: 100
+negative-bottom-margin-on-tall-child: 70
+float-past-the-last-child: 90
+abspos-child: 100
+line-in-the-middle: 70
+block-in-inline: 100
+in-plain-block: 100
+overflow-hidden: 100
+overflow-auto: 100, scroll height 120
+flex-item: 100
+grid-item: 100
+inline-block: 100
+table-cell: 100
+float: 100
+abspos: 100
+table-with-caption: 110
+fieldset: 100
+vertical: 100
diff --git a/Tests/LibWeb/Text/input/bfc-root-height-ends-with-last-in-flow-child.html b/Tests/LibWeb/Text/input/bfc-root-height-ends-with-last-in-flow-child.html
new file mode 100644
index 00000000000..42bc1613de6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/bfc-root-height-ends-with-last-in-flow-child.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body { margin: 0; }
+ .root { display: flow-root; position: relative; width: 300px; font: 14px/20px monospace; }
+ .tall { height: 120px; }
+ .last { height: 10px; }
+ .float { float: left; width: 100px; }
+</style>
+<div class="root" id="pulled-up"><div class="tall"></div><div class="last" style="margin-top: -30px"></div></div>
+<div class="root" id="pulled-up-to-the-top"><div class="tall"></div><div class="last" style="margin-top: -130px"></div></div>
+<div class="root" id="pulled-up-above-the-top"><div class="tall"></div><div class="last" style="margin-top: -200px"></div></div>
+<div class="root" id="tall-child-in-the-middle"><div class="last"></div><div class="tall"></div><div class="last" style="margin-top: -100px"></div></div>
+<div class="root" id="then-empty-with-margins"><div class="tall"></div><div class="last" style="margin-top: -30px"></div><div style="margin: 5px 0"></div></div>
+<div class="root" id="with-bottom-margin"><div class="tall"></div><div class="last" style="margin-top: -30px; margin-bottom: 15px"></div></div>
+<div class="root" id="negative-bottom-margin"><div class="tall"></div><div class="last" style="margin-bottom: -50px"></div></div>
+<div class="root" id="negative-bottom-margin-past-the-top"><div class="tall"></div><div class="last" style="margin-bottom: -200px"></div></div>
+<div class="root" id="relatively-positioned"><div class="tall"></div><div class="last" style="position: relative; top: -100px"></div></div>
+<div class="root" id="pulled-up-wrapper"><div class="tall"></div><div style="margin-top: -30px"><div class="last"></div></div></div>
+<div class="root" id="negative-bottom-margin-on-tall-child"><div class="tall" style="margin-bottom: -60px"></div><div class="last"></div></div>
+<div class="root" id="float-past-the-last-child"><div class="float" style="height: 90px"></div><div class="tall"></div><div class="last" style="margin-top: -100px"></div></div>
+<div class="root" id="abspos-child"><div class="tall"></div><div class="last" style="margin-top: -30px"></div><div style="position: absolute; top: 0; height: 300px; width: 5px"></div></div>
+<div class="root" id="line-in-the-middle"><div class="tall"></div>text<div class="last" style="margin-top: -80px"></div></div>
+<div class="root" id="block-in-inline"><div class="tall"></div><span><div class="last" style="margin-top: -30px"></div></span></div>
+<div class="root" id="in-plain-block"><div><div class="tall"></div><div class="last" style="margin-top: -30px"></div></div></div>
+<div class="root" id="overflow-hidden" style="display: block; overflow: hidden"><div class="tall"></div><div class="last" style="margin-top: -30px"></div></div>
+<div class="root" id="overflow-auto" style="display: block; overflow: auto"><div class="tall"></div><div class="last" style="margin-top: -30px"></div></div>
+<div class="root" id="flex-item" style="display: flex"><div style="flex: 1"><div class="tall"></div><div class="last" style="margin-top: -30px"></div></div></div>
+<div class="root" id="grid-item" style="display: grid"><div><div class="tall"></div><div class="last" style="margin-top: -30px"></div></div></div>
+<div class="root" id="inline-block" style="display: inline-block"><div class="tall"></div><div class="last" style="margin-top: -30px"></div></div>
+<div class="root" id="table-cell" style="display: table-cell"><div class="tall"></div><div class="last" style="margin-top: -30px"></div></div>
+<div class="root" id="float" style="float: left"><div class="tall" style="width: 50px"></div><div class="last" style="margin-top: -30px"></div></div>
+<div style="clear: both"></div>
+<div class="root" id="abspos" style="position: absolute"><div class="tall" style="width: 50px"></div><div class="last" style="margin-top: -30px"></div></div>
+<table class="root" id="table-with-caption" style="display: table; border-spacing: 0"><caption style="caption-side: bottom"><div class="tall"></div><div class="last" style="margin-top: -30px"></div></caption><tr><td style="padding: 0; height: 10px"></td></tr></table>
+<fieldset class="root" id="fieldset" style="display: block; border: 0; padding: 0; margin: 0"><div class="tall"></div><div class="last" style="margin-top: -30px"></div></fieldset>
+<div class="root" id="vertical" style="writing-mode: vertical-rl; height: 60px; width: auto"><div class="tall" style="width: 120px; height: auto"></div><div class="last" style="width: 10px; height: auto; margin-right: -30px"></div></div>
+<script>
+ test(() => {
+ // The automatic height of a box that establishes a block formatting context ends where its flow ends: at the
+ // bottom margin edge of its last in-flow child, however far up a negative margin pulls that child. A child
+ // before it that reaches further down overflows the box, and can be scrolled to.
+ for (const root of document.querySelectorAll(".root")) {
+ const rect = root.getBoundingClientRect();
+ const scroll = root.id == "overflow-auto" ? `, scroll height ${root.scrollHeight}` : "";
+ println(`${root.id}: ${root.id == "vertical" ? rect.width : rect.height}${scroll}`);
+ }
+ });
+</script>