diff --git a/Tests/LibWeb/Text/expected/contenteditable-should-insert-into-nested-node.txt b/Tests/LibWeb/Text/expected/contenteditable-should-insert-into-nested-node.txt
new file mode 100644
index 00000000000..624a9fc578c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/contenteditable-should-insert-into-nested-node.txt
@@ -0,0 +1 @@
+hello!!!
diff --git a/Tests/LibWeb/Text/input/contenteditable-should-insert-into-nested-node.html b/Tests/LibWeb/Text/input/contenteditable-should-insert-into-nested-node.html
new file mode 100644
index 00000000000..5aee244b769
--- /dev/null
+++ b/Tests/LibWeb/Text/input/contenteditable-should-insert-into-nested-node.html
@@ -0,0 +1,10 @@
+
+
+
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp
index 1e4dd593e0b..ba0e91b7924 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp
+++ b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp
@@ -757,7 +757,18 @@ void HTMLElement::did_receive_focus()
{
if (m_content_editable_state != ContentEditableState::True)
return;
- document().set_cursor_position(DOM::Position::create(realm(), *this, 0));
+
+ DOM::Text* text = nullptr;
+ for_each_in_inclusive_subtree_of_type([&](auto& node) {
+ text = &node;
+ return TraversalDecision::Continue;
+ });
+
+ if (!text) {
+ document().set_cursor_position(DOM::Position::create(realm(), *this, 0));
+ return;
+ }
+ document().set_cursor_position(DOM::Position::create(realm(), *text, text->length()));
}
// https://html.spec.whatwg.org/multipage/interaction.html#dom-accesskeylabel