Skip to content

Truly independent web browser

LibWeb: Keep sticky boxes in the content box of their containing block

Andreas Kling08dd50d007a8
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/refresh.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/refresh.rs
index 3bce856998c..6caf02be2b6 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_context/refresh.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/refresh.rs
@@ -10,6 +10,7 @@ use crate::css::computed_value_types::ComputedLengthPercentageOrAuto;
use crate::css::css_pixels::{CssPixelPoint, CssPixelRect, CssPixelSize, CssPixels};
use crate::layout::node_data::NodeSlotId;
use crate::painting::host::FfiVisualContextTreeInputs;
+use crate::painting::paintable_data::FfiPixelBox;
use crate::painting::paintable_geometry;
use crate::painting::paintable_rows::PaintableRowsRead;
use crate::painting::style_queries;
@@ -66,6 +67,41 @@ fn resolve_sticky_insets_in_css_pixels(
}
}
+// NB: The computed margins, with auto as zero. The used margins of a block include the space its width leaves free.
+fn computed_margins_in_css_pixels(
+ layout_arena: &impl PaintableRowsRead,
+ slot: NodeSlotId,
+ percentage_basis: CssPixels,
+) -> FfiPixelBox {
+ let Some(style) = layout_arena.node_style_if_live(slot) else {
+ return FfiPixelBox::default();
+ };
+ let resolve = |side: &ComputedLengthPercentageOrAuto| {
+ if side.is_auto() {
+ CssPixels::from_raw(0)
+ } else {
+ side.to_px(percentage_basis)
+ }
+ };
+ let margin = &style.surround().margin;
+ FfiPixelBox {
+ top: resolve(&margin.top),
+ right: resolve(&margin.right),
+ bottom: resolve(&margin.bottom),
+ left: resolve(&margin.left),
+ }
+}
+
+fn deflated(rect: CssPixelRect, edges: FfiPixelBox) -> CssPixelRect {
+ let zero = CssPixels::from_raw(0);
+ CssPixelRect::new(
+ rect.x + edges.left,
+ rect.y + edges.top,
+ (rect.width - edges.left - edges.right).max(zero),
+ (rect.height - edges.top - edges.bottom).max(zero),
+ )
+}
+
fn is_containing_block_descendant_of(
layout_arena: &impl PaintableRowsRead,
slot: NodeSlotId,
@@ -125,19 +161,21 @@ pub(crate) fn compute_sticky_data(
let sticky_border_box_rect = paintable_geometry::absolute_border_box_rect(layout_arena, paintable);
let containing_block = layout_arena.paintable_data(paintable).containing_block;
let scroller_rect = paintable_geometry::absolute_rect(layout_arena, scroller);
- let (containing_block_region, needs_parent_offset_adjustment) = if containing_block == scroller {
- let size = paintable_geometry::scrollable_overflow_rect(layout_arena, containing_block)
- .unwrap_or_else(|| paintable_geometry::absolute_padding_box_rect(layout_arena, containing_block))
- .size();
- (
- CssPixelRect::from_location_and_size(CssPixelPoint::default(), size),
- false,
- )
+ // NB: The box moves within the content box of its containing block, less its own margins. For the scroll
+ // container itself, that is its scrollable overflow rectangle less its padding. This matches other engines.
+ let (containing_block_content_box, needs_parent_offset_adjustment) = if containing_block == scroller {
+ let overflow_rect = paintable_geometry::scrollable_overflow_rect(layout_arena, containing_block)
+ .unwrap_or_else(|| paintable_geometry::absolute_padding_box_rect(layout_arena, containing_block));
+ let padding = paintable_geometry::committed_padding(layout_arena, containing_block);
+ (deflated(overflow_rect, padding), false)
} else {
- let region = paintable_geometry::absolute_border_box_rect(layout_arena, containing_block)
- .translated(-scroller_rect.x, -scroller_rect.y);
- (region, true)
+ (paintable_geometry::absolute_rect(layout_arena, containing_block), true)
};
+ let containing_block_width = paintable_geometry::absolute_rect(layout_arena, containing_block).width;
+ let containing_block_region = deflated(
+ containing_block_content_box.translated(-scroller_rect.x, -scroller_rect.y),
+ computed_margins_in_css_pixels(layout_arena, paintable, containing_block_width),
+ );
let scale = device_pixels_per_css_pixel as f32;
let device_point = |point: CssPixelPoint| FloatPoint {
diff --git a/Tests/LibWeb/Text/expected/position-sticky-containing-block-content-box.txt b/Tests/LibWeb/Text/expected/position-sticky-containing-block-content-box.txt
new file mode 100644
index 00000000000..8757ad6d8e8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/position-sticky-containing-block-content-box.txt
@@ -0,0 +1,4 @@
+without-overflow: 110
+with-overflow: 140
+padded-containing-block: 30
+sticky-margin: 15
diff --git a/Tests/LibWeb/Text/input/position-sticky-containing-block-content-box.html b/Tests/LibWeb/Text/input/position-sticky-containing-block-content-box.html
new file mode 100644
index 00000000000..8c6f0fac13e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/position-sticky-containing-block-content-box.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<style>
+body { margin: 0; }
+.scroller { overflow: auto; padding: 20px; height: 100px; width: 300px; }
+.sticky { position: sticky; top: 120px; height: 10px; }
+</style>
+<div class="scroller" id="without-overflow">
+ <div class="sticky"></div>
+</div>
+<div class="scroller" id="with-overflow">
+ <div class="sticky"></div>
+ <div style="height: 300px"></div>
+</div>
+<div class="scroller" id="padded-containing-block">
+ <div style="padding-bottom: 30px; height: 50px"><div class="sticky" style="top: 80px"></div></div>
+ <div style="height: 300px"></div>
+</div>
+<div class="scroller" id="sticky-margin">
+ <div style="height: 50px"><div class="sticky" style="top: 80px; margin-bottom: 15px"></div></div>
+ <div style="height: 300px"></div>
+</div>
+<script src="include.js"></script>
+<script>
+test(() => {
+ for (const id of ["without-overflow", "with-overflow", "padded-containing-block", "sticky-margin"]) {
+ const scroller = document.getElementById(id);
+ scroller.scrollTop = 30;
+ const top = scroller.querySelector(".sticky").getBoundingClientRect().top - scroller.getBoundingClientRect().top;
+ println(`${id}: ${top}`);
+ }
+});
+</script>