From 7f3192e9bb5eac25c47c287aff624752bb680e4f Mon Sep 17 00:00:00 2001 From: Annya Date: Fri, 20 Sep 2024 21:58:44 +0800 Subject: [PATCH] LibWeb: Fix "incorrect behavior on select()" This patch resolves the problem outlined in issue #1446, where the cursor_position node could be substituted with any other existing node by the mousedown event. Previously, if this new node was not equal to m_text_node, the function would return prematurely without updating the selection. With this fix, we ensure that the selection is properly updated in such cases, rather than simply exiting the function. Also, in order to address the issue in #1339, the logic of document().set_cursor_position has been put behind to the logic of Selection. Closes #1446 --- Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp | 7 ++++--- Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 97d3087bcc51..6d0058284d61 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -2429,13 +2429,14 @@ HTMLInputElement::ValueAttributeMode HTMLInputElement::value_attribute_mode() co void HTMLInputElement::selection_was_changed(size_t selection_start, size_t selection_end) { - if (!m_text_node || !document().cursor_position() || document().cursor_position()->node() != m_text_node) + if (!m_text_node) return; - document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, selection_end)); - if (auto selection = document().get_selection()) MUST(selection->set_base_and_extent(*m_text_node, selection_start, *m_text_node, selection_end)); + + if (document().cursor_position() && document().cursor_position()->node() == m_text_node) + document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, selection_end)); } } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp index f52e4de74e88..cb6acd51f891 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp @@ -461,13 +461,14 @@ void HTMLTextAreaElement::queue_firing_input_event() void HTMLTextAreaElement::selection_was_changed(size_t selection_start, size_t selection_end) { - if (!m_text_node || !document().cursor_position() || document().cursor_position()->node() != m_text_node) + if (!m_text_node) return; - document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, selection_end)); - if (auto selection = document().get_selection()) MUST(selection->set_base_and_extent(*m_text_node, selection_start, *m_text_node, selection_end)); + + if (document().cursor_position() && document().cursor_position()->node() == m_text_node) + document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, selection_end)); } }