Skip to content

Truly independent web browser

LibWeb: Reject the lines a point misses as a whole in a hit test

Aliaksandr Kalenikeb4c616970cf
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
index c55dacda0e8..5b30ef42cf4 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
@@ -172,6 +172,19 @@ pub(crate) trait HitTestItemSink {
true
}
+ fn rejects_text_fragments_placed_at_their_line_root_before_building_them(&self) -> bool {
+ false
+ }
+
+ fn misses_line(
+ &mut self,
+ _line_root: NodeSlotId,
+ _line_index: u32,
+ _line_root_absolute_content_origin: CssPixelPoint,
+ ) -> bool {
+ false
+ }
+
fn append(&mut self, item: HitTestItem);
}
@@ -285,8 +298,40 @@ impl<'a, R: PaintRead, S: HitTestItemSink> HitTestItems<'a, R, S> {
.accumulated_visual_context_for_descendants;
// Fragments inside self-painting inline boxes are recorded during that box's paint.
let filter = fragment_ownership::effective_filter(self.rows, paintable);
+ let line_root_positions = self
+ .sink
+ .rejects_text_fragments_placed_at_their_line_root_before_building_them()
+ .then(|| paintable_geometry::LineRootPositions::new(self.rows, paintable));
+ let side = self.rows.committed_side_data(paintable);
+ let mut last_line_missed: Option<(u32, bool)> = None;
filter.for_each_owned_fragment_index(fragment_count, |index| {
- if self.fragment_is_block_level_box(paintable, index) {
+ let fragment = &side.fragments()[index];
+ if fragment.is_atomic_inline {
+ return;
+ }
+ if let Some(positions) = &line_root_positions {
+ let line_root_origin = positions.of_line(side.lines(), fragment.line_index as usize);
+ let line_missed = match last_line_missed {
+ Some((line_index, missed)) if line_index == fragment.line_index => missed,
+ _ => {
+ let missed = self.sink.misses_line(paintable, fragment.line_index, line_root_origin);
+ last_line_missed = Some((fragment.line_index, missed));
+ missed
+ }
+ };
+ if line_missed {
+ return;
+ }
+ let rect_placed_at_its_line_root =
+ text_fragment::rect_in_containing_block(fragment).translated_by(line_root_origin);
+ if !self
+ .sink
+ .wants(HitTestItemKind::TextFragment, rect_placed_at_its_line_root, context)
+ {
+ return;
+ }
+ }
+ if text_fragment::is_block_level_box(self.rows, fragment) {
return;
}
self.append_text_fragment(paintable, index as u32, context);
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs
index fb18d3a91a4..ac5ad6eb48c 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs
@@ -15,7 +15,7 @@ use super::query::{DepthSortingState, item_contains_point};
use super::*;
use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};
use crate::painting::host::FfiHitTestQueryCallbacks;
-use crate::painting::ink_overflow::{ScopeInkBound, scope_ink_bound};
+use crate::painting::ink_overflow::{HitTestBoundOfLine, ScopeInkBound, scope_ink_bound};
use crate::painting::paint_order_plan::{
PaintOrderInputs, PaintOrderItem, PaintProducer, PaintScope, PaintScopeKind, PaintScopePlan, PlanLeavesOut,
};
@@ -109,6 +109,13 @@ impl<R: PaintRead> PlanLeavesOut for WhatThePointMisses<'_, '_, R> {
fn leaves_out_child_scope(&mut self, child: PaintScope, child_inputs: PaintOrderInputs) -> bool {
self.walk.point_misses_child_scope(child, child_inputs, self.owner)
}
+
+ fn leaves_out_line(&mut self, line_root: NodeSlotId, line_index: u32) -> bool {
+ line_root == self.owner.slot
+ && self
+ .walk
+ .point_misses_line(line_root, line_index, self.owner.absolute_content_origin)
+ }
}
#[derive(Clone, Copy)]
@@ -187,6 +194,19 @@ impl<R: PaintRead> HitTestItemSink for PointWalk<'_, R> {
.contains_point(to_css_point(local))
}
+ fn rejects_text_fragments_placed_at_their_line_root_before_building_them(&self) -> bool {
+ self.skips_scopes_the_point_misses
+ }
+
+ fn misses_line(
+ &mut self,
+ line_root: NodeSlotId,
+ line_index: u32,
+ line_root_absolute_content_origin: CssPixelPoint,
+ ) -> bool {
+ self.point_misses_line(line_root, line_index, line_root_absolute_content_origin)
+ }
+
fn append(&mut self, item: HitTestItem) {
let Some(local) = self.local_point_inside_clips(item.context) else {
return;
@@ -258,6 +278,36 @@ impl<R: PaintRead> PointWalk<'_, R> {
paintable_geometry::absolute_rect(self.rows, child).location()
}
+ fn point_misses_line(
+ &mut self,
+ line_root: NodeSlotId,
+ line_index: u32,
+ line_root_absolute_content_origin: CssPixelPoint,
+ ) -> bool {
+ let rows = self.rows;
+ let side = rows.committed_side_data(line_root);
+ let Some(bounds) = side.hit_test_bounds_of_lines.as_deref() else {
+ return false;
+ };
+ if bounds.len() != side.lines().len() {
+ return false;
+ }
+ let Some(HitTestBoundOfLine::RectInContentBoxOfLineRoot(bound)) = bounds.get(line_index as usize) else {
+ return false;
+ };
+ let bound = bound.translated_by(line_root_absolute_content_origin);
+ let context = rows
+ .paintable_data(line_root)
+ .accumulated_visual_context_for_descendants;
+ let Some(local) = self.local_point_ignoring_clips(context) else {
+ return false;
+ };
+ let one_pixel = CssPixels::from_integer(1);
+ !bound
+ .inflated(one_pixel, one_pixel, one_pixel, one_pixel)
+ .contains_point(to_css_point(local))
+ }
+
fn point_misses_child_scope(
&mut self,
child: PaintScope,
diff --git a/Libraries/LibWeb/Rust/src/painting/ink_overflow.rs b/Libraries/LibWeb/Rust/src/painting/ink_overflow.rs
index 913e9d14cd2..b32ca6ddf46 100644
--- a/Libraries/LibWeb/Rust/src/painting/ink_overflow.rs
+++ b/Libraries/LibWeb/Rust/src/painting/ink_overflow.rs
@@ -500,6 +500,58 @@ fn has_resampled_ink(style: crate::layout::ComputedValuesView<'_>) -> bool {
})
}
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub(crate) enum HitTestBoundOfLine {
+ RectInContentBoxOfLineRoot(CssPixelRect),
+ Unbounded,
+}
+
+pub(crate) fn hit_test_bounds_of_lines(
+ source: &impl PaintRead,
+ line_root: NodeSlotId,
+) -> Option<std::sync::Arc<[HitTestBoundOfLine]>> {
+ use crate::painting::paintable_data::PaintableFlag;
+ if !node_painting::has_lines(source, line_root)
+ || source
+ .paintable_data(line_root)
+ .has_flag(PaintableFlag::HasLaterBoxFragments)
+ {
+ return None;
+ }
+ let side = source.committed_side_data(line_root);
+ if side.lines().len() < 2 {
+ return None;
+ }
+ let content_origin = paintable_geometry::absolute_rect(source, line_root).location();
+ let mut bounds = vec![HitTestBoundOfLine::RectInContentBoxOfLineRoot(CssPixelRect::default()); side.lines().len()];
+ for fragment in side.fragments() {
+ let bound = bounds.get_mut(fragment.line_index as usize)?;
+ let HitTestBoundOfLine::RectInContentBoxOfLineRoot(rect) = bound else {
+ continue;
+ };
+ if !fragment.is_atomic_inline {
+ rect.unite(text_fragment::rect_in_containing_block(fragment));
+ continue;
+ }
+ let atomic_inline = fragment.layout_node;
+ if !source.paintable_row_is_populated(atomic_inline) {
+ continue;
+ }
+ let ink = source.stored_ink_overflow(atomic_inline);
+ if ink.unbounded || ink.contribution_unbounded {
+ *bound = HitTestBoundOfLine::Unbounded;
+ continue;
+ }
+ let padding_box_origin = paintable_geometry::first_fragment_padding_box_rect(source, atomic_inline).location();
+ rect.unite(
+ ink.rect
+ .translated_by(padding_box_origin)
+ .translated(-content_origin.x, -content_origin.y),
+ );
+ }
+ Some(bounds.into())
+}
+
/// What bounds everything a paint scope paints.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum ScopeInkBound {
@@ -775,6 +827,11 @@ impl crate::layout::LayoutNodeArena {
source.paintable_rows.ink_overflow_dirty[index] = false;
let ink = recalculated_ink_overflow(&source, state, node);
recalculated += 1;
+ let hit_test_bounds_of_lines = hit_test_bounds_of_lines(&source, node);
+ if hit_test_bounds_of_lines.is_some() || source.committed_side_data(node).hit_test_bounds_of_lines.is_some()
+ {
+ source.committed_side_data_mut(node).hit_test_bounds_of_lines = hit_test_bounds_of_lines;
+ }
let store = &mut source.paintable_rows;
if store.ink_overflow.get(index).is_some_and(|previous| *previous == ink) {
continue;
@@ -822,6 +879,12 @@ impl crate::layout::LayoutNodeArena {
"Stored ink overflow is stale: {:?} {node:?}, stored {stored:?}, recomputed {recomputed:?}",
source.node_kind_if_live(node)
);
+ let stored_hit_test_bounds_of_lines = source.committed_side_data(node).hit_test_bounds_of_lines.clone();
+ let recomputed_hit_test_bounds_of_lines = hit_test_bounds_of_lines(&source, node);
+ assert!(
+ stored_hit_test_bounds_of_lines == recomputed_hit_test_bounds_of_lines,
+ "Stored hit test bounds of the lines of {node:?} are stale: stored {stored_hit_test_bounds_of_lines:?}, recomputed {recomputed_hit_test_bounds_of_lines:?}"
+ );
counts.checked += 1;
}
counts
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs b/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs
index 7d3b571a826..5ee4c4a2ce4 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs
@@ -316,6 +316,7 @@ impl PaintScopePlan {
items: SmallVec::new(),
last_inputs: std::cell::Cell::new(None),
leaves_out,
+ last_line_left_out: None,
};
let establishes_stacking_context =
scope.kind == PaintScopeKind::PaintedAsStackingContext && builder.has_stacking_context(scope.owner);
@@ -335,6 +336,7 @@ impl PaintScopePlan {
pub(crate) trait PlanLeavesOut {
fn leaves_out_child_scope(&mut self, child: PaintScope, child_inputs: PaintOrderInputs) -> bool;
+ fn leaves_out_line(&mut self, line_root: NodeSlotId, line_index: u32) -> bool;
}
struct LeavesOutNothing;
@@ -343,6 +345,10 @@ impl PlanLeavesOut for LeavesOutNothing {
fn leaves_out_child_scope(&mut self, _: PaintScope, _: PaintOrderInputs) -> bool {
false
}
+
+ fn leaves_out_line(&mut self, _: NodeSlotId, _: u32) -> bool {
+ false
+ }
}
struct PaintOrderBuilder<'a, R: PaintRead, LeavesOut: PlanLeavesOut> {
@@ -352,6 +358,7 @@ struct PaintOrderBuilder<'a, R: PaintRead, LeavesOut: PlanLeavesOut> {
// Planning asks several questions about the same row in a row; keep its answers.
last_inputs: std::cell::Cell<Option<(NodeSlotId, PaintOrderInputs)>>,
leaves_out: &'a mut LeavesOut,
+ last_line_left_out: Option<(NodeSlotId, u32, bool)>,
}
impl<R: PaintRead, LeavesOut: PlanLeavesOut> PaintOrderBuilder<'_, R, LeavesOut> {
@@ -411,7 +418,9 @@ impl<R: PaintRead, LeavesOut: PlanLeavesOut> PaintOrderBuilder<'_, R, LeavesOut>
let filter = crate::painting::fragment_ownership::effective_filter(arena, paintable);
if content.items.is_empty() {
for index in 0..content.fragments.len() as u32 {
- if filter.owns_fragment(index) {
+ if filter.owns_fragment(index)
+ && !self.leaves_out_line(root, content.fragments[index as usize].line_index)
+ {
self.items
.push(PaintOrderItem::Producer(PaintProducer::TextFragment(index)));
}
@@ -421,6 +430,15 @@ impl<R: PaintRead, LeavesOut: PlanLeavesOut> PaintOrderBuilder<'_, R, LeavesOut>
// interleaved in each line. Layout retains that order alongside geometry.
for item in &content.items {
use crate::layout::inline_content::InlineItem;
+ let line_index = match *item {
+ InlineItem::BoxPiece(index) => content.inline_box_pieces[index as usize].line_index,
+ InlineItem::TextFragment(index) | InlineItem::AtomicInline(index) => {
+ content.fragments[index as usize].line_index
+ }
+ };
+ if self.leaves_out_line(root, line_index) {
+ continue;
+ }
match *item {
InlineItem::BoxPiece(index) => {
let piece = &content.inline_box_pieces[index as usize];
@@ -460,6 +478,18 @@ impl<R: PaintRead, LeavesOut: PlanLeavesOut> PaintOrderBuilder<'_, R, LeavesOut>
.push(PaintOrderItem::Producer(PaintProducer::ForegroundCommands));
}
+ fn leaves_out_line(&mut self, line_root: NodeSlotId, line_index: u32) -> bool {
+ if let Some((root, index, left_out)) = self.last_line_left_out
+ && root == line_root
+ && index == line_index
+ {
+ return left_out;
+ }
+ let left_out = self.leaves_out.leaves_out_line(line_root, line_index);
+ self.last_line_left_out = Some((line_root, line_index, left_out));
+ left_out
+ }
+
fn is_atomic_inline_content(&self, paintable: NodeSlotId) -> bool {
self.inputs(paintable).is_atomic_inline_content()
}
diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs b/Libraries/LibWeb/Rust/src/painting/paintable_data.rs
index c9af72766bc..e2338f4b4f3 100644
--- a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paintable_data.rs
@@ -223,6 +223,7 @@ pub(crate) struct CommittedSideData {
pub(crate) order_inputs: crate::painting::paint_order_plan::PaintOrderInputs,
pub(crate) containing_block_fragment: u32,
pub(crate) box_fragments: Option<std::sync::Arc<Vec<CommittedBoxFragment>>>,
+ pub(crate) hit_test_bounds_of_lines: Option<std::sync::Arc<[crate::painting::ink_overflow::HitTestBoundOfLine]>>,
}
/// Rows are the same when they hold equal records; a shared record is compared only when the rows
@@ -241,6 +242,11 @@ impl PartialEq for CommittedSideData {
a == b
})
&& self.containing_block_fragment == other.containing_block_fragment
+ && same_payload(
+ self.hit_test_bounds_of_lines.as_ref(),
+ other.hit_test_bounds_of_lines.as_ref(),
+ |a, b| a == b,
+ )
&& same_payload(self.box_fragments.as_ref(), other.box_fragments.as_ref(), |a, b| {
a.len() == b.len()
&& a.iter().zip(b.iter()).all(|(left, right)| {
diff --git a/Tests/LibWeb/Text/expected/hit_testing/lines-of-inline-blocks.txt b/Tests/LibWeb/Text/expected/hit_testing/lines-of-inline-blocks.txt
new file mode 100644
index 00000000000..96b9c9beee2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/hit_testing/lines-of-inline-blocks.txt
@@ -0,0 +1,11 @@
+a cell on the first line: cell-0 [cell-0 lines body html]
+a cell on the last line: cell-11 [cell-11 lines body html]
+past the cells of the last line: lines [lines body html]
+a child of a cell on the first line, under a cell of the second: cell-7, tall is under it
+a child of a cell on the first line, reaching into the last: tall [tall lines body html]
+below the cells: body [body html]
+below the cells, once the child grew: tall [tall body html]
+past the cells of the last line, once the child grew: lines [lines body html]
+text with no line height: zero [zero text body html]
+text on the second line: text [text body html]
+past the text of the last line: text [text body html]
diff --git a/Tests/LibWeb/Text/input/hit_testing/lines-of-inline-blocks.html b/Tests/LibWeb/Text/input/hit_testing/lines-of-inline-blocks.html
new file mode 100644
index 00000000000..24c431b15a0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/hit_testing/lines-of-inline-blocks.html
@@ -0,0 +1,53 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; font: 20px/20px monospace; }
+ #lines { width: 210px; }
+ .cell { display: inline-block; width: 40px; height: 20px; vertical-align: top; }
+ #tall { width: 10px; height: 60px; margin-left: 20px; }
+ #text { margin: 40px 0 0; width: 210px; }
+ #zero { line-height: 0; }
+</style>
+<div id="lines"></div>
+<p id="text">first <span id="zero">zero</span> line<br>second line<br>third line</p>
+<script>
+ function describe(element) {
+ return element ? element.id || element.localName : "null";
+ }
+ function probe(label, x, y) {
+ const all = document.elementsFromPoint(x, y).map(describe).join(" ");
+ println(`${label}: ${describe(document.elementFromPoint(x, y))} [${all}]`);
+ }
+ function probeTopmostAndWhetherUnder(label, x, y, id) {
+ const under = document.elementsFromPoint(x, y).some(element => element.id === id);
+ println(`${label}: ${describe(document.elementFromPoint(x, y))}, ${id} ${under ? "is" : "is not"} under it`);
+ }
+ test(() => {
+ const lines = document.getElementById("lines");
+ for (let index = 0; index < 12; ++index) {
+ const cell = document.createElement("span");
+ cell.className = "cell";
+ cell.id = `cell-${index}`;
+ lines.appendChild(cell);
+ }
+ const tall = document.createElement("div");
+ tall.id = "tall";
+ document.getElementById("cell-2").appendChild(tall);
+
+ probe("a cell on the first line", 10, 10);
+ probe("a cell on the last line", 50, 50);
+ probe("past the cells of the last line", 150, 50);
+ probeTopmostAndWhetherUnder("a child of a cell on the first line, under a cell of the second", 105, 30, "tall");
+ probe("a child of a cell on the first line, reaching into the last", 105, 50);
+ probe("below the cells", 105, 70);
+ tall.style.height = "80px";
+ probe("below the cells, once the child grew", 105, 70);
+ probe("past the cells of the last line, once the child grew", 150, 50);
+
+ const zero = document.getElementById("zero").getBoundingClientRect();
+ probe("text with no line height", zero.left + zero.width / 2, zero.top + zero.height / 2);
+ const text = document.getElementById("text").getBoundingClientRect();
+ probe("text on the second line", text.left + 5, text.top + 30);
+ probe("past the text of the last line", text.left + 200, text.top + 50);
+ });
+</script>