Skip to content

Commit

Permalink
LibWeb: Fix "incorrect behavior on select()"
Browse files Browse the repository at this point in the history
This patch resolves the problem outlined in issue LadybirdBrowser#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 LadybirdBrowser#1339, the logic of
document().set_cursor_position has been put behind to the logic of
Selection.

Closes LadybirdBrowser#1446
  • Loading branch information
An-n-ya committed Sep 20, 2024
1 parent 5ac0e81 commit 7f3192e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
7 changes: 4 additions & 3 deletions Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}

0 comments on commit 7f3192e

Please sign in to comment.