Skip to content

Truly independent web browser

LibWeb: Have caret queries ask a NodeScopes about the DOM

Andreas Kling168aadd315a3
diff --git a/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs b/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs
index e4824e9e36c..9ca9afa9b96 100644
--- a/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs
+++ b/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs
@@ -9,6 +9,7 @@
//! a token nor call an entry that does.
use super::*;
+use crate::painting::hit_test::caret::HostNodeScopes;
use crate::painting::host::FfiCaretAt;
use crate::painting::host::FfiVisualContextTreeInputs;
use crate::painting::paint_passes::{PassEffect, pending_preparation, run as run_paint_pass};
@@ -1079,7 +1080,10 @@ pub unsafe extern "C" fn layout_hit_test_caret_position_from_point(
let mode = crate::painting::hit_test::caret::CaretPositionMode::from_u8(mode);
*out = with_hit_test_list_spatial_indexes_and_visual_context_tree(host, read, true, None, |list, tree, arena| {
list.caret_position_from_point(
- &main_thread,
+ &HostNodeScopes {
+ main_thread: &main_thread,
+ callbacks: &callbacks,
+ },
arena,
tree,
&callbacks,
@@ -1115,9 +1119,11 @@ pub unsafe extern "C" fn layout_hit_test_caret_at_line_edge(
let main_thread = unsafe { main_thread(host) };
*out = with_hit_test_list_and_caret_lines(host, read, None, |list, arena| {
list.caret_at_line_edge(
- &main_thread,
+ &HostNodeScopes {
+ main_thread: &main_thread,
+ callbacks: &callbacks,
+ },
arena,
- &callbacks,
&query,
offset,
affinity_is_downstream,
@@ -1158,9 +1164,11 @@ pub unsafe extern "C" fn layout_hit_test_caret_on_adjacent_line(
let main_thread = unsafe { main_thread(host) };
*out = with_hit_test_list_and_caret_lines(host, read, None, |list, arena| {
list.caret_on_adjacent_line(
- &main_thread,
+ &HostNodeScopes {
+ main_thread: &main_thread,
+ callbacks: &callbacks,
+ },
arena,
- &callbacks,
&query,
offset,
affinity_is_downstream,
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
index 5351e7dfbd0..acf65520662 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
@@ -15,6 +15,24 @@ use crate::painting::rect_to_viewport_transform::RectToViewportTransform;
use crate::painting::text_fragment::CaretMatch;
use crate::painting::visual_context::{IncludeVisualViewportTransform, SpatialNodeIndex};
+/// What a caret query reads of the DOM: which nodes a scope holds.
+pub(crate) trait NodeScopes {
+ /// Whether `node` is `ancestor` or one of its descendants in the DOM.
+ fn contains(&self, ancestor: FfiNodeIdentity, node: FfiNodeIdentity) -> bool;
+}
+
+/// The host's answers, which only its thread reaches.
+pub(crate) struct HostNodeScopes<'a> {
+ pub(crate) main_thread: &'a crate::stage::MainThread<'a>,
+ pub(crate) callbacks: &'a FfiHitTestQueryCallbacks,
+}
+
+impl NodeScopes for HostNodeScopes<'_> {
+ fn contains(&self, ancestor: FfiNodeIdentity, node: FfiNodeIdentity) -> bool {
+ self.callbacks.contains(self.main_thread, ancestor, node)
+ }
+}
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum CaretPositionType {
@@ -450,16 +468,15 @@ impl HitTestList {
fn line_in_scope(
&self,
- main_thread: &crate::stage::MainThread,
+ scopes: &impl NodeScopes,
arena: &impl PaintRead,
- callbacks: &FfiHitTestQueryCallbacks,
line_index: usize,
scope: FfiNodeIdentity,
) -> bool {
let line = &self.caret_lines[line_index];
for caret_item_index in line.first_caret_item_index..=line.last_caret_item_index {
let node = self.item_target(arena, self.caret_item_indices[caret_item_index]);
- if !node.is_none() && callbacks.contains(main_thread, scope, node) {
+ if !node.is_none() && scopes.contains(scope, node) {
return true;
}
}
@@ -469,7 +486,7 @@ impl HitTestList {
#[allow(clippy::too_many_arguments)]
pub(crate) fn find_closest_line(
&self,
- main_thread: &crate::stage::MainThread,
+ scopes: &impl NodeScopes,
arena: &impl PaintRead,
visual_context_tree: &VisualContextTree,
callbacks: &FfiHitTestQueryCallbacks,
@@ -501,7 +518,7 @@ impl HitTestList {
};
for line_index in 0..self.caret_lines.len() {
- if !scope.is_none() && !self.line_in_scope(main_thread, arena, callbacks, line_index, scope) {
+ if !scope.is_none() && !self.line_in_scope(scopes, arena, line_index, scope) {
continue;
}
let line = self.caret_lines[line_index].clone();
@@ -660,9 +677,8 @@ impl HitTestList {
#[allow(clippy::too_many_arguments)]
pub(crate) fn adjacent_line(
&self,
- main_thread: &crate::stage::MainThread,
+ scopes: &impl NodeScopes,
arena: &impl PaintRead,
- callbacks: &FfiHitTestQueryCallbacks,
current_line_index: usize,
direction: CaretLineDirection,
inline_coordinate: CssPixels,
@@ -731,7 +747,7 @@ impl HitTestList {
// Separate paint contexts inside one editing host must not isolate its editable lines.
if line_index == current_line_index
|| is_in_other_fragment_of_current_block_container(line)
- || !self.line_in_scope(main_thread, arena, callbacks, line_index, scope)
+ || !self.line_in_scope(scopes, arena, line_index, scope)
{
continue;
}
@@ -777,7 +793,7 @@ impl HitTestList {
let line = &self.caret_lines[line_index];
is_in_other_fragment_of_current_block_container(line)
&& line.box_fragment == adjacent_fragment
- && self.line_in_scope(main_thread, arena, callbacks, line_index, scope)
+ && self.line_in_scope(scopes, arena, line_index, scope)
})
.min_by_key(|&line_index| {
let block_coordinate = line_block_middle(self.caret_lines[line_index].rect, writing_mode);
@@ -886,14 +902,9 @@ impl HitTestList {
// Whether the node of the caret's boundary is `ancestor` or one of its descendants. A boundary beside a node is
// inside that node's parent.
- fn caret_boundary_is_inside(
- main_thread: &crate::stage::MainThread,
- callbacks: &FfiHitTestQueryCallbacks,
- ancestor: FfiNodeIdentity,
- caret: &FfiResolvedCaret,
- ) -> bool {
+ fn caret_boundary_is_inside(scopes: &impl NodeScopes, ancestor: FfiNodeIdentity, caret: &FfiResolvedCaret) -> bool {
let boundary_is_beside_node = caret.boundary != FfiCaretBoundaryKind::Offset;
- (!boundary_is_beside_node || ancestor != caret.node) && callbacks.contains(main_thread, ancestor, caret.node)
+ (!boundary_is_beside_node || ancestor != caret.node) && scopes.contains(ancestor, caret.node)
}
/// The caret position at `point`. When `constraint_scope` names a node, the position is constrained to lines inside
@@ -901,7 +912,7 @@ impl HitTestList {
#[allow(clippy::too_many_arguments)]
pub(crate) fn caret_position_from_point(
&self,
- main_thread: &crate::stage::MainThread,
+ scopes: &impl NodeScopes,
arena: &impl PaintRead,
tree: &VisualContextTree,
callbacks: &FfiHitTestQueryCallbacks,
@@ -919,7 +930,7 @@ impl HitTestList {
// A constrained search only accepts direct hits inside the constraint scope.
if constrained && let Some(item) = topmost_item {
let node = self.item_target(arena, item.index);
- if node.is_none() || !callbacks.contains(main_thread, constraint_scope, node) {
+ if node.is_none() || !scopes.contains(constraint_scope, node) {
topmost_item = None;
}
}
@@ -956,7 +967,7 @@ impl HitTestList {
// a selection outside a textarea), so it transforms points without rejecting them against clips.
let respect_clip = !constrained;
let find_closest_line =
- |scope| self.find_closest_line(main_thread, arena, tree, callbacks, point, mode, scope, respect_clip);
+ |scope| self.find_closest_line(scopes, arena, tree, callbacks, point, mode, scope, respect_clip);
let mut closest_line = find_closest_line(line_scope);
let point_is_in_column_of_closest_line = closest_line.index.is_some()
&& closest_line.fragment_inline_distance == CssPixels::default()
@@ -988,7 +999,7 @@ impl HitTestList {
let hit_target = self.item_dispatch_target(arena, hit.index);
if !hit_target.is_none()
&& !caret.caret.node.is_none()
- && !Self::caret_boundary_is_inside(main_thread, callbacks, hit_target, &caret.caret)
+ && !Self::caret_boundary_is_inside(scopes, hit_target, &caret.caret)
{
if self.items[hit.index].can_produce_caret_position
&& self.item_is_direct_caret_target(arena, hit.index)
@@ -1021,9 +1032,8 @@ impl HitTestList {
#[allow(clippy::too_many_arguments)]
pub(crate) fn caret_at_line_edge(
&self,
- main_thread: &crate::stage::MainThread,
+ scopes: &impl NodeScopes,
arena: &impl PaintRead,
- callbacks: &FfiHitTestQueryCallbacks,
query: &FfiCaretPositionQuery,
offset: usize,
affinity_is_downstream: bool,
@@ -1045,7 +1055,7 @@ impl HitTestList {
};
}
let item_index = self.item_at_line_edge_matching(line_index, logical_edge, |index| {
- scope.is_none() || callbacks.contains(main_thread, scope, self.item_target(arena, index))
+ scope.is_none() || scopes.contains(scope, self.item_target(arena, index))
})?;
let mut caret = self.caret_at_item(arena, item_index, CssPixelPoint::default(), logical_edge)?;
if physical {
@@ -1072,9 +1082,8 @@ impl HitTestList {
#[allow(clippy::too_many_arguments)]
pub(crate) fn caret_on_adjacent_line(
&self,
- main_thread: &crate::stage::MainThread,
+ scopes: &impl NodeScopes,
arena: &impl PaintRead,
- callbacks: &FfiHitTestQueryCallbacks,
query: &FfiCaretPositionQuery,
offset: usize,
affinity_is_downstream: bool,
@@ -1083,15 +1092,8 @@ impl HitTestList {
scope: FfiNodeIdentity,
) -> Option<FfiCaretAt> {
let line_index = self.caret_line_for_position(arena, query, offset, affinity_is_downstream)?;
- let (adjacent_line_index, point) = self.adjacent_line(
- main_thread,
- arena,
- callbacks,
- line_index,
- direction,
- inline_coordinate,
- scope,
- )?;
+ let (adjacent_line_index, point) =
+ self.adjacent_line(scopes, arena, line_index, direction, inline_coordinate, scope)?;
// Reuse point-to-caret resolution after choosing the line so text, atomic boxes, and empty lines share one rule
// for selecting the position closest to the preferred inline coordinate.
self.caret_at_line(arena, adjacent_line_index, point, CaretPositionMode::Normal)