Bladeren bron

LibWeb: Put cursor in last text node when contenteditable is focused

With this change we match behavior of other engines a bit more closer.
Aliaksandr Kalenik 8 maanden geleden
bovenliggende
commit
d88e6bee5d

+ 1 - 0
Tests/LibWeb/Text/expected/contenteditable-should-insert-into-nested-node.txt

@@ -0,0 +1 @@
+hello!!!

+ 10 - 0
Tests/LibWeb/Text/input/contenteditable-should-insert-into-nested-node.html

@@ -0,0 +1,10 @@
+<script src="include.js"></script>
+<div id="editable" contenteditable="true"><p id="text">hello</p></div>
+<script>
+    test(() => {
+        const editable = document.getElementById('editable');
+        internals.sendText(editable, "!!!");
+        const text = document.getElementById('text');
+        println(text.textContent);
+    });
+</script>

+ 12 - 1
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<DOM::Text>([&](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