LibWeb: Don't move focus when setting input value

This commit is contained in:
Gingeh 2024-09-10 11:28:44 +10:00 committed by Jelle Raaijmakers
commit 1d9c404b8c
Notes: github-actions[bot] 2024-09-12 09:46:32 +00:00
7 changed files with 59 additions and 26 deletions

View file

@ -508,7 +508,7 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_range(
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#set-the-selection-range
void FormAssociatedTextControlElement::set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction)
void FormAssociatedTextControlElement::set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction, SelectionSource source)
{
// 1. If start is null, let start be zero.
start = start.value_or(0);
@ -551,9 +551,9 @@ void FormAssociatedTextControlElement::set_the_selection_range(Optional<WebIDL::
if (was_modified) {
auto& html_element = form_associated_element_to_html_element();
// AD-HOC: If there is no selection, we do not fire the event. This seems to correspond to how
// other browsers behave.
if (m_selection_start != m_selection_end) {
// AD-HOC: We don't fire the event if the user moves the cursor without selecting any text.
// This is not in the spec but matches how other browsers behave.
if (source == SelectionSource::DOM || m_selection_start != m_selection_end) {
html_element.queue_an_element_task(Task::Source::UserInteraction, [&html_element] {
auto select_event = DOM::Event::create(html_element.realm(), EventNames::select, { .bubbles = true });
static_cast<DOM::EventTarget*>(&html_element)->dispatch_event(select_event);

View file

@ -122,6 +122,11 @@ private:
bool m_parser_inserted { false };
};
enum class SelectionSource {
UI,
DOM,
};
class FormAssociatedTextControlElement : public FormAssociatedElement {
public:
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
@ -151,7 +156,7 @@ public:
WebIDL::ExceptionOr<void> set_range_text(String const& replacement, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end, Bindings::SelectionMode = Bindings::SelectionMode::Preserve);
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
void set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction = SelectionDirection::None);
void set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction = SelectionDirection::None, SelectionSource source = SelectionSource::DOM);
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
WebIDL::ExceptionOr<void> set_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, Optional<String> direction);

View file

@ -562,7 +562,7 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_value(String const& value)
m_text_node->set_data(m_value);
update_placeholder_visibility();
document().set_cursor_position(DOM::Position::create(realm, *m_text_node, m_text_node->data().bytes().size()));
set_the_selection_range(m_text_node->length(), m_text_node->length());
}
update_shadow_tree();
@ -2429,7 +2429,7 @@ HTMLInputElement::ValueAttributeMode HTMLInputElement::value_attribute_mode() co
void HTMLInputElement::selection_was_changed(size_t selection_start, size_t selection_end)
{
if (!m_text_node)
if (!m_text_node || !document().cursor_position() || document().cursor_position()->node() != m_text_node)
return;
document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, selection_end));

View file

@ -164,8 +164,6 @@ String HTMLTextAreaElement::value() const
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-value
void HTMLTextAreaElement::set_value(String const& value)
{
auto& realm = this->realm();
// 1. Let oldAPIValue be this element's API value.
auto old_api_value = api_value();
@ -182,7 +180,7 @@ void HTMLTextAreaElement::set_value(String const& value)
m_text_node->set_data(m_raw_value);
update_placeholder_visibility();
document().set_cursor_position(DOM::Position::create(realm, *m_text_node, m_text_node->data().bytes().size()));
set_the_selection_range(m_text_node->length(), m_text_node->length());
}
}
}
@ -463,7 +461,7 @@ void HTMLTextAreaElement::queue_firing_input_event()
void HTMLTextAreaElement::selection_was_changed(size_t selection_start, size_t selection_end)
{
if (!m_text_node)
if (!m_text_node || !document().cursor_position() || document().cursor_position()->node() != m_text_node)
return;
document().set_cursor_position(DOM::Position::create(realm(), *m_text_node, selection_end));