1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 23:17:44 +00:00

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
8 changed files with 88 additions and 11 deletions

View file

@ -13,6 +13,7 @@
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLIFrameElement.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLMediaElement.h>
#include <LibWeb/HTML/HTMLVideoElement.h>
#include <LibWeb/Layout/Viewport.h>
@ -769,6 +770,8 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin
}
if (m_browsing_context->cursor_position() && m_browsing_context->cursor_position()->node()->is_editable()) {
auto& node = verify_cast<DOM::Text>(*m_browsing_context->cursor_position()->node());
if (key == KeyCode::Key_Backspace) {
if (!m_browsing_context->decrement_cursor_position_offset()) {
// FIXME: Move to the previous node and delete the last character there.
@ -799,15 +802,18 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin
return true;
}
if (key == KeyCode::Key_Home) {
auto& node = verify_cast<DOM::Text>(*m_browsing_context->cursor_position()->node());
m_browsing_context->set_cursor_position(DOM::Position::create(realm, node, 0));
return true;
}
if (key == KeyCode::Key_End) {
auto& node = verify_cast<DOM::Text>(*m_browsing_context->cursor_position()->node());
m_browsing_context->set_cursor_position(DOM::Position::create(realm, node, (unsigned)node.data().bytes().size()));
return true;
}
if (key == KeyCode::Key_Return && is<HTML::HTMLInputElement>(node.editable_text_node_owner())) {
auto& input_element = static_cast<HTML::HTMLInputElement&>(*node.editable_text_node_owner());
input_element.commit_pending_changes();
return true;
}
if (!should_ignore_keydown_event(code_point)) {
m_edit_event_handler->handle_insert(JS::NonnullGCPtr { *m_browsing_context->cursor_position() }, code_point);
m_browsing_context->increment_cursor_position_offset();