Skip to content

Truly independent web browser

LibWeb: Clip IntersectionObserver with the shared overflow clip

Aliaksandr Kalenikbf857492e117
diff --git a/Libraries/LibWeb/HTML/NavigableContainer.cpp b/Libraries/LibWeb/HTML/NavigableContainer.cpp
index 8be3bcb4a7f..81a99f4f06d 100644
--- a/Libraries/LibWeb/HTML/NavigableContainer.cpp
+++ b/Libraries/LibWeb/HTML/NavigableContainer.cpp
@@ -504,16 +504,11 @@ bool NavigableContainer::currently_delays_the_load_event() const
return m_content_navigable->delays_the_load_event_of_its_container();
}
-// Clips rect to the document's viewport and to every overflow-clipping containing block above layout_node.
+// Clips rect to the document's viewport and to every clipping containing block above layout_node.
static CSSPixelRect visible_part_in_document_viewport(Layout::Node const& layout_node, CSSPixelRect rect)
{
auto visible = rect.intersected(CSSPixelRect { {}, layout_node.document().viewport_rect().size() });
- for (auto const* ancestor = layout_node.containing_block(); ancestor && Painting::has_committed_box(*ancestor); ancestor = ancestor->containing_block()) {
- if (ancestor->overflow_x() == CSS::Overflow::Visible && ancestor->overflow_y() == CSS::Overflow::Visible)
- continue;
- visible.intersect(Painting::rect_to_viewport(*ancestor, Painting::absolute_padding_box_rect(*ancestor), Layout::RustFFI::ViewportSpace::VisualViewport));
- }
- return visible;
+ return Painting::visible_rect_in_containing_blocks(layout_node, visible, Layout::RustFFI::ViewportSpace::VisualViewport);
}
// The bounding box, in the coordinates of the content box's document, of a rect in that document's viewport.
diff --git a/Libraries/LibWeb/Painting/BoxViews.cpp b/Libraries/LibWeb/Painting/BoxViews.cpp
index 32bf71f1c1b..88a11b49904 100644
--- a/Libraries/LibWeb/Painting/BoxViews.cpp
+++ b/Libraries/LibWeb/Painting/BoxViews.cpp
@@ -690,6 +690,14 @@ CSSPixelRect rect_to_viewport(Layout::Node const& node, CSSPixelRect const& rect
return result;
}
+CSSPixelRect visible_rect_in_containing_blocks(Layout::Node const& node, CSSPixelRect const& rect, Layout::RustFFI::ViewportSpace space)
+{
+ auto mapping = viewport_mapping(node.document());
+ CSSPixelRect visible;
+ Layout::RustFFI::render_state_visible_rect_in_containing_blocks(node.document_host(), committed_row_slot(node), mapping, rect, space, &visible);
+ return visible;
+}
+
CSSPixelPoint point_to_local(Layout::Node const& node, CSSPixelPoint point, Layout::RustFFI::ViewportSpace space)
{
if (!has_committed_box(node))
diff --git a/Libraries/LibWeb/Painting/BoxViews.h b/Libraries/LibWeb/Painting/BoxViews.h
index 8434b48c615..4a2df2f84bc 100644
--- a/Libraries/LibWeb/Painting/BoxViews.h
+++ b/Libraries/LibWeb/Painting/BoxViews.h
@@ -65,6 +65,8 @@ WEB_API bool is_paintable_with_lines(Layout::Node const&);
WEB_API Layout::RustFFI::FfiViewportMapping viewport_mapping(DOM::Document const&);
WEB_API Layout::RustFFI::FfiViewportMapping viewport_mapping(DOM::Document const&, Compositing::AccumulatedVisualContextTree const&);
WEB_API CSSPixelRect rect_to_viewport(Layout::Node const&, CSSPixelRect const&, Layout::RustFFI::ViewportSpace);
+// The part of a rect in the viewport that the content clips of the box's containing blocks leave.
+WEB_API CSSPixelRect visible_rect_in_containing_blocks(Layout::Node const&, CSSPixelRect const&, Layout::RustFFI::ViewportSpace);
// The point mapped into the space the box is painted in, past any clips, or the point itself where the box's transforms
// hide it.
WEB_API CSSPixelPoint point_to_local(Layout::Node const&, CSSPixelPoint, Layout::RustFFI::ViewportSpace);
diff --git a/Libraries/LibWeb/Rust/src/painting/ffi.rs b/Libraries/LibWeb/Rust/src/painting/ffi.rs
index 30f3b9da1cb..96f9c4a84f6 100644
--- a/Libraries/LibWeb/Rust/src/painting/ffi.rs
+++ b/Libraries/LibWeb/Rust/src/painting/ffi.rs
@@ -733,6 +733,24 @@ pub struct FfiEmptyLineCaretRect {
pub style_source: NodeSlotId,
}
+/// The part of `rect`, in the viewport, that the content clips of the containing blocks of the box in `slot` leave.
+#[unsafe(no_mangle)]
+pub extern "C" fn render_state_visible_rect_in_containing_blocks(
+ host: &DocumentHost,
+ slot: NodeSlotId,
+ viewport_mapping: crate::painting::viewport_mapping::FfiViewportMapping<'_>,
+ rect: FfiCssPixelRect,
+ space: crate::painting::viewport_mapping::ViewportSpace,
+ out: &mut FfiCssPixelRect,
+) {
+ let viewport_mapping = viewport_mapping.mapping();
+ *out = host.read_rows(node_read(), false, |rows| {
+ let visible =
+ viewport_mapping.visible_rect_in_ancestor(rows, slot, rect.into(), NodeSlotId::INVALID, space, |clip| clip);
+ visible.into()
+ });
+}
+
/// Whether a row has ever been given a style with `content-visibility: auto`, which is when a
/// layout commit collects the boxes with it. The host knows without asking right after the round that committed it.
#[unsafe(no_mangle)]
diff --git a/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs b/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs
index cd13b9c85b6..083dd31559b 100644
--- a/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs
+++ b/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs
@@ -4,34 +4,14 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-use crate::css::computed_value_views::ComputedValuesView;
-use crate::css::css_enums::{overflow, positioning};
+use crate::css::css_enums::positioning;
use crate::css::css_pixels::CssPixelRect;
use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};
use crate::layout::node_facts::{self, NodeShape};
use crate::painting::paint_read::{PaintRead, PaintRow};
-use crate::painting::paintable_geometry;
use crate::painting::style_queries;
use crate::painting::viewport_mapping::{ViewportMapping, ViewportSpace};
-fn content_clip_rect_in_viewport_space(
- arena: &impl PaintRead,
- node: NodeSlotId,
- style: ComputedValuesView<'_>,
- viewport_mapping: &ViewportMapping<'_>,
-) -> Option<CssPixelRect> {
- let has_content_clip = style.overflow_x() != overflow::VISIBLE || style.overflow_y() != overflow::VISIBLE;
- if !has_content_clip {
- return None;
- }
- Some(viewport_mapping.rect_to_viewport(
- arena,
- node,
- paintable_geometry::absolute_padding_box_rect(arena, node),
- ViewportSpace::LayoutViewport,
- ))
-}
-
// https://www.w3.org/TR/intersection-observer/#compute-the-intersection
pub(crate) fn intersection_rect(
arena: &impl PaintRead,
@@ -40,62 +20,34 @@ pub(crate) fn intersection_rect(
intersection_root: NodeSlotId,
root_bounds: CssPixelRect,
viewport_mapping: &ViewportMapping<'_>,
- mut inflate_scroll_container_clip_rect_by_scroll_margin: impl FnMut(CssPixelRect) -> CssPixelRect,
+ inflate_scroll_container_clip_rect_by_scroll_margin: impl FnMut(CssPixelRect) -> CssPixelRect,
) -> CssPixelRect {
// 1. Let intersectionRect be the result of getting the bounding box for target.
- let mut intersection_rect = target_rect;
-
// 2. Let container be the containing block of target.
// 3. While container is not root:
- if arena.paintable_row_is_populated(target) {
- let mut container = arena.node_containing_block_if_live(target);
- while let Some(container_slot) = container {
- // Stop when we reach the intersection root.
- if container_slot == intersection_root {
- break;
- }
- if !arena.paintable_row_is_populated(container_slot) {
- break;
- }
-
- // FIXME: 3.1. If container is the document of a nested browsing context, update
- // intersectionRect by clipping to the viewport of the document, and update
- // container to be the browsing context container of container.
-
- // NOTE: Steps 3.2 (map to container coordinate space) and 3.5 (update container) are
- // unnecessary here because get_bounding_client_rect() and transform_rect_to_viewport()
- // already produce viewport-relative coordinates.
-
- // 3.3. If container is a scroll container, apply the observer’s [[scrollMargin]]
- // to the container’s clip rect.
- // 3.4. If container has a content clip or a css clip-path property, update intersectionRect
- // by applying container’s clip.
- // FIXME: Handle clip-path.
- if let Some(style) = arena.node_style_if_live(container_slot)
- && let Some(mut clip_rect) =
- content_clip_rect_in_viewport_space(arena, container_slot, style, viewport_mapping)
- {
- // Apply scroll margin to expand the scrollport for scroll containers.
- let container_kind = arena.node_kind_if_live(container_slot).unwrap_or(NodeKind::Unset);
- if node_facts::kind_and_style_make_scroll_container(container_kind, Some(style)) {
- clip_rect = inflate_scroll_container_clip_rect_by_scroll_margin(clip_rect);
- }
-
- intersection_rect = intersection_rect.intersected(clip_rect);
- }
- container = arena.node_containing_block_if_live(container_slot);
- }
- }
+ // FIXME: 3.1. If container is the document of a nested browsing context, update intersectionRect by clipping to
+ // the viewport of the document, and update container to be the browsing context container of
+ // container.
+ // NOTE: Steps 3.2 (map to container coordinate space) and 3.5 (update container) are unnecessary here because the
+ // bounding box and every clip are mapped into the viewport.
+ // 3.3. If container is a scroll container, apply the observer’s [[scrollMargin]] to the container’s clip rect.
+ // 3.4. If container has a content clip or a css clip-path property, update intersectionRect by applying
+ // container’s clip.
+ let intersection_rect = viewport_mapping.visible_rect_in_ancestor(
+ arena,
+ target,
+ target_rect,
+ intersection_root,
+ ViewportSpace::LayoutViewport,
+ inflate_scroll_container_clip_rect_by_scroll_margin,
+ );
// FIXME: 4. Map intersectionRect to the coordinate space of root.
// 5. Update intersectionRect by intersecting it with the root intersection rectangle.
- intersection_rect = intersection_rect.intersected(root_bounds);
-
// FIXME: 6. Map intersectionRect to the coordinate space of the viewport of the document containing target.
-
// 7. Return intersectionRect.
- intersection_rect
+ intersection_rect.intersected(root_bounds)
}
pub(crate) fn transform_subtree_is_clipped_outside(
@@ -131,7 +83,7 @@ pub(crate) fn transform_subtree_is_clipped_outside(
return false;
}
- if let Some(clip_rect) = content_clip_rect_in_viewport_space(arena, ancestor, style, viewport_mapping)
+ if let Some(clip_rect) = viewport_mapping.content_clip_rect(arena, ancestor, ViewportSpace::LayoutViewport)
&& !clip_rect.edge_adjacent_intersects(root_bounds)
{
has_disjoint_clip = true;
diff --git a/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs b/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs
index bf0fa614678..3920da244e5 100644
--- a/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs
+++ b/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs
@@ -11,10 +11,12 @@ use crate::css::css_pixels::{CssPixelPoint, CssPixelRect, CssPixels, FfiCssPixel
use crate::layout::node_data::NodeSlotId;
use crate::painting::hit_test::to_css_point;
use crate::painting::paint_read::PaintRead;
+use crate::painting::style_queries;
use crate::painting::visual_context::{
ClipBehavior, ContextRef, IncludeVisualViewportTransform, SpatialNodeIndex, VISUAL_VIEWPORT_NODE_INDEX,
VisualContextTree, WorldTransforms,
};
+use crate::painting::visual_context::{basic_shapes, node_values};
use libcompositing_rust::ffi::{tree_from_handle, world_transforms_from_handle};
use libgfx_rust::{FloatPoint, FloatRect};
use std::ffi::c_void;
@@ -115,6 +117,60 @@ impl<'a> ViewportMapping<'a> {
}
}
+ /// The rect `node` clips its content to, in the viewport: its overflow clip and the bounding box of its clip-path.
+ pub(crate) fn content_clip_rect(
+ &self,
+ arena: &impl PaintRead,
+ node: NodeSlotId,
+ space: ViewportSpace,
+ ) -> Option<CssPixelRect> {
+ let overflow_clip = node_values::overflow_clip_rect(arena, node);
+ let clip_path_bounds =
+ basic_shapes::compute_basic_shape_clip_path_data(arena, node, 1.0).map(|(_, bounds, _)| {
+ CssPixelRect::new(
+ CssPixels::from_integer(bounds.x.into()),
+ CssPixels::from_integer(bounds.y.into()),
+ CssPixels::from_integer(bounds.width.into()),
+ CssPixels::from_integer(bounds.height.into()),
+ )
+ });
+ let clip = match (overflow_clip, clip_path_bounds) {
+ (Some(overflow_clip), Some(clip_path_bounds)) => overflow_clip.intersected(clip_path_bounds),
+ (overflow_clip, clip_path_bounds) => overflow_clip.or(clip_path_bounds)?,
+ };
+ Some(self.rect_to_viewport(arena, node, clip, space))
+ }
+
+ /// `rect`, in the viewport, clipped by the content clip of every box on the containing block chain of `node` below
+ /// `ancestor`, the clip of a scroll container passed through `inflate_scroll_container_clip` first.
+ pub(crate) fn visible_rect_in_ancestor(
+ &self,
+ arena: &impl PaintRead,
+ node: NodeSlotId,
+ rect: CssPixelRect,
+ ancestor: NodeSlotId,
+ space: ViewportSpace,
+ mut inflate_scroll_container_clip: impl FnMut(CssPixelRect) -> CssPixelRect,
+ ) -> CssPixelRect {
+ let mut visible = rect;
+ if !arena.paintable_row_is_populated(node) {
+ return visible;
+ }
+ let mut container = arena.node_containing_block_if_live(node);
+ while let Some(container_slot) =
+ container.filter(|slot| *slot != ancestor && arena.paintable_row_is_populated(*slot))
+ {
+ if let Some(mut clip) = self.content_clip_rect(arena, container_slot, space) {
+ if style_queries::is_scroll_container(arena, container_slot) {
+ clip = inflate_scroll_container_clip(clip);
+ }
+ visible = visible.intersected(clip);
+ }
+ container = arena.node_containing_block_if_live(container_slot);
+ }
+ visible
+ }
+
/// Maps `point`, in the viewport, into the local space of `context`, or gives none where a clip that
/// `clip_behavior` respects leaves it out or the context's transforms hide it. The point stays unrounded, so that a
/// space scaled down far, such as SVG user units under a small viewBox, keeps its precision.
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs
index 27bb1e3801a..5522d4adb7e 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs
@@ -875,11 +875,11 @@ fn used_overflow(
(overflow_x, overflow_y)
}
-pub(crate) fn compute_clip_data(layout_arena: &impl PaintRead, slot: NodeSlotId, pixel_ratio: f64) -> Option<ClipData> {
+/// The rect a box's overflow clips its content to, unbounded along an axis it does not clip.
+pub(crate) fn overflow_clip_rect(layout_arena: &impl PaintRead, slot: NodeSlotId) -> Option<CssPixelRect> {
use crate::css::css_enums::overflow;
- let node = slot;
- let style = layout_arena.node_style_if_live(node)?;
- let (overflow_x, overflow_y) = used_overflow(layout_arena, node, style);
+ let style = layout_arena.node_style_if_live(slot)?;
+ let (overflow_x, overflow_y) = used_overflow(layout_arena, slot, style);
// https://drafts.csswg.org/css-overflow-3/#propdef-overflow
// 'clip'
@@ -905,7 +905,12 @@ pub(crate) fn compute_clip_data(layout_arena: &impl PaintRead, slot: NodeSlotId,
overflow::CLIP => (overflow_clip_edge.y, overflow_clip_edge.y + overflow_clip_edge.height),
_ => (padding_box.y, padding_box.y + padding_box.height),
};
- let clip_rect = CssPixelRect::new(left, top, right - left, bottom - top);
+ Some(CssPixelRect::new(left, top, right - left, bottom - top))
+}
+
+pub(crate) fn compute_clip_data(layout_arena: &impl PaintRead, slot: NodeSlotId, pixel_ratio: f64) -> Option<ClipData> {
+ let clip_rect = overflow_clip_rect(layout_arena, slot)?;
+ let style = layout_arena.node_style_if_live(slot)?;
// https://drafts.csswg.org/css-overflow-3/#corner-clipping
// As mentioned in CSS Backgrounds 3 § 4.3 Corner Clipping, the clipping region established by 'overflow' can be
@@ -922,7 +927,7 @@ pub(crate) fn compute_clip_data(layout_arena: &impl PaintRead, slot: NodeSlotId,
// INTEROP: A single-line text field clips its contents to its padding box without rounding the corners, so that
// text selected at the edge of a short field with rounded corners keeps its square highlight.
let is_text_input = layout_arena.node_kind_if_live(slot) == Some(crate::layout::node_data::NodeKind::TextInputBox);
- let radii = if overflow_x != overflow::VISIBLE && overflow_y != overflow::VISIBLE && !is_text_input {
+ let radii = if overflow_clips_both_axes(layout_arena, slot) && !is_text_input {
padding_edge_border_radii(style, layout_arena, slot)
} else {
BorderRadii::default()
diff --git a/Tests/LibWeb/Text/expected/IntersectionObserver/per-axis-clip-and-clip-margin.txt b/Tests/LibWeb/Text/expected/IntersectionObserver/per-axis-clip-and-clip-margin.txt
new file mode 100644
index 00000000000..334e18da77d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/IntersectionObserver/per-axis-clip-and-clip-margin.txt
@@ -0,0 +1,4 @@
+per_axis: ratio=1 intersectionRect=(10,120,20,20)
+clip_margin: ratio=1 intersectionRect=(160,120,20,20)
+control_clipped: ratio=0 intersectionRect=(0,0,0,0)
+control_visible: ratio=1 intersectionRect=(460,120,20,20)
diff --git a/Tests/LibWeb/Text/input/IntersectionObserver/per-axis-clip-and-clip-margin.html b/Tests/LibWeb/Text/input/IntersectionObserver/per-axis-clip-and-clip-margin.html
new file mode 100644
index 00000000000..1365b2880e7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/IntersectionObserver/per-axis-clip-and-clip-margin.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<style>
+body { margin: 0 }
+.c { position: absolute; top: 0; width: 100px; height: 100px; background: lightgray; }
+.t { position: absolute; left: 10px; top: 120px; width: 20px; height: 20px; background: green; }
+</style>
+<script src="../include.js"></script>
+<div class="c" style="left: 0; overflow-x: clip; overflow-y: visible"><div class="t" id="per_axis"></div></div>
+<div class="c" style="left: 150px; overflow: clip; overflow-clip-margin: 50px"><div class="t" id="clip_margin"></div></div>
+<div class="c" style="left: 300px; overflow: clip"><div class="t" id="control_clipped"></div></div>
+<div class="c" style="left: 450px"><div class="t" id="control_visible"></div></div>
+<script>
+ asyncTest(done => {
+ // Each target sits 20px below its 100px tall container. Only the third container clips it away: the first
+ // clips the horizontal axis only, and the second's clip edge extends 50px past its padding box.
+ const ids = ["per_axis", "clip_margin", "control_clipped", "control_visible"];
+ const lines = new Map();
+ const observer = new IntersectionObserver(entries => {
+ for (const entry of entries) {
+ const r = entry.intersectionRect;
+ lines.set(entry.target.id, `${entry.target.id}: ratio=${entry.intersectionRatio} intersectionRect=(${r.x},${r.y},${r.width},${r.height})`);
+ }
+ if (lines.size < ids.length)
+ return;
+ for (const id of ids)
+ println(lines.get(id));
+ observer.disconnect();
+ done();
+ });
+ for (const id of ids)
+ observer.observe(document.getElementById(id));
+ });
+</script>