diff --git a/Libraries/LibWeb/Selection/Selection.cpp b/Libraries/LibWeb/Selection/Selection.cpp index c2cf68f4678..6391939b154 100644 --- a/Libraries/LibWeb/Selection/Selection.cpp +++ b/Libraries/LibWeb/Selection/Selection.cpp @@ -550,6 +550,8 @@ void Selection::set_range(GC::Ptr range) // 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::Ref new_editing_host = *range->start_container()->editing_host(); + while (new_editing_host->parent() && new_editing_host->parent()->is_editing_host()) + new_editing_host = *new_editing_host->parent(); 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 be67c9120c4..d4681df908a 100644 --- a/Tests/LibWeb/Text/expected/selection-focuses-editing-host.txt +++ b/Tests/LibWeb/Text/expected/selection-focuses-editing-host.txt @@ -7,3 +7,9 @@ Range: [object Text] 1 [object Text] 1 -- Refocusing on same editing host -- Range: [object Text] 0 [object Text] 0
+-- Nested editing host -- +Range: [object Text] 0 [object Text] 3 +
+-- Nested editing host with uneditable element as parent -- +Range: [object Text] 0 [object Text] 3 + diff --git a/Tests/LibWeb/Text/input/selection-focuses-editing-host.html b/Tests/LibWeb/Text/input/selection-focuses-editing-host.html index 1b71c937e5f..37a0a2daae7 100644 --- a/Tests/LibWeb/Text/input/selection-focuses-editing-host.html +++ b/Tests/LibWeb/Text/input/selection-focuses-editing-host.html @@ -2,6 +2,8 @@
foo
foobar
+
foo bar
+
foo bar baz