From fc37c4ad40beba249a7ea5452f11014d43da68f6 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 7 Sep 2024 17:42:18 -0400 Subject: [PATCH] LibWeb: Only set the editable text cursor position if necessary When the user clicks on a text node, the event handler sets the cursor position to the location that was clicked. But it would then be set back to 0 in the DOM node's focus handler. Leave the cursor alone, unless the the DOM node was never set as the cursor position node (which will occur when the user clicks on the DOM node, but outside the shadow text node). In that case, move the cursor to the end of the text node. The end result here is that the cursor is placed where the user clicked, or set to the end of node if the user clicked outside of the shadow text node. --- Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp | 3 ++- Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index ecec8b66287..2a573ce11af 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -1147,7 +1147,8 @@ void HTMLInputElement::did_receive_focus() if (m_placeholder_text_node) m_placeholder_text_node->invalidate_style(DOM::StyleInvalidationReason::DidReceiveFocus); - document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, 0)); + if (auto cursor = document().cursor_position(); !cursor || m_text_node != cursor->node()) + document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, m_text_node->length())); } void HTMLInputElement::did_lose_focus() diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp index 04412c87de1..4d0b5dcd304 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp @@ -77,7 +77,8 @@ void HTMLTextAreaElement::did_receive_focus() if (m_placeholder_text_node) m_placeholder_text_node->invalidate_style(DOM::StyleInvalidationReason::DidReceiveFocus); - document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, 0)); + if (auto cursor = document().cursor_position(); !cursor || m_text_node != cursor->node()) + document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, 0)); } void HTMLTextAreaElement::did_lose_focus()