Skip to content

Truly independent web browser

LibWeb: End a scroll container’s in-flow content where its flow ends

sideshowbarker2ba4110cd5b0
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index a78a5504db0..568f0b299a6 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -715,6 +715,36 @@ impl BlockMarginState {
}
}
+ /// Collapses the margins after a block-level box of the flow: what the contents of the box left in their margin
+ /// state, `margins_left_by_contents`, where the box has contents in the flow, and its block-end margin. Returns the
+ /// margin the flow leaves past the block end of the box: its block-end margin, collapsed with the margins of its
+ /// contents that collapse through its block end. For a box that margins collapse through, which sits past the
+ /// margins before it, that is how much its block-end margin adds to them.
+ fn collapse_margins_after_block_level_box(
+ &mut self,
+ margins_left_by_contents: Option<&Self>,
+ collapses_through: bool,
+ continues_in_next_fragment: bool,
+ margin_bottom: CssPixels,
+ ) -> CssPixels {
+ if let Some(contents) = margins_left_by_contents {
+ self.take_in_margins_left_by_contents(contents);
+ }
+ let last_in_flow_child_margin_bottom_collapsed =
+ margins_left_by_contents.is_some_and(|contents| contents.box_last_in_flow_child_margin_bottom_collapsed);
+ if !collapses_through && (!last_in_flow_child_margin_bottom_collapsed || continues_in_next_fragment) {
+ self.reset();
+ }
+ let margin_before_block_end_margin = self.current_collapsed_margin();
+ self.add_margin(margin_bottom);
+ self.update_open_top_margin_group();
+ if collapses_through {
+ self.current_collapsed_margin() - margin_before_block_end_margin
+ } else {
+ self.current_collapsed_margin()
+ }
+ }
+
/// The block-start margin of a block container in this margin state, whose contents collapsed `group` into it.
fn block_start_margin_collapsing(&self, group: TopMarginGroup) -> CssPixels {
self.margins_since_reset
@@ -1521,17 +1551,39 @@ impl<'pass> BlockFormattingContext<'pass> {
continues_in_next_fragment: bool,
margin_bottom: CssPixels,
) {
- let mut margin_state = self.margin_state_mut();
- if let Some(contents) = margins_left_by_contents {
- margin_state.take_in_margins_left_by_contents(contents);
- }
- let last_in_flow_child_margin_bottom_collapsed =
- margins_left_by_contents.is_some_and(|contents| contents.box_last_in_flow_child_margin_bottom_collapsed);
- if !collapses_through && (!last_in_flow_child_margin_bottom_collapsed || continues_in_next_fragment) {
- margin_state.reset();
- }
- margin_state.add_margin(margin_bottom);
- margin_state.update_open_top_margin_group();
+ self.margin_state_mut().collapse_margins_after_block_level_box(
+ margins_left_by_contents,
+ collapses_through,
+ continues_in_next_fragment,
+ margin_bottom,
+ );
+ }
+
+ /// The margin this flow leaves past the block end of a block-level box, which collapsing the margins after the box
+ /// answers (see [`BlockMarginState::collapse_margins_after_block_level_box`]), before they are collapsed.
+ /// `contents_are_in_this_flow` says if the innermost margin state is that of the box's contents.
+ fn block_end_margin_collapsed_after_block_level_box(
+ &self,
+ contents_are_in_this_flow: bool,
+ collapses_through: bool,
+ continues_in_next_fragment: bool,
+ margin_bottom: CssPixels,
+ ) -> CssPixels {
+ let states = self.margin_states.borrow();
+ let (enclosing, margins_left_by_contents) = if contents_are_in_this_flow {
+ let [.., enclosing, contents] = states.as_slice() else {
+ panic!("a block container's contents have a margin state of their own");
+ };
+ (enclosing, Some(contents))
+ } else {
+ (states.last().expect("a flow has a margin state"), None)
+ };
+ enclosing.clone().collapse_margins_after_block_level_box(
+ margins_left_by_contents,
+ collapses_through,
+ continues_in_next_fragment,
+ margin_bottom,
+ )
}
/// Takes out the top margin group of the innermost margin state, whose margins collapse into the block-start margin
@@ -4546,11 +4598,27 @@ impl<'pass> BlockFormattingContext<'pass> {
let node = sized_child.node;
let has_independent_formatting_context =
formatting_context::formatting_context_type_created_by_box(self.facts(node)).is_some();
- let placed_child = child_to_place.with_offset_from_relatively_positioned_inline_ancestors(
- interrupted_inline_content.map_or(FfiCssPixelPoint::default(), |interrupted| {
- interrupted.offset_from_relatively_positioned_inline_ancestors
- }),
- );
+ let margins_collapse_through_without_clearance = !has_independent_formatting_context
+ && self.margins_collapse_through(sized_child, false, summary_of_children_laid_out_in_this_flow.as_ref());
+ // NB: Clearance separates the margins of a box, whatever its contents left of theirs.
+ let collapses_through = !introduced_clearance && margins_collapse_through_without_clearance;
+ // NB: A child its own run laid out has the metrics that run handed back, and any other those of its cells here.
+ let metrics = match &laid_out_box_handed_back_by_its_run {
+ Some(handed_back) => handed_back.cells,
+ None => self.metrics_of(sized_child),
+ };
+ let placed_child = child_to_place
+ .with_offset_from_relatively_positioned_inline_ancestors(
+ interrupted_inline_content.map_or(FfiCssPixelPoint::default(), |interrupted| {
+ interrupted.offset_from_relatively_positioned_inline_ancestors
+ }),
+ )
+ .with_block_end_margin_collapsed_in_flow(self.block_end_margin_collapsed_after_block_level_box(
+ !has_independent_formatting_context,
+ collapses_through,
+ fragment_facts.breaks.continues_in_next,
+ metrics.margin_bottom(),
+ ));
let mut deferred_legend = None;
let mut placed = match pending_position {
Some(position) => formatting_context::place_child(
@@ -4580,15 +4648,6 @@ impl<'pass> BlockFormattingContext<'pass> {
}
}
- let margins_collapse_through_without_clearance = !has_independent_formatting_context
- && self.margins_collapse_through(sized_child, false, summary_of_children_laid_out_in_this_flow.as_ref());
- // NB: Clearance separates the margins of a box, whatever its contents left of theirs.
- let collapses_through = !introduced_clearance && margins_collapse_through_without_clearance;
- // NB: A child its own run laid out has the metrics that run handed back, and any other those of its cells here.
- let metrics = match &laid_out_box_handed_back_by_its_run {
- Some(handed_back) => handed_back.cells,
- None => self.metrics_of(sized_child),
- };
let margins_left_by_contents =
(!has_independent_formatting_context).then(|| self.leave_margin_state_of_contents(node));
if !collapses_through {
@@ -5162,6 +5221,14 @@ impl<'pass> BlockFormattingContext<'pass> {
x: entry.content_inline_offset,
y: self.flow_position() + block_start_margin + metrics.border_box_top(false),
};
+ // NB: Clearance separates the margins of a box, whatever its contents left of theirs.
+ let collapses_through = !introduces_clearance && entry.margins_collapse_through_without_clearance;
+ let block_end_margin_collapsed_in_flow = self.margin_state().clone().collapse_margins_after_block_level_box(
+ Some(&margins_left_by_contents),
+ collapses_through,
+ false,
+ metrics.margin_bottom(),
+ );
// NB: A balancing probe builds no fragments, so it only finds where the box goes.
let placed = formatting_context::place_child(
&self.run,
@@ -5170,7 +5237,8 @@ impl<'pass> BlockFormattingContext<'pass> {
cells: self.metrics_of(sized_child),
line_data: root.fragment.line_data.clone(),
})
- .with_in_flow_box_insets(insets),
+ .with_in_flow_box_insets(insets)
+ .with_block_end_margin_collapsed_in_flow(block_end_margin_collapsed_in_flow),
content_offset,
None,
self.run.fragments.is_some().then_some(root),
@@ -5187,8 +5255,6 @@ impl<'pass> BlockFormattingContext<'pass> {
})
.collect(),
);
- // NB: Clearance separates the margins of a box, whatever its contents left of theirs.
- let collapses_through = !introduces_clearance && entry.margins_collapse_through_without_clearance;
if !collapses_through {
self.block_offset_of_current_block_container.set(Some(
content_offset.y
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index c2bbab422c5..34ffe6c85a7 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -468,6 +468,15 @@ impl PlacedChild {
self
}
+ /// The child, a block-level box of a block container's flow, with the margin that flow leaves past its block end.
+ pub(crate) fn with_block_end_margin_collapsed_in_flow(mut self, margin: CssPixels) -> Self {
+ self.facts_decided_by_parent
+ .fragment_facts
+ .show
+ .block_end_margin_collapsed_in_flow = Some(margin);
+ self
+ }
+
/// The child, a block-level box, with the offset the relative positioning of the inline boxes it interrupts gives
/// it.
pub(crate) fn with_offset_from_relatively_positioned_inline_ancestors(mut self, offset: FfiCssPixelPoint) -> Self {
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
index ddcddfaac21..2c0a687b32b 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
@@ -10,7 +10,7 @@ use crate::css::css_pixels::{CssPixelPoint, CssPixelRect, CssPixels};
use crate::css::display::FfiDisplay;
use crate::layout::LayoutNodeArena;
use crate::layout::fragment_tree::{Fragment, FragmentLink, OuterLinks, SvgPayload};
-use crate::layout::geometry::{Frame, Sides};
+use crate::layout::geometry::{Frame, Side, Sides};
use crate::layout::inline_content::{InlineContent, PhysicalPlane};
use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};
use crate::layout::node_facts::{self, NodeShape};
@@ -377,6 +377,7 @@ pub(crate) fn scrollable_overflow_of<'link>(
// its 3D rendering context. [CSS3-TRANSFORMS]
let container_inline_axis_is_horizontal =
style.is_none_or(|style| style.writing_mode() == writing_mode::HORIZONTAL_TB);
+ let container_frame = Frame::of(style.map_or(writing_mode::HORIZONTAL_TB, |style| style.writing_mode()));
for link in contained_links {
let child = &link.fragment;
let child_node = child.node;
@@ -461,6 +462,19 @@ pub(crate) fn scrollable_overflow_of<'link>(
} else {
(zero, zero)
};
+ // NB: Past the block end of a box in the flow of a block container, the margin that counts is
+ // what that flow collapsed there, not the box's own: The margins of the box's contents that
+ // collapse through its block end count too, and those that collapse through the box itself
+ // count once.
+ let [top, right, bottom, left] =
+ match link.placement.shown_as.facts.block_end_margin_collapsed_in_flow {
+ Some(block_end_margin) => {
+ let mut margins = [top, right, bottom, left];
+ margins[container_frame.physical_side(Side::Bottom) as usize] = block_end_margin;
+ margins
+ }
+ None => [top, right, bottom, left],
+ };
let (left, right) = if overflow_directions.horizontal_axis_is_positive {
(left.max(zero), right.max(-border_box.width))
} else {
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
index fa55dad714c..540ebc3c913 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
@@ -345,6 +345,11 @@ pub(crate) struct ParentShowFacts {
/// for a box hidden after a clamp point, and the area before the block-end content edge of a line-clamp container
/// for a float inside it.
pub(crate) clip_decided_by_parent: Option<CssPixelRect>,
+ /// For a block-level box in the flow of a block container: the margin that flow leaves past the block end of the
+ /// box, which is its block-end margin, collapsed with the margins of its contents that collapse through its block
+ /// end. For a box that margins collapse through, which sits past the margins before it, that is how much its
+ /// block-end margin adds to them. The scrollable overflow of a scroll container ends its in-flow content there.
+ pub(crate) block_end_margin_collapsed_in_flow: Option<CssPixels>,
}
impl ParentShowFacts {
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index fe41492c0ad..a7f7990dd79 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -1292,7 +1292,9 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
return;
}
let has_fragments = !self.line(line_index).fragments.is_empty();
- if has_fragments {
+ // NB: A line that holds nothing but a forced break is a line box all the same, so the margins before it end
+ // there as they do before any other.
+ if has_fragments || self.line(line_index).has_forced_break {
self.prepare_to_append_inline_content();
}
if self.line(line_index).takes_inline_box_struts() {
diff --git a/Tests/LibWeb/Text/expected/scrollable-overflow-collapsed-in-flow-margins.txt b/Tests/LibWeb/Text/expected/scrollable-overflow-collapsed-in-flow-margins.txt
new file mode 100644
index 00000000000..c6908e7266c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scrollable-overflow-collapsed-in-flow-margins.txt
@@ -0,0 +1,29 @@
+text-then-empty: 34 34
+block-then-empty: 48 48
+block-in-inline-then-break-then-empty: 82 82
+block-in-inline-then-break: 68 68
+block-in-inline-then-empty: 48 48
+block-in-inline-then-text-then-empty: 82 82
+block-in-inline-then-empty-in-inline: 48 48
+text-then-break-then-empty: 34 34
+block-then-break-then-empty: 82 82
+only-empty: 14 14
+empty-then-block: 48 48
+block-then-two-empty: 48 48
+empty-with-larger-top-margin: 64 64
+empty-with-larger-bottom-margin: 64 64
+empty-with-negative-top-margin: 59 59
+empty-in-block: 48 48
+block-in-block: 48 48
+empty-with-clear: 48 48
+padding: 62 62
+padding-around-block-in-block: 62 62
+overflowing-block-in-block: 64 112
+overflowing-then-empty: 64 112
+overflowing-then-two-empty: 64 128
+overflowing-only-empty-after-block: 64 94
+not-overflowing-empty: 64 64
+overflowing-text-then-empty: 64 98
+overflowing-block-in-inline-then-break-then-empty: 64 146
+vertical: 48 48
+in-flex-item: 48 48
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-overflow/scrollable-overflow-padding.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-overflow/scrollable-overflow-padding.txt
index fa911e759d2..0c11c2a544b 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-overflow/scrollable-overflow-padding.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-overflow/scrollable-overflow-padding.txt
@@ -2,18 +2,17 @@ Harness status: OK
Found 30 tests
-28 Pass
-2 Fail
+30 Pass
Pass scrollable-container 1
Pass scrollable-container 2
Pass scrollable-container 3
Pass scrollable-container 4
-Fail scrollable-container 5
+Pass scrollable-container 5
Pass scrollable-container 6
Pass scrollable-container 7
Pass scrollable-container 8
Pass scrollable-container 9
-Fail scrollable-container 10
+Pass scrollable-container 10
Pass scrollable-container 11
Pass scrollable-container 12
Pass scrollable-container 13
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-overflow/scrollable-overflow-self-collapsing.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-overflow/scrollable-overflow-self-collapsing.txt
new file mode 100644
index 00000000000..bf827165e2e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-overflow/scrollable-overflow-self-collapsing.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass .target 1
+Pass .target 2
+Pass .target 3
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/scrollable-overflow-collapsed-in-flow-margins.html b/Tests/LibWeb/Text/input/scrollable-overflow-collapsed-in-flow-margins.html
new file mode 100644
index 00000000000..8646e181467
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scrollable-overflow-collapsed-in-flow-margins.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ .scroller { width: 300px; overflow: auto; font: 14px/20px sans-serif; }
+ p { margin: 14px 0; }
+ .padded { height: 50px; padding: 7px; }
+ .block { height: 50px; }
+</style>
+<div class="scroller" id="text-then-empty">Text<p></p></div>
+<div class="scroller" id="block-then-empty"><p>Text</p><p></p></div>
+<div class="scroller" id="block-in-inline-then-break-then-empty"><span><p>Text</p></span><br><p></p></div>
+<div class="scroller" id="block-in-inline-then-break"><span><p>Text</p></span><br></div>
+<div class="scroller" id="block-in-inline-then-empty"><span><p>Text</p></span><p></p></div>
+<div class="scroller" id="block-in-inline-then-text-then-empty"><span><p>Text</p></span>x<p></p></div>
+<div class="scroller" id="block-in-inline-then-empty-in-inline"><span><p>Text</p></span><span><p></p></span></div>
+<div class="scroller" id="text-then-break-then-empty">Text<br><p></p></div>
+<div class="scroller" id="block-then-break-then-empty"><p>Text</p><br><p></p></div>
+<div class="scroller" id="only-empty"><p></p></div>
+<div class="scroller" id="empty-then-block"><p></p><p>Text</p></div>
+<div class="scroller" id="block-then-two-empty"><p>Text</p><p></p><p></p></div>
+<div class="scroller" id="empty-with-larger-top-margin"><p>Text</p><p style="margin: 30px 0 5px"></p></div>
+<div class="scroller" id="empty-with-larger-bottom-margin"><p>Text</p><p style="margin: 5px 0 30px"></p></div>
+<div class="scroller" id="empty-with-negative-top-margin"><p>Text</p><p style="margin: -5px 0 30px"></p></div>
+<div class="scroller" id="empty-in-block"><p>Text</p><div><p></p></div></div>
+<div class="scroller" id="block-in-block"><div><p>Text</p></div></div>
+<div class="scroller" id="empty-with-clear"><p>Text</p><p style="clear: both"></p></div>
+<div class="scroller" id="padding" style="padding: 7px"><p>Text</p><p></p></div>
+<div class="scroller" id="padding-around-block-in-block" style="padding: 7px"><div><p>Text</p></div></div>
+<div class="scroller padded" id="overflowing-block-in-block"><div><div class="block"></div><p>Text</p></div></div>
+<div class="scroller padded" id="overflowing-then-empty"><div class="block"></div><p>Text</p><p></p></div>
+<div class="scroller padded" id="overflowing-then-two-empty"><div class="block"></div><p>Text</p><p style="margin: 5px 0 30px"></p><p></p></div>
+<div class="scroller padded" id="overflowing-only-empty-after-block"><div class="block"></div><p style="margin: 5px 0 30px"></p></div>
+<div class="scroller padded" id="not-overflowing-empty"><p></p></div>
+<div class="scroller padded" id="overflowing-text-then-empty"><div class="block"></div>Text<p></p></div>
+<div class="scroller padded" id="overflowing-block-in-inline-then-break-then-empty"><div class="block"></div><span><p>Text</p></span><br><p></p></div>
+<div class="scroller" id="vertical" style="writing-mode: vertical-rl; width: auto; height: 100px"><p style="margin: 0 14px">Text</p><p style="margin: 0 14px"></p></div>
+<div class="scroller" id="in-flex-item" style="display: flex; flex-direction: column"><div style="overflow: auto"><p>Text</p><p></p></div></div>
+<script>
+ test(() => {
+ // The in-flow content of a scroll container ends where its flow does: after the margins that collapse at the end
+ // of it, counted once. So a box whose height fits its content has nothing to scroll, whatever margins collapse
+ // through its last children.
+ for (const scroller of document.querySelectorAll(".scroller")) {
+ if (scroller.id == "in-flex-item")
+ println(`${scroller.id}: ${scroller.firstChild.clientHeight} ${scroller.firstChild.scrollHeight}`);
+ else if (scroller.id == "vertical")
+ println(`${scroller.id}: ${scroller.clientWidth} ${scroller.scrollWidth}`);
+ else
+ println(`${scroller.id}: ${scroller.clientHeight} ${scroller.scrollHeight}`);
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-overflow/scrollable-overflow-self-collapsing.html b/Tests/LibWeb/Text/input/wpt-import/css/css-overflow/scrollable-overflow-self-collapsing.html
new file mode 100644
index 00000000000..36b063a6c1f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-overflow/scrollable-overflow-self-collapsing.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<meta name="assert" content="This ensures that an end self-collapsing block contributes to the scrollable overflow correctly.">
+<link rel="help" href="https://drafts.csswg.org/css-overflow-3/#scrollable" />
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../resources/check-layout-th.js"></script>
+<body onload="checkLayout('.target')">
+ <div class="target" style="width: 100px; height: 100px; overflow: scroll;" data-expected-scroll-height="110">
+ <div style="width: 50px; height: 100px; margin-bottom: 10px; background: lime;"></div>
+ <div></div> <!-- self-collapsing -->
+ </div>
+ <div class="target" style="width: 100px; height: 100px; overflow: scroll;" data-expected-scroll-height="150">
+ <div style="width: 50px; height: 100px; margin-bottom: 10px; background: lime;"></div>
+ <div style="margin-bottom: 50px;"></div> <!-- self-collapsing -->
+ </div>
+ <div class="target" style="width: 100px; height: 100px; overflow: scroll;" data-expected-scroll-height="180">
+ <div style="width: 50px; height: 100px; margin-bottom: 30px; background: lime;"></div>
+ <div style="margin-bottom: 100px;"> <!-- self-collapsing -->
+ <div style="margin-top: -20px;"></div>
+ </div>
+ </div>
+ <div id=log></div>
+</body>