Truly independent web browser
LibWeb: Don’t focus an editor from a click beside it
sideshowbarkerd0bffbbe71b7
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cppindex 4386a7ae6e4..00eed7840d3 100644--- a/Libraries/LibWeb/Page/EventHandler.cpp+++ b/Libraries/LibWeb/Page/EventHandler.cpp@@ -123,8 +123,10 @@ static GC::Ptr<DOM::Node> associated_descendant_editing_host(Layout::BegunRead c});if (editing_host && !has_multiple_editing_hosts) {+ // NB: An editing host inside the hit node is not what the click is on: The click landed on the hit node+ // around it, as in its padding or below it.if (ancestor == &node)- return editing_host;+ return nullptr;auto const* editing_host_layout_node = editing_host->layout_node(read);if (hit_node_rect.has_value() && editing_host_layout_node && Painting::has_committed_box(*editing_host_layout_node)&& hit_node_rect->intersects(Painting::absolute_rect(*editing_host_layout_node)))@@ -204,6 +206,46 @@ static Optional<Painting::CaretPosition> caret_position_from_editable_hit_node(L};}+// A click on content outside a block-level editing host puts the caret before or after the host in its parent, rather+// than inside it, as the caret position from the point does when it snaps to the host's nearest text. So the click+// neither selects in the host nor focuses it. The caret goes before the host where it landed at the start of the+// host's content, and after it otherwise. An inline editing host takes the caret, as on the line beside it.+static Optional<Painting::CaretPosition> caret_position_beside_block_editing_host(Layout::BegunRead const& read, Painting::CaretPosition const& caret_position, DOM::Node& hit_node)+{+ auto boundary_node = caret_position.boundary_node();+ if (!boundary_node || !boundary_node->is_editable_or_editing_host() || hit_node.is_editable_or_editing_host())+ return {};+ auto editing_host = boundary_node->is_editing_host() ? boundary_node : boundary_node->editing_host();+ if (!editing_host || hit_node.is_inclusive_descendant_of(*editing_host))+ return {};+ auto const* editing_host_layout_node = as_if<Layout::NodeWithStyle>(editing_host->layout_node(read));+ if (!editing_host_layout_node || editing_host_layout_node->display().is_inline_outside())+ return {};+ auto parent = editing_host->parent();+ if (!parent)+ return {};++ auto first_leaf = first_editable_leaf_descendant(*editing_host);+ bool const at_start = caret_position.boundary.offset == 0+ && (boundary_node == editing_host || boundary_node == first_leaf);+ auto offset = editing_host->index() + (at_start ? 0 : 1);++ auto* arena = hit_node.document().layout_node_arena_if_created();+ if (!arena)+ return {};+ auto* layout_node = DOM::NodeIdentity::of(*parent).bound_layout_node(read, *arena);+ if (!layout_node || !Painting::has_committed_box(*layout_node))+ layout_node = DOM::NodeIdentity::of(hit_node).bound_layout_node(read, *arena);+ if (!layout_node || !Painting::has_committed_box(*layout_node))+ return {};++ return Painting::CaretPosition {+ .paintable = Painting::committed_row_slot(*layout_node),+ .arena = *arena,+ .boundary = { DOM::NodeIdentity::of(*parent), static_cast<WebIDL::UnsignedLong>(offset) },+ };+}+static bool should_use_caret_position_from_editable_hit_node(Optional<Painting::CaretPosition> const& caret_position, DOM::Node& hit_node, DOM::Node& editing_host){if (!hit_node.is_inclusive_descendant_of(editing_host))@@ -3688,6 +3730,11 @@ Optional<Painting::CaretPosition> EventHandler::caret_position_for_mouse_selectiif (!caret_position.has_value())return {};+ if (editable_hit_node_before_focus && editable_hit_node_before_focus->is_connected() && !editing_host_before_focus) {+ if (auto beside = caret_position_beside_block_editing_host(read, *caret_position, *editable_hit_node_before_focus); beside.has_value())+ caret_position = beside;+ }+return caret_position;}diff --git a/Tests/LibWeb/Text/expected/Editing/click-beside-block-editing-host.txt b/Tests/LibWeb/Text/expected/Editing/click-beside-block-editing-host.txtnew file mode 100644index 00000000000..560847b8645--- /dev/null+++ b/Tests/LibWeb/Text/expected/Editing/click-beside-block-editing-host.txt@@ -0,0 +1,5 @@+Wrapper padding around a block editor: focus body, caret in div#wrapper+Inside a block editor: focus p#editorInArea, caret in p#editorInArea+Then far below it in its container: focus body+Typing then leaves the editor as it was: true+On the line beside an inline editor: focus span#inlineEditor, caret in span#inlineEditordiff --git a/Tests/LibWeb/Text/input/Editing/click-beside-block-editing-host.html b/Tests/LibWeb/Text/input/Editing/click-beside-block-editing-host.htmlnew file mode 100644index 00000000000..e8a0d1fab28--- /dev/null+++ b/Tests/LibWeb/Text/input/Editing/click-beside-block-editing-host.html@@ -0,0 +1,43 @@+<!DOCTYPE html>+<style>+ body { margin: 0; }+ p { margin: 0; }+ #wrapper { padding: 30px; width: 300px; }+ #area { height: 300px; }+</style>+<div id="area"><p id="editorInArea" contenteditable>editable</p></div>+<div id="wrapper"><div id="editorInWrapper" contenteditable>editable</div></div>+<div><span id="inlineEditor" contenteditable>editable</span></div>+<script src="../include.js"></script>+<script>+ // A click beside a block-level editing host, on the non-editable content around it, focuses nothing in it and+ // leaves the caret outside it, so typing doesn't go into it. A click on the line beside an inline editing host+ // puts the caret in it.+ asyncTest(async done => {+ const name = node => node ? node.localName + (node.id ? `#${node.id}` : "") : "null";+ const rect = id => document.getElementById(id).getBoundingClientRect();+ const report = label => {+ const anchor = getSelection().anchorNode;+ const anchorElement = anchor && anchor.nodeType !== Node.ELEMENT_NODE ? anchor.parentElement : anchor;+ println(`${label}: focus ${name(document.activeElement)}, caret in ${name(anchorElement)}`);+ };+ const click = async (x, y) => {+ internals.click(x, y);+ await new Promise(resolve => requestAnimationFrame(resolve));+ };++ await click(rect("wrapper").left + 5, rect("wrapper").top + 5);+ report("Wrapper padding around a block editor");++ await click(rect("editorInArea").left + 5, rect("editorInArea").top + 5);+ report("Inside a block editor");+ await click(rect("area").right - 50, rect("area").bottom - 10);+ println(`Then far below it in its container: focus ${name(document.activeElement)}`);+ internals.sendText(document.body, "X");+ println(`Typing then leaves the editor as it was: ${document.getElementById("editorInArea").textContent === "editable"}`);++ await click(rect("inlineEditor").right + 300, rect("inlineEditor").top + rect("inlineEditor").height / 2);+ report("On the line beside an inline editor");+ done();+ });+</script>