Skip to content

Truly independent web browser

LibWeb: Return a caret position over an element with no line

sideshowbarker1ea9b6dcc626
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
index bd6d54f6fbf..5351e7dfbd0 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
@@ -999,9 +999,18 @@ impl HitTestList {
caret.with_debug_rect_in_viewport(tree, callbacks, self.items[hit.index].context.spatial)
});
}
- return self
- .item_is_inline_adjacent_to_line(hit.index, line_index)
- .then_some(caret);
+ if self.item_is_inline_adjacent_to_line(hit.index, line_index) {
+ return Some(caret);
+ }
+ // A selection doesn't follow the point into an overlay or side content. But a plain caret query over an
+ // element whose content has no line near the point (an empty line, e.g.) puts the caret at the start of
+ // that element, rather than on a line outside it.
+ if mode != CaretPositionMode::Normal {
+ return None;
+ }
+ return self.caret_at_hit_container(arena, hit.index).map(|caret| {
+ caret.with_debug_rect_in_viewport(tree, callbacks, self.items[hit.index].context.spatial)
+ });
}
}
Some(caret)
diff --git a/Tests/LibWeb/Text/expected/hit_testing/caret-position-from-point-over-empty-line.txt b/Tests/LibWeb/Text/expected/hit_testing/caret-position-from-point-over-empty-line.txt
new file mode 100644
index 00000000000..4ff0f425865
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/hit_testing/caret-position-from-point-over-empty-line.txt
@@ -0,0 +1,6 @@
+(5, 9): #text "first line" @ 1
+(5, 20): div#empty @ 0
+(5, 27): div#empty @ 0
+(200, 27): div#empty @ 0
+(5, 35): div#empty @ 0
+(5, 45): #text "third line" @ 1
diff --git a/Tests/LibWeb/Text/expected/hit_testing/content-editable-toggle.txt b/Tests/LibWeb/Text/expected/hit_testing/content-editable-toggle.txt
index 5b4366f6a46..bec5b0156fa 100644
--- a/Tests/LibWeb/Text/expected/hit_testing/content-editable-toggle.txt
+++ b/Tests/LibWeb/Text/expected/hit_testing/content-editable-toggle.txt
@@ -1,5 +1,5 @@
-caret before toggle: null
+caret before toggle: [object Text] @0
caret with contenteditable: [object HTMLDivElement] @0
-caret after removal: null
+caret after removal: [object Text] @0
caret with designMode: [object HTMLDivElement] @0
-caret after designMode off: null
+caret after designMode off: [object Text] @0
diff --git a/Tests/LibWeb/Text/expected/hit_testing/inert-toggle-after-paint.txt b/Tests/LibWeb/Text/expected/hit_testing/inert-toggle-after-paint.txt
index 799a83f341f..dc105298f87 100644
--- a/Tests/LibWeb/Text/expected/hit_testing/inert-toggle-after-paint.txt
+++ b/Tests/LibWeb/Text/expected/hit_testing/inert-toggle-after-paint.txt
@@ -15,7 +15,7 @@ pseudo: pseudo / null
first letter: letter-row / #text
display: contents span inert:
text: text-row / #text
-contents: contents-row / null
+contents: contents-row / contents-row
pseudo: pseudo / null
first letter: letter-row / #text
display: contents span inert removed:
diff --git a/Tests/LibWeb/Text/input/hit_testing/caret-position-from-point-over-empty-line.html b/Tests/LibWeb/Text/input/hit_testing/caret-position-from-point-over-empty-line.html
new file mode 100644
index 00000000000..76017ffb4a5
--- /dev/null
+++ b/Tests/LibWeb/Text/input/hit_testing/caret-position-from-point-over-empty-line.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ .view-lines { position: relative; height: 54px; font: 14px monospace; user-select: none; }
+ .view-line { position: absolute; left: 0; width: 400px; height: 18px; white-space: nowrap; }
+</style>
+<div class="view-lines">
+ <div class="view-line" style="top: 0"><span><span>first line</span></span></div>
+ <div class="view-line" id="empty" style="top: 18px"><span><span></span></span></div>
+ <div class="view-line" style="top: 36px"><span><span>third line</span></span></div>
+</div>
+<script>
+ test(() => {
+ const describe = (position) => {
+ if (!position)
+ return "null";
+ const node = position.offsetNode;
+ const name = node.nodeType === Node.TEXT_NODE ? `#text "${node.data}"` : `${node.nodeName.toLowerCase()}${node.id ? "#" + node.id : ""}`;
+ return `${name} @ ${position.offset}`;
+ };
+ const results = [];
+ for (const [x, y] of [[5, 9], [5, 20], [5, 27], [200, 27], [5, 35], [5, 45]])
+ results.push(`(${x}, ${y}): ${describe(document.caretPositionFromPoint(x, y))}`);
+ for (const line of results)
+ println(line);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/hit_testing/content-editable-toggle.html b/Tests/LibWeb/Text/input/hit_testing/content-editable-toggle.html
index 36af91c2040..c808b590e9a 100644
--- a/Tests/LibWeb/Text/input/hit_testing/content-editable-toggle.html
+++ b/Tests/LibWeb/Text/input/hit_testing/content-editable-toggle.html
@@ -7,43 +7,45 @@
</style>
<script src="../include.js"></script>
<div id="target"></div>
-<!-- Without the empty-editable caret target, the query resolves against this text's caret
- line instead of the div, so the editable and non-editable answers differ. -->
+<!-- The query point is beside the div, over nothing but the body. Without the empty-editable
+ caret target, it resolves against this text's caret line instead of the div's, so the
+ editable and non-editable answers differ. (A point over the div itself is at the div's start
+ either way.) -->
<p id="text-below">some text below the target</p>
<script>
test(() => {
const { x, y, width, height } = target.getBoundingClientRect();
- const cx = x + width / 2;
+ const cx = x + width + 50;
const cy = y + height / 2;
- const caretNodeAtCenter = () => {
+ const caretNodeBesideTarget = () => {
const position = document.caretPositionFromPoint(cx, cy);
return position ? `${position.offsetNode} @${position.offset}` : "null";
};
// Query once and then force a re-record, so the queries below run against
// steady-state recordings rather than the initial one.
- caretNodeAtCenter();
+ caretNodeBesideTarget();
document.body.style.outline = "1px solid transparent";
// NB: Results are buffered and printed at the end: println() itself mutates the DOM,
// and any unrelated invalidation between a toggle and its query would rebuild the
// hit-test display list and mask a missed invalidation from the toggle itself.
const results = [];
- results.push(`caret before toggle: ${caretNodeAtCenter()}`);
+ results.push(`caret before toggle: ${caretNodeBesideTarget()}`);
target.setAttribute("contenteditable", "");
- results.push(`caret with contenteditable: ${caretNodeAtCenter()}`);
+ results.push(`caret with contenteditable: ${caretNodeBesideTarget()}`);
target.removeAttribute("contenteditable");
- results.push(`caret after removal: ${caretNodeAtCenter()}`);
+ results.push(`caret after removal: ${caretNodeBesideTarget()}`);
// designMode has no attribute-change path, so recomputing the editable flags is the
// only invalidation these toggles get.
document.designMode = "on";
- results.push(`caret with designMode: ${caretNodeAtCenter()}`);
+ results.push(`caret with designMode: ${caretNodeBesideTarget()}`);
document.designMode = "off";
- results.push(`caret after designMode off: ${caretNodeAtCenter()}`);
+ results.push(`caret after designMode off: ${caretNodeBesideTarget()}`);
for (const result of results)
println(result);