1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:37:46 +00:00

LibWeb: Fire "input" and "change" events when editing a text <input>

This isn't entirely on-spec, but will hopefully allow us to make
progress in other areas.
This commit is contained in:
Andreas Kling 2022-02-17 12:59:32 +01:00
parent 4cbce0e34c
commit 5f54b8dd6c
5 changed files with 39 additions and 1 deletions

View file

@ -11,6 +11,7 @@
#include <LibWeb/HTML/BrowsingContextContainer.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/Layout/BreakNode.h>
#include <LibWeb/Layout/InitialContainingBlock.h>
#include <LibWeb/Layout/TextNode.h>
@ -41,6 +42,12 @@ BrowsingContext::~BrowsingContext()
void BrowsingContext::did_edit(Badge<EditEventHandler>)
{
reset_cursor_blink_cycle();
if (m_cursor_position.node() && is<DOM::Text>(*m_cursor_position.node())) {
auto& text_node = static_cast<DOM::Text&>(*m_cursor_position.node());
if (auto* input_element = text_node.owner_input_element())
input_element->did_edit_text_node({});
}
}
void BrowsingContext::reset_cursor_blink_cycle()

View file

@ -112,6 +112,23 @@ void HTMLInputElement::run_input_activation_behavior()
}
}
void HTMLInputElement::did_edit_text_node(Badge<BrowsingContext>)
{
// NOTE: This is a bit ad-hoc, but basically implements part of "4.10.5.5 Common event behaviors"
// https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
auto input_event = DOM::Event::create(HTML::EventNames::input);
input_event->set_bubbles(true);
input_event->set_composed(true);
dispatch_event(move(input_event));
// FIXME: This should only fire when the input is "committed", whatever that means.
auto change_event = DOM::Event::create(HTML::EventNames::change);
change_event->set_bubbles(true);
dispatch_event(move(change_event));
});
}
bool HTMLInputElement::enabled() const
{
return !has_attribute(HTML::AttributeNames::disabled);
@ -147,6 +164,7 @@ void HTMLInputElement::create_shadow_tree_if_needed()
element->set_attribute(HTML::AttributeNames::style, "white-space: pre");
m_text_node = adopt_ref(*new DOM::Text(document(), initial_value));
m_text_node->set_always_editable(true);
m_text_node->set_owner_input_element({}, *this);
element->append_child(*m_text_node);
shadow_root->append_child(move(element));
set_shadow_root(move(shadow_root));

View file

@ -42,6 +42,8 @@ public:
void did_click_button(Badge<Layout::ButtonBox>);
void did_edit_text_node(Badge<BrowsingContext>);
virtual bool is_focusable() const override;
virtual void parse_attribute(FlyString const&, String const&) override;