diff --git a/Libraries/LibWeb/HTML/HTMLElement.cpp b/Libraries/LibWeb/HTML/HTMLElement.cpp
index 9ccaa06d8d5..bb5b9a8cec1 100644
--- a/Libraries/LibWeb/HTML/HTMLElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLElement.cpp
@@ -18,6 +18,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -41,6 +42,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -2039,6 +2041,12 @@ void HTMLElement::did_receive_focus()
auto editing_host = document().editing_host_manager();
editing_host->set_active_contenteditable_element(this);
+ // Don't update the selection if we're already part of the active range.
+ if (auto range = document().get_selection()->range()) {
+ if (is_inclusive_ancestor_of(range->start_container()) || is_inclusive_ancestor_of(range->end_container()))
+ return;
+ }
+
DOM::Text* text = nullptr;
for_each_in_inclusive_subtree_of_type([&](auto& node) {
text = &node;
diff --git a/Tests/LibWeb/Text/expected/selection-in-focused-editing-host.txt b/Tests/LibWeb/Text/expected/selection-in-focused-editing-host.txt
new file mode 100644
index 00000000000..639b40d9271
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/selection-in-focused-editing-host.txt
@@ -0,0 +1,5 @@
+Range: [object Text] 1 [object Text] 2
+document.activeElement: [object HTMLDivElement]
+Selection must be the same as above:
+Range: [object Text] 1 [object Text] 2
+document.activeElement: [object HTMLDivElement]
diff --git a/Tests/LibWeb/Text/input/selection-in-focused-editing-host.html b/Tests/LibWeb/Text/input/selection-in-focused-editing-host.html
new file mode 100644
index 00000000000..7b072529521
--- /dev/null
+++ b/Tests/LibWeb/Text/input/selection-in-focused-editing-host.html
@@ -0,0 +1,26 @@
+
+foo
+
+