From 738eb68dda6f2585342f0cb4bbd3fbb79cba24af Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Wed, 20 Aug 2025 10:49:28 +0200 Subject: [PATCH] LibWeb: Always focus on editing host if currently not focused We were constraining the focusing behavior for editing hosts a bit too much; regardless of how the selection changed, if the start container is inside an editing host and it's currently not focused, we should focus it. This fixes focus stealing by other elements that set a selection inside an editing host on a click event, for example. --- Libraries/LibWeb/Selection/Selection.cpp | 3 +- .../selection-focuses-editing-host.txt | 9 +++++- .../input/selection-focuses-editing-host.html | 32 ++++++++++++++----- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Libraries/LibWeb/Selection/Selection.cpp b/Libraries/LibWeb/Selection/Selection.cpp index 8777d139ab9..51b268749cd 100644 --- a/Libraries/LibWeb/Selection/Selection.cpp +++ b/Libraries/LibWeb/Selection/Selection.cpp @@ -549,9 +549,8 @@ void Selection::set_range(GC::Ptr range) // https://developer.mozilla.org/en-US/docs/Web/API/Selection#behavior_of_selection_api_in_terms_of_editing_host_focus_changes // AD-HOC: Focus editing host if the previous selection was outside of it. There seems to be no spec for this. if (range && range->start_container()->is_editable_or_editing_host()) { - GC::Ptr old_editing_host = old_range ? old_range->start_container()->editing_host() : nullptr; GC::Ref new_editing_host = *range->start_container()->editing_host(); - if (new_editing_host != old_editing_host && document()->focused_element() != new_editing_host) { + if (document()->focused_element() != new_editing_host) { // FIXME: Determine and propagate the right focus trigger. HTML::run_focusing_steps(new_editing_host, nullptr, HTML::FocusTrigger::Other); } diff --git a/Tests/LibWeb/Text/expected/selection-focuses-editing-host.txt b/Tests/LibWeb/Text/expected/selection-focuses-editing-host.txt index be2795ad27e..be67c9120c4 100644 --- a/Tests/LibWeb/Text/expected/selection-focuses-editing-host.txt +++ b/Tests/LibWeb/Text/expected/selection-focuses-editing-host.txt @@ -1,2 +1,9 @@ +-- Simple editing host -- Range: [object Text] 1 [object Text] 2 -document.activeElement: [object HTMLDivElement] +
+-- Editing host with nested -- +Range: [object Text] 1 [object Text] 1 +
+-- Refocusing on same editing host -- +Range: [object Text] 0 [object Text] 0 +
diff --git a/Tests/LibWeb/Text/input/selection-focuses-editing-host.html b/Tests/LibWeb/Text/input/selection-focuses-editing-host.html index bad433e94f7..1b71c937e5f 100644 --- a/Tests/LibWeb/Text/input/selection-focuses-editing-host.html +++ b/Tests/LibWeb/Text/input/selection-focuses-editing-host.html @@ -1,15 +1,31 @@ -
foo
+
foo
+
foobar
+