Truly independent web browser
LibWeb: Stick to the nearest scroll container even without overflow
Andreas Kling1cfe5f72fbb5
diff --git a/Libraries/LibCompositing/Rust/src/ffi.rs b/Libraries/LibCompositing/Rust/src/ffi.rsindex 8235c3653ad..f472f0cb6d7 100644--- a/Libraries/LibCompositing/Rust/src/ffi.rs+++ b/Libraries/LibCompositing/Rust/src/ffi.rs@@ -723,6 +723,7 @@ pub unsafe extern "C" fn visual_context_tree_test_builder_append_sticky(scrollport_size: constraints.scrollport_size,containing_block_region: constraints.containing_block_region,needs_parent_offset_adjustment: constraints.needs_parent_offset_adjustment,+ non_scrolling_scroll_container: NodeSlotId::INVALID,inset_top: inset(constraints.inset_top),inset_right: inset(constraints.inset_right),inset_bottom: inset(constraints.inset_bottom),diff --git a/Libraries/LibCompositing/Rust/src/visual_context/mod.rs b/Libraries/LibCompositing/Rust/src/visual_context/mod.rsindex e811ae2516a..edb8b8946eb 100644--- a/Libraries/LibCompositing/Rust/src/visual_context/mod.rs+++ b/Libraries/LibCompositing/Rust/src/visual_context/mod.rs@@ -165,6 +165,9 @@ pub struct StickyData {pub scrollport_size: FloatSize,pub containing_block_region: FloatRect,pub needs_parent_offset_adjustment: bool,+ // The nearest scroll container of the box when it has no scrollable overflow, and so no scroll node. Its+ // scrollport never moves, and the geometry above is relative to it rather than to the scroller.+ pub non_scrolling_scroll_container: NodeSlotId,pub inset_top: Option<f32>,pub inset_right: Option<f32>,pub inset_bottom: Option<f32>,@@ -190,6 +193,7 @@ impl StickyData {scrollport_size: FloatSize::default(),containing_block_region: FloatRect::default(),needs_parent_offset_adjustment: false,+ non_scrolling_scroll_container: NodeSlotId::INVALID,inset_top: None,inset_right: None,inset_bottom: None,diff --git a/Libraries/LibCompositing/Rust/src/visual_context/queries.rs b/Libraries/LibCompositing/Rust/src/visual_context/queries.rsindex a41c196f450..baa588d1dc3 100644--- a/Libraries/LibCompositing/Rust/src/visual_context/queries.rs+++ b/Libraries/LibCompositing/Rust/src/visual_context/queries.rs@@ -597,7 +597,8 @@ impl VisualContextTree {continue;};let node_index = SpatialNodeIndex(index);- if sticky.scroller == VISUAL_VIEWPORT_NODE_INDEX {+ let scrollport_is_static = !sticky.non_scrolling_scroll_container.is_invalid();+ if sticky.scroller == VISUAL_VIEWPORT_NODE_INDEX && !scrollport_is_static {record_entry(offsets, node_index, FloatPoint::default());continue;}@@ -605,18 +606,23 @@ impl VisualContextTree {// NB: A fixed-position ancestor can disconnect the sticky box's spatial chain from its// scrollport. Only offsets on the spatial chain move the box, even though the scrollport// is still used for its size and insets.+ // NB: A non-scrolling scroll container moves with the sticky ancestors outside it, so only the+ // sticky ancestors inside it move the box within its scrollport.let mut parent_sticky_offset = FloatPoint::default();let mut ancestor = node.parent;while ancestor != sticky.scroller && ancestor != VISUAL_VIEWPORT_NODE_INDEX {let ancestor_node = &self.spatial_nodes[ancestor.0 as usize];- if matches!(ancestor_node.data, SpatialData::Sticky(_)) {+ if let SpatialData::Sticky(ancestor_sticky) = &ancestor_node.data+ && (!scrollport_is_static+ || ancestor_sticky.non_scrolling_scroll_container == sticky.non_scrolling_scroll_container)+ {let entry = device_offset_for_index(offsets, ancestor);parent_sticky_offset.x += entry.x;parent_sticky_offset.y += entry.y;}ancestor = ancestor_node.parent;}- let scroller_entry = if ancestor == sticky.scroller {+ let scroller_entry = if ancestor == sticky.scroller && !scrollport_is_static {device_offset_for_index(offsets, sticky.scroller)} else {FloatPoint::default()@@ -1428,6 +1434,7 @@ mod tests {},containing_block_region: FloatRect::new(0.0, 0.0, 800.0, 2000.0),needs_parent_offset_adjustment: true,+ non_scrolling_scroll_container: NodeSlotId::INVALID,inset_top: Some(0.0),inset_right: None,inset_bottom: None,@@ -1454,6 +1461,7 @@ mod tests {},containing_block_region: FloatRect::new(0.0, 100.0, 800.0, 50.0),needs_parent_offset_adjustment: true,+ non_scrolling_scroll_container: NodeSlotId::INVALID,inset_top: Some(20.0),inset_right: None,inset_bottom: None,@@ -1503,6 +1511,7 @@ mod tests {},containing_block_region: FloatRect::new(0.0, 0.0, 100.0, 1000.0),needs_parent_offset_adjustment: false,+ non_scrolling_scroll_container: NodeSlotId::INVALID,inset_top: Some(0.0),inset_right: None,inset_bottom: None,@@ -1539,6 +1548,37 @@ mod tests {assert_eq!(tree.resolve_sticky_offsets(&offsets), vec![(node, point(0.0, 0.0))]);}+ #[test]+ fn sticky_offsets_in_a_non_scrolling_scroll_container_ignore_the_scroller_and_outer_sticky_boxes() {+ let mut tree = identity_tree();+ let viewport_scroll = tree.append_spatial(scroll(), VISUAL_VIEWPORT_NODE_INDEX);+ let outer_sticky = tree.append_spatial(sticky(viewport_scroll, None, 0.0), viewport_scroll);+ // A 22px box 21px into a scroll container without overflow, sticking 51px below its scrollport top within a+ // 64px containing block that starts 32px down.+ let mut data = sticky(viewport_scroll, Some(outer_sticky), 21.0);+ if let SpatialData::Sticky(sticky) = &mut data {+ sticky.border_box_size = FloatSize {+ width: 15.0,+ height: 22.0,+ };+ sticky.containing_block_region = FloatRect::new(0.0, 32.0, 404.0, 64.0);+ sticky.inset_top = Some(51.0);+ sticky.non_scrolling_scroll_container = NodeSlotId { index: 1 };+ }+ let inner_sticky = tree.append_spatial(data, outer_sticky);++ for scroll_position in [0.0, 300.0] {+ let offsets = [FloatPoint::default(), point(0.0, -scroll_position)];+ assert_eq!(+ tree.resolve_sticky_offsets(&offsets),+ vec![+ (outer_sticky, point(0.0, scroll_position)),+ (inner_sticky, point(0.0, 30.0))+ ]+ );+ }+ }+#[test]fn sticky_offsets_ignore_scrollers_outside_the_spatial_chain() {let mut tree = identity_tree();diff --git a/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs b/Libraries/LibCompositing/Rust/src/visual_context/serialize.rsindex e5e6523df3c..8e6e3ba393b 100644--- a/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs+++ b/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs@@ -28,7 +28,7 @@ use libgfx_rust::{};const SERIALIZED_TREE_MAGIC: u32 = 0x5443_5641;-const SERIALIZED_TREE_FORMAT: u32 = 8;+const SERIALIZED_TREE_FORMAT: u32 = 9;const SPATIAL_KIND_SCROLL: u8 = 0;const SPATIAL_KIND_STICKY: u8 = 1;@@ -320,6 +320,7 @@ fn write_spatial_data(writer: &mut TreeByteWriter, data: &SpatialData) {writer.size(sticky.scrollport_size);writer.float_rect(sticky.containing_block_region);writer.bool(sticky.needs_parent_offset_adjustment);+ writer.u32(sticky.non_scrolling_scroll_container.index);writer.optional_f32(sticky.inset_top);writer.optional_f32(sticky.inset_right);writer.optional_f32(sticky.inset_bottom);@@ -374,6 +375,7 @@ fn read_spatial_data(reader: &mut TreeByteReader<'_>) -> Option<SpatialData> {scrollport_size: reader.size()?,containing_block_region: reader.float_rect()?,needs_parent_offset_adjustment: reader.bool()?,+ non_scrolling_scroll_container: NodeSlotId { index: reader.u32()? },inset_top: reader.optional_f32()?,inset_right: reader.optional_f32()?,inset_bottom: reader.optional_f32()?,@@ -991,6 +993,7 @@ mod tests {},containing_block_region: FloatRect::new(0.0, 0.0, 800.0, 2000.0),needs_parent_offset_adjustment: true,+ non_scrolling_scroll_container: NodeSlotId::INVALID,inset_top: Some(0.0),inset_right: None,inset_bottom: Some(2.5),@@ -1016,6 +1019,7 @@ mod tests {},containing_block_region: FloatRect::new(0.0, 100.0, 800.0, 50.0),needs_parent_offset_adjustment: false,+ non_scrolling_scroll_container: NodeSlotId { index: 7 },inset_top: None,inset_right: Some(1.0),inset_bottom: None,diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/dump.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/dump.rsindex 9971b0efff4..30bc14e8feb 100644--- a/Libraries/LibWeb/Rust/src/painting/visual_context/dump.rs+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/dump.rs@@ -85,13 +85,17 @@ impl VisualContextTreeDump for VisualContextTree {}let _ = write!(text,- " position_relative_to_scroller={} border_box_size={} scrollport_size={} containing_block_region={} needs_parent_offset_adjustment={} insets=[",+ " position_relative_to_scroller={} border_box_size={} scrollport_size={} containing_block_region={} needs_parent_offset_adjustment={}",format_point(sticky.position_relative_to_scroller),format_size(sticky.border_box_size),format_size(sticky.scrollport_size),format_rect(sticky.containing_block_region),sticky.needs_parent_offset_adjustment);+ if !sticky.non_scrolling_scroll_container.is_invalid() {+ text.push_str(" non_scrolling_scroll_container");+ }+ text.push_str(" insets=[");let mut is_first_inset = true;let mut append_inset = |side: &str, inset: Option<f32>| {let Some(inset) = inset else {@@ -488,6 +492,7 @@ mod node_dump_tests {},containing_block_region: FloatRect::new(0.0, 0.0, 800.0, 16.0),needs_parent_offset_adjustment: true,+ non_scrolling_scroll_container: NodeSlotId::INVALID,inset_top: Some(20.0),inset_right: None,inset_bottom: Some(1.5),diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/refresh.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/refresh.rsindex d5c5945aae3..3bce856998c 100644--- a/Libraries/LibWeb/Rust/src/painting/visual_context/refresh.rs+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/refresh.rs@@ -9,7 +9,6 @@ use super::{SpatialData, StickyData, VisualContextTree};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::chrome_geometry;use crate::painting::host::FfiVisualContextTreeInputs;use crate::painting::paintable_geometry;use crate::painting::paintable_rows::PaintableRowsRead;@@ -24,7 +23,9 @@ pub(crate) struct ResolvedStickyInsets {pub left: Option<CssPixels>,}-fn nearest_wheel_scrollable_ancestor_along_containing_blocks(+// https://drafts.csswg.org/css-position/#stickypos-insets+// The sticky view rectangle comes from the nearest scroll container, even one without scrollable overflow.+fn nearest_scroll_container_along_containing_blocks(layout_arena: &impl PaintableRowsRead,slot: NodeSlotId,) -> Option<NodeSlotId> {@@ -33,8 +34,7 @@ fn nearest_wheel_scrollable_ancestor_along_containing_blocks(if !layout_arena.paintable_row_is_populated(block) {return None;}- let axes = chrome_geometry::wheel_scrollable_axes(layout_arena, block);- if axes.horizontal || axes.vertical {+ if style_queries::is_scroll_container(layout_arena, block) {return Some(block);}if style_queries::is_fixed_position(layout_arena, block) {@@ -46,17 +46,14 @@ fn nearest_wheel_scrollable_ancestor_along_containing_blocks(}// https://drafts.csswg.org/css-position/#insets-pub(crate) fn resolve_sticky_insets_in_css_pixels(+fn resolve_sticky_insets_in_css_pixels(layout_arena: &impl PaintableRowsRead,slot: NodeSlotId,+ scrollport_size: CssPixelSize,) -> ResolvedStickyInsets {let Some(style) = layout_arena.node_style_if_live(slot) else {return ResolvedStickyInsets::default();};- let scrollport_size = nearest_wheel_scrollable_ancestor_along_containing_blocks(layout_arena, slot)- .map_or(CssPixelSize::default(), |scroller| {- paintable_geometry::absolute_rect(layout_arena, scroller).size()- });let resolve_side = |side: &ComputedLengthPercentageOrAuto, scrollport_extent: CssPixels| {(!side.is_auto()).then(|| side.to_px(scrollport_extent))};@@ -69,6 +66,21 @@ pub(crate) fn resolve_sticky_insets_in_css_pixels(}}+fn is_containing_block_descendant_of(+ layout_arena: &impl PaintableRowsRead,+ slot: NodeSlotId,+ ancestor: NodeSlotId,+) -> bool {+ let mut block = layout_arena.paintable_data(slot).containing_block;+ while !block.is_invalid() && layout_arena.paintable_row_is_populated(block) {+ if block == ancestor {+ return true;+ }+ block = layout_arena.paintable_data(block).containing_block;+ }+ false+}+// The geometry stays zero while either row has been replaced by a subtree relayout: the pending// tree rebuild recreates the node before anything reads it.pub(crate) fn compute_sticky_data(@@ -94,17 +106,28 @@ pub(crate) fn compute_sticky_data(if scroller_slot == NO_SCROLL_STATE_SLOT {return data;}- let scroller = scroll_state.state_at_slot(scroller_slot).paintable;+ let mut scroller = scroll_state.state_at_slot(scroller_slot).paintable;if !layout_arena.paintable_row_is_populated(paintable) || !layout_arena.paintable_row_is_populated(scroller) {return data;}+ // NB: Only scroll containers with scrollable overflow get a scroll node. When the nearest scroll container has+ // none, it sits inside the scroller of the scroll node, and its scrollport never moves. The box sticks to+ // that scrollport instead.+ if let Some(scroll_container) = nearest_scroll_container_along_containing_blocks(layout_arena, paintable)+ && scroll_container != scroller+ && is_containing_block_descendant_of(layout_arena, scroll_container, scroller)+ {+ scroller = scroll_container;+ data.non_scrolling_scroll_container = scroll_container;+ }+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)- .expect("scroll ancestor has scrollable overflow")+ .unwrap_or_else(|| paintable_geometry::absolute_padding_box_rect(layout_arena, containing_block)).size();(CssPixelRect::from_location_and_size(CssPixelPoint::default(), size),@@ -126,7 +149,7 @@ pub(crate) fn compute_sticky_data(height: size.height.to_float() * scale,};let device_inset = |inset: Option<CssPixels>| inset.map(|inset| inset.to_float() * scale);- let insets = resolve_sticky_insets_in_css_pixels(layout_arena, paintable);+ let insets = resolve_sticky_insets_in_css_pixels(layout_arena, paintable, scroller_rect.size());let region_location = device_point(containing_block_region.location());let region_size = device_size(containing_block_region.size());diff --git a/Tests/LibWeb/Text/expected/position-sticky-in-scroll-container-without-overflow.txt b/Tests/LibWeb/Text/expected/position-sticky-in-scroll-container-without-overflow.txtnew file mode 100644index 00000000000..d9a68eb9ebc--- /dev/null+++ b/Tests/LibWeb/Text/expected/position-sticky-in-scroll-container-without-overflow.txt@@ -0,0 +1,3 @@+tip in scroller without overflow: -2,75+tip in scroller without overflow inside scrolled scroller: -2,95+header in scrolled viewport: 10diff --git a/Tests/LibWeb/Text/input/position-sticky-in-scroll-container-without-overflow.html b/Tests/LibWeb/Text/input/position-sticky-in-scroll-container-without-overflow.htmlnew file mode 100644index 00000000000..cbc6231bbe4--- /dev/null+++ b/Tests/LibWeb/Text/input/position-sticky-in-scroll-container-without-overflow.html@@ -0,0 +1,42 @@+<!DOCTYPE html>+<style>+html { overflow-x: hidden; }+body { margin: 0; height: 3000px; }+.scroller { overflow: auto; padding: 24px 12px 16px; width: 400px; }+.outer { overflow: auto; height: 200px; width: 400px; }+.grid { display: grid; grid-template-rows: 0 auto; align-items: center; margin-top: 32px; }+.tip { position: sticky; top: 51px; right: calc(100% - 1px); margin-left: -14px; width: 15px; height: 22px; }+.message { height: 64px; }+#header { position: sticky; top: 10px; height: 20px; }+</style>+<div id="header"></div>+<div class="scroller" id="bubble">+ <div class="grid"><div class="tip"></div><div class="message"></div></div>+</div>+<div class="outer" id="outer">+ <div style="height: 100px"></div>+ <div class="scroller">+ <div class="grid"><div class="tip"></div><div class="message"></div></div>+ </div>+ <div style="height: 1000px"></div>+</div>+<script src="include.js"></script>+<script>+test(() => {+ const offset = (element, selector) => {+ const rect = element.querySelector(selector).getBoundingClientRect();+ const container = element.getBoundingClientRect();+ return `${rect.left - container.left},${rect.top - container.top}`;+ };++ const bubble = document.getElementById("bubble");+ println(`tip in scroller without overflow: ${offset(bubble, ".tip")}`);++ const outer = document.getElementById("outer");+ outer.scrollTop = 80;+ println(`tip in scroller without overflow inside scrolled scroller: ${offset(outer, ".tip")}`);++ window.scrollTo(0, 500);+ println(`header in scrolled viewport: ${document.getElementById("header").getBoundingClientRect().top}`);+});+</script>