Skip to content

Truly independent web browser

LibWeb: Map range rects through the context line content paints in

Aliaksandr Kalenik51ea67cabf85
diff --git a/Libraries/LibWeb/Rust/src/layout/script_entries.rs b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
index 0ebd5ef2f49..eaf71aebfaf 100644
--- a/Libraries/LibWeb/Rust/src/layout/script_entries.rs
+++ b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
@@ -171,7 +171,13 @@ pub unsafe extern "C" fn layout_script_text_range_rects(
);
let rect_in_viewport_space = if arena.slot_is_live(block) {
- viewport_mapping.rect_to_viewport(paintable_rows, block, rect, ViewportSpace::LayoutViewport)
+ viewport_mapping.line_content_rect_to_viewport(
+ paintable_rows,
+ block,
+ fragment.layout_node,
+ rect,
+ ViewportSpace::LayoutViewport,
+ )
} else {
rect
};
diff --git a/Libraries/LibWeb/Rust/src/painting/client_rects.rs b/Libraries/LibWeb/Rust/src/painting/client_rects.rs
index faf9817b85b..405ccae4e6e 100644
--- a/Libraries/LibWeb/Rust/src/painting/client_rects.rs
+++ b/Libraries/LibWeb/Rust/src/painting/client_rects.rs
@@ -236,7 +236,13 @@ pub(crate) fn for_each_client_rect(
// line.
if arena.node_kind_if_live(layout_node) == Some(NodeKind::BreakNode) {
if let Some((line_root, rect)) = line_break_rect(arena, layout_node) {
- push_rect(viewport_mapping.rect_to_viewport(arena, line_root, rect, ViewportSpace::LayoutViewport));
+ push_rect(viewport_mapping.line_content_rect_to_viewport(
+ arena,
+ line_root,
+ layout_node,
+ rect,
+ ViewportSpace::LayoutViewport,
+ ));
}
return;
}
diff --git a/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs b/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs
index 3920da244e5..6f5a02ccc88 100644
--- a/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs
+++ b/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs
@@ -88,6 +88,29 @@ impl<'a> ViewportMapping<'a> {
self.rect_in_space_to_viewport(spatial, rect, space)
}
+ /// Maps `rect`, of the line content of `node` that the line root `root` lays out, into the viewport through the
+ /// context that content is painted in: that of the nearest self-painting inline box around `node`, or else the one
+ /// `root` paints its descendants in, which a scroll container moves by its scroll offset.
+ pub(crate) fn line_content_rect_to_viewport(
+ &self,
+ arena: &impl PaintRead,
+ root: NodeSlotId,
+ node: NodeSlotId,
+ rect: CssPixelRect,
+ space: ViewportSpace,
+ ) -> CssPixelRect {
+ if self.transforms.is_none() {
+ return rect;
+ }
+ let painter =
+ crate::painting::fragment_ownership::nearest_self_painting_inline_box(arena, node).unwrap_or(root);
+ if !arena.paintable_row_is_populated(painter) {
+ return CssPixelRect::default();
+ }
+ let context = arena.paintable_data(painter).accumulated_visual_context_for_descendants;
+ self.rect_in_space_to_viewport(context.spatial, rect, space)
+ }
+
/// Maps `rect`, in the local space of `spatial`, into the viewport.
pub(crate) fn rect_in_space_to_viewport(
&self,
diff --git a/Tests/LibWeb/Text/expected/range-client-rects-in-scroll-container.txt b/Tests/LibWeb/Text/expected/range-client-rects-in-scroll-container.txt
new file mode 100644
index 00000000000..929a0efad02
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/range-client-rects-in-scroll-container.txt
@@ -0,0 +1,6 @@
+text: 10,35 40x20
+br1: left 290, middle -5
+br2: left 290, middle 25
+br3: left 250, middle 55
+sticky text: 30,155 40x20
+text after sticky: 10,185 80x20
diff --git a/Tests/LibWeb/Text/input/range-client-rects-in-scroll-container.html b/Tests/LibWeb/Text/input/range-client-rects-in-scroll-container.html
new file mode 100644
index 00000000000..0a22eabda61
--- /dev/null
+++ b/Tests/LibWeb/Text/input/range-client-rects-in-scroll-container.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<style>
+body { margin: 0; }
+.scroller { position: absolute; width: 200px; height: 100px; overflow: scroll; font: 20px/30px Ahem; }
+#text { left: 10px; top: 10px; }
+#breaks { left: 250px; top: 10px; }
+#sticky-container { left: 10px; top: 150px; }
+#sticky { position: sticky; top: 0; }
+</style>
+<div class="scroller" id="text">aaaa<br>bbbb<br>cccc<br>dddd<br>eeee<br>ffff<br>gggg<br>hhhh</div>
+<div class="scroller" id="breaks">xx<br id="br1">yy<br id="br2"><br id="br3">zz<br>z<br>z<br>z<br>z</div>
+<div class="scroller" id="sticky-container">one<br>two<br><span id="sticky">sticky</span><br>four<br>five<br>six<br>seven<br>eight</div>
+<script src="include.js"></script>
+<script>
+ // Text and line breaks directly inside a scroll container move with its scroll offset, and the text of a sticky
+ // positioned inline box with its sticky offset.
+ test(() => {
+ const format = rect => `${Math.round(rect.left)},${Math.round(rect.top)} ${Math.round(rect.width)}x${Math.round(rect.height)}`;
+ const rangeRects = (text, start, end) => {
+ const range = document.createRange();
+ range.setStart(text, start);
+ range.setEnd(text, end);
+ return [...range.getClientRects()].map(format).join(" ");
+ };
+ document.getElementById("text").scrollTop = 40;
+ document.getElementById("breaks").scrollTop = 30;
+ const stickyContainer = document.getElementById("sticky-container");
+ stickyContainer.scrollTop = 60;
+
+ println(`text: ${rangeRects(document.getElementById("text").childNodes[4], 0, 2)}`);
+ for (const id of ["br1", "br2", "br3"]) {
+ const rect = document.getElementById(id).getClientRects()[0];
+ println(`${id}: left ${Math.round(rect.left)}, middle ${Math.round(rect.top + rect.height / 2)}`);
+ }
+ println(`sticky text: ${rangeRects(document.getElementById("sticky").firstChild, 1, 3)}`);
+ println(`text after sticky: ${rangeRects(stickyContainer.childNodes[6], 0, 4)}`);
+ });
+</script>