Truly independent web browser
LibWeb: Move the selection on a click below an element’s text
sideshowbarker6ad276a0c1ed
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rsindex acf65520662..ce8e82a68ac 100644--- a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs@@ -969,6 +969,7 @@ impl HitTestList {let find_closest_line =|scope| self.find_closest_line(scopes, arena, tree, callbacks, point, mode, scope, respect_clip);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()&& closest_line.fragment_inline_distance == CssPixels::default()&& arena.paintable_row_is_populated(closest_line.block_container)@@ -1013,11 +1014,21 @@ impl HitTestList {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.+ // A selection doesn't follow the point into an overlay or side content: It stays in the hit element's+ // own content, on its line closest to the point, as a closer line outside it took the place of. 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;+ let line_index_in_scope = closest_line_in_scope.index.filter(|&index| index != line_index)?;+ return self+ .caret_at_line(arena, line_index_in_scope, closest_line_in_scope.local_point, mode)+ .map(|caret| {+ caret.with_debug_rect_in_viewport(+ tree,+ callbacks,+ self.caret_lines[line_index_in_scope].context.spatial,+ )+ });}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)diff --git a/Tests/LibWeb/Text/expected/selection-click-below-text-with-closer-line-outside.txt b/Tests/LibWeb/Text/expected/selection-click-below-text-with-closer-line-outside.txtnew file mode 100644index 00000000000..d2249f92052--- /dev/null+++ b/Tests/LibWeb/Text/expected/selection-click-below-text-with-closer-line-outside.txt@@ -0,0 +1,2 @@+After a click in the header: text in div#header, offset 1+After a click far below the paragraph: text in p#paragraph, offset 14diff --git a/Tests/LibWeb/Text/input/selection-click-below-text-with-closer-line-outside.html b/Tests/LibWeb/Text/input/selection-click-below-text-with-closer-line-outside.htmlnew file mode 100644index 00000000000..475e8c1153d--- /dev/null+++ b/Tests/LibWeb/Text/input/selection-click-below-text-with-closer-line-outside.html@@ -0,0 +1,36 @@+<!DOCTYPE html>+<style>+ body { margin: 0; }+ p { margin: 0; }+ #header { height: 40px; }+ #area { height: 200px; }+</style>+<div id="header">header text</div>+<div id="area"><p id="paragraph">paragraph text</p></div>+<div><span id="below">text below the area</span></div>+<script src="include.js"></script>+<script>+ // A click in the empty part of an element, far below its text but close to a line outside it, puts the caret on+ // the element's own closest line, and moves the selection there.+ asyncTest(async done => {+ const describe = () => {+ const selection = getSelection();+ const node = selection.anchorNode;+ const element = node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;+ return `${node.nodeType === Node.TEXT_NODE ? "text in " : ""}${element.localName}#${element.id}, offset ${selection.anchorOffset}`;+ };+ const click = async (x, y) => {+ internals.click(x, y);+ await new Promise(resolve => requestAnimationFrame(resolve));+ };++ const header = document.getElementById("header").getBoundingClientRect();+ await click(header.left + 10, header.top + 10);+ println(`After a click in the header: ${describe()}`);++ const area = document.getElementById("area").getBoundingClientRect();+ await click(area.right - 50, area.bottom - 5);+ println(`After a click far below the paragraph: ${describe()}`);+ done();+ });+</script>