LibWeb: Support committing changes to <input> elements

We currently fire the change event on <input> elements when they lose
focus. The spec allows for us to also fire the event when changes are
"committed", so long as such an action makes sense for the input type.

This patch detects when the return key is entered in an <input> element
and uses that as the commit action for text-related types. If no change
has occurred since the last commit, no change event is fired.
This commit is contained in:
Timothy Flynn 2023-11-30 12:54:30 -05:00 committed by Andreas Kling
parent 97665fa92f
commit 7edfeb7056
Notes: sideshowbarker 2024-07-17 09:41:18 +09:00
8 changed files with 88 additions and 11 deletions

View file

@ -276,8 +276,6 @@ WebIDL::ExceptionOr<void> HTMLInputElement::run_input_activation_behavior()
TRY(form->submit_form(*this));
} else if (type_state() == TypeAttributeState::FileUpload || type_state() == TypeAttributeState::Color) {
show_the_picker_if_applicable(*this);
} else {
dispatch_event(DOM::Event::create(realm(), EventNames::change));
}
return {};
@ -289,6 +287,8 @@ void HTMLInputElement::did_edit_text_node(Badge<BrowsingContext>)
m_value = value_sanitization_algorithm(m_text_node->data());
m_dirty_value = true;
m_has_uncommitted_changes = true;
update_placeholder_visibility();
// NOTE: This is a bit ad-hoc, but basically implements part of "4.10.5.5 Common event behaviors"
@ -406,6 +406,33 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_value(String const& value)
return {};
}
void HTMLInputElement::commit_pending_changes()
{
// The change event fires when the value is committed, if that makes sense for the control,
// or else when the control loses focus
switch (type_state()) {
case TypeAttributeState::Email:
case TypeAttributeState::Password:
case TypeAttributeState::Search:
case TypeAttributeState::Telephone:
case TypeAttributeState::Text:
case TypeAttributeState::URL:
if (!m_has_uncommitted_changes)
return;
break;
default:
break;
}
m_has_uncommitted_changes = false;
queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
auto change_event = DOM::Event::create(realm(), HTML::EventNames::change, { .bubbles = true });
dispatch_event(change_event);
});
}
void HTMLInputElement::update_placeholder_visibility()
{
if (!m_placeholder_element)
@ -615,13 +642,7 @@ void HTMLInputElement::did_receive_focus()
void HTMLInputElement::did_lose_focus()
{
// The change event fires when the value is committed, if that makes sense for the control,
// or else when the control loses focus
queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
change_event->set_bubbles(true);
dispatch_event(change_event);
});
commit_pending_changes();
}
void HTMLInputElement::attribute_changed(FlyString const& name, Optional<String> const& value)