Skip to content

Truly independent web browser

LibWeb: Place the caret inside an inline-block for a point in it

sideshowbarker04369cdad5e8
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
index ce8e82a68ac..215a28fbe43 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
@@ -494,6 +494,7 @@ impl HitTestList {
mode: CaretPositionMode,
scope: FfiNodeIdentity,
respect_clip: bool,
+ block_container: NodeSlotId,
) -> ClosestLine {
debug_assert!(self.caret_lines_built);
let rows = arena;
@@ -521,6 +522,9 @@ impl HitTestList {
if !scope.is_none() && !self.line_in_scope(scopes, arena, line_index, scope) {
continue;
}
+ if !block_container.is_invalid() && self.caret_lines[line_index].block_container != block_container {
+ continue;
+ }
let line = self.caret_lines[line_index].clone();
let Some(local) = local_float_point(visual_context_tree, callbacks, line.context, point, respect_clip)
else {
@@ -939,14 +943,40 @@ impl HitTestList {
if let Some(item) = topmost_item {
let matches_hit_item = topmost_hit_item
.is_some_and(|hit| hit.index == item.index && self.item_is_direct_caret_target(arena, item.index));
- if (constrained || topmost_hit_item.is_none() || matches_hit_item)
- && let Some(caret) = self.caret_at_item(arena, item.index, item.local_point, CaretPositionType::Closest)
- {
- return Some(caret.with_debug_rect_in_viewport(
- tree,
- callbacks,
- self.items[item.index].context.spatial,
- ));
+ if constrained || topmost_hit_item.is_none() || matches_hit_item {
+ // NB: A point inside an inline-block resolves to a position in the box's own lines, if it has any, as
+ // a point over any other block container's content does, rather than to a boundary beside the
+ // whole box.
+ if self.items[item.index].kind == HitTestItemKind::Box {
+ let inner_line = self.find_closest_line(
+ scopes,
+ arena,
+ tree,
+ callbacks,
+ point,
+ mode,
+ FfiNodeIdentity::default(),
+ !constrained,
+ self.items[item.index].paintable,
+ );
+ if let Some(line_index) = inner_line.index
+ && let Some(caret) = self.caret_at_line(arena, line_index, inner_line.local_point, mode)
+ {
+ return Some(caret.with_debug_rect_in_viewport(
+ tree,
+ callbacks,
+ self.caret_lines[line_index].context.spatial,
+ ));
+ }
+ }
+ if let Some(caret) = self.caret_at_item(arena, item.index, item.local_point, CaretPositionType::Closest)
+ {
+ return Some(caret.with_debug_rect_in_viewport(
+ tree,
+ callbacks,
+ self.items[item.index].context.spatial,
+ ));
+ }
}
}
@@ -966,8 +996,19 @@ impl HitTestList {
// A constrained search must find a line even when the point is outside the scope's clipped area (e.g. dragging
// 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(scopes, arena, tree, callbacks, point, mode, scope, respect_clip);
+ let find_closest_line = |scope| {
+ self.find_closest_line(
+ scopes,
+ arena,
+ tree,
+ callbacks,
+ point,
+ mode,
+ scope,
+ respect_clip,
+ NodeSlotId::INVALID,
+ )
+ };
let mut closest_line = find_closest_line(line_scope);
let closest_line_in_scope = closest_line;
let point_is_in_column_of_closest_line = closest_line.index.is_some()
diff --git a/Tests/LibWeb/Text/expected/caret-position-in-inline-block-padding.txt b/Tests/LibWeb/Text/expected/caret-position-in-inline-block-padding.txt
new file mode 100644
index 00000000000..fd49f0e7ea8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/caret-position-in-inline-block-padding.txt
@@ -0,0 +1,4 @@
+inner box, left padding: "bar"@0
+inner box, right padding: "bar"@3
+outer box, padding above its line: "foo "@0
+dragged from "foo" into the inner box's left padding: "foo "
diff --git a/Tests/LibWeb/Text/input/caret-position-in-inline-block-padding.html b/Tests/LibWeb/Text/input/caret-position-in-inline-block-padding.html
new file mode 100644
index 00000000000..6e220a6f70c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/caret-position-in-inline-block-padding.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<style>
+ body { font: 20px serif; margin: 20px; }
+ span.box { border: 2px solid black; padding: 2em; display: inline-block; }
+</style>
+<span class="box" id="outer">foo <span class="box" id="inner">bar</span></span>
+<script src="include.js"></script>
+<script>
+ test(() => {
+ const describe = (x, y) => {
+ const position = document.caretPositionFromPoint(x, y);
+ const node = position.offsetNode;
+ return `${node.nodeType === Node.TEXT_NODE ? JSON.stringify(node.data) : node.nodeName}@${position.offset}`;
+ };
+ const outer = document.getElementById("outer").getBoundingClientRect();
+ const inner = document.getElementById("inner").getBoundingClientRect();
+ println(`inner box, left padding: ${describe(inner.left + 10, inner.top + inner.height / 2)}`);
+ println(`inner box, right padding: ${describe(inner.right - 10, inner.top + inner.height / 2)}`);
+ println(`outer box, padding above its line: ${describe(outer.right - 10, outer.top + 10)}`);
+
+ const text = document.getElementById("outer").firstChild;
+ const range = document.createRange();
+ range.setStart(text, 0);
+ range.setEnd(text, 1);
+ const start = range.getBoundingClientRect();
+ internals.mouseDown(start.left + 1, start.top + start.height / 2, 1, 0, 0, 1);
+ internals.mouseMove(inner.left + 10, inner.top + inner.height / 2, 0, 1);
+ internals.mouseUp(inner.left + 10, inner.top + inner.height / 2, 0, 0, 0);
+ println(`dragged from "foo" into the inner box's left padding: "${getSelection().toString()}"`);
+ });
+</script>