mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-27 14:58:46 +00:00
LibWeb: Refactor all LabelableNode subclasses + input event handling :^)
This commit is messy due to the Paintable and Layout classes being tangled together. The RadioButton, CheckBox and ButtonBox classes are now subclasses of FormAssociatedLabelableNode. This subclass separates these layout nodes from LabelableNode, which is also the superclass of non-form associated labelable nodes (Progress). ButtonPaintable, CheckBoxPaintable and RadioButtonPaintable no longer call events on DOM nodes directly from their mouse event handlers; instead, all the functionality is now directly in EventHandler, which dispatches the related events. handle_mousedown and related methods return a bool indicating whether the event handling should proceed. Paintable classes can now return an alternative DOM::Node which should be the target of the mouse event. Labels use this to indicate that the labeled control should be the target of the mouse events. HTMLInputElement put its activation behavior on run_activation_behavior, which wasn't actually called anywhere and had to be manually called by other places. We now use activation_behavior which is used by EventDispatcher. This commit also brings HTMLInputElement closer to spec by removing the did_foo functions that did ad-hoc event dispatching and unifies the behavior under run_input_activation_behavior.
This commit is contained in:
parent
06ccc45157
commit
29583104d2
Notes:
sideshowbarker
2024-07-17 17:20:49 +09:00
Author: https://github.com/sin-ack
Commit: 29583104d2
Pull-request: https://github.com/SerenityOS/serenity/pull/13052
Reviewed-by: https://github.com/Lubrsi
Reviewed-by: https://github.com/awesomekling
Reviewed-by: https://github.com/linusg ✅
Reviewed-by: https://github.com/trflynn89
26 changed files with 409 additions and 466 deletions
|
@ -23,33 +23,20 @@ namespace Web::HTML {
|
|||
HTMLInputElement::HTMLInputElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
||||
: FormAssociatedElement(document, move(qualified_name))
|
||||
{
|
||||
activation_behavior = [this](auto&) {
|
||||
// The activation behavior for input elements are these steps:
|
||||
|
||||
// FIXME: 1. If this element is not mutable and is not in the Checkbox state and is not in the Radio state, then return.
|
||||
|
||||
// 2. Run this element's input activation behavior, if any, and do nothing otherwise.
|
||||
run_input_activation_behavior();
|
||||
};
|
||||
}
|
||||
|
||||
HTMLInputElement::~HTMLInputElement()
|
||||
{
|
||||
}
|
||||
|
||||
void HTMLInputElement::did_click_button(Badge<Painting::ButtonPaintable>)
|
||||
{
|
||||
// FIXME: This should be a PointerEvent.
|
||||
dispatch_event(DOM::Event::create(EventNames::click));
|
||||
|
||||
if (type() == "submit") {
|
||||
if (auto* form = first_ancestor_of_type<HTMLFormElement>()) {
|
||||
form->submit_form(this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void HTMLInputElement::did_click_checkbox(Badge<Painting::CheckBoxPaintable>)
|
||||
{
|
||||
// FIXME: This should be a PointerEvent.
|
||||
auto click_event = DOM::Event::create(EventNames::click);
|
||||
click_event->set_bubbles(true);
|
||||
dispatch_event(move(click_event));
|
||||
}
|
||||
|
||||
RefPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
|
||||
{
|
||||
if (type() == "hidden")
|
||||
|
@ -69,7 +56,7 @@ RefPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::Sty
|
|||
return layout_node;
|
||||
}
|
||||
|
||||
void HTMLInputElement::set_checked(bool checked, ChangeSource change_source, ShouldRunActivationBehavior should_run_activation_behavior)
|
||||
void HTMLInputElement::set_checked(bool checked, ChangeSource change_source)
|
||||
{
|
||||
if (m_checked == checked)
|
||||
return;
|
||||
|
@ -81,25 +68,12 @@ void HTMLInputElement::set_checked(bool checked, ChangeSource change_source, Sho
|
|||
|
||||
m_checked = checked;
|
||||
set_needs_style_update(true);
|
||||
|
||||
if (should_run_activation_behavior == ShouldRunActivationBehavior::Yes)
|
||||
run_activation_behavior();
|
||||
}
|
||||
|
||||
void HTMLInputElement::run_activation_behavior()
|
||||
{
|
||||
// The activation behavior for input elements are these steps:
|
||||
|
||||
// FIXME: 1. If this element is not mutable and is not in the Checkbox state and is not in the Radio state, then return.
|
||||
|
||||
// 2. Run this element's input activation behavior, if any, and do nothing otherwise.
|
||||
run_input_activation_behavior();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/input.html#input-activation-behavior
|
||||
void HTMLInputElement::run_input_activation_behavior()
|
||||
{
|
||||
if (type() == "checkbox") {
|
||||
if (type() == "checkbox" || type() == "radio") {
|
||||
// 1. If the element is not connected, then return.
|
||||
if (!is_connected())
|
||||
return;
|
||||
|
@ -114,6 +88,18 @@ void HTMLInputElement::run_input_activation_behavior()
|
|||
auto change_event = DOM::Event::create(HTML::EventNames::change);
|
||||
change_event->set_bubbles(true);
|
||||
dispatch_event(move(change_event));
|
||||
} else if (type() == "submit") {
|
||||
RefPtr<HTMLFormElement> form;
|
||||
// 1. If the element does not have a form owner, then return.
|
||||
if (!(form = this->form()))
|
||||
return;
|
||||
|
||||
// 2. If the element's node document is not fully active, then return.
|
||||
if (!document().is_fully_active())
|
||||
return;
|
||||
|
||||
// 3. Submit the form owner from the element.
|
||||
form->submit_form(this);
|
||||
} else {
|
||||
dispatch_event(DOM::Event::create(EventNames::change));
|
||||
}
|
||||
|
@ -208,7 +194,7 @@ void HTMLInputElement::parse_attribute(FlyString const& name, String const& valu
|
|||
// When the checked content attribute is added, if the control does not have dirty checkedness,
|
||||
// the user agent must set the checkedness of the element to true
|
||||
if (!m_dirty_checkedness)
|
||||
set_checked(true, ChangeSource::Programmatic, ShouldRunActivationBehavior::No);
|
||||
set_checked(true, ChangeSource::Programmatic);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -219,7 +205,7 @@ void HTMLInputElement::did_remove_attribute(FlyString const& name)
|
|||
// When the checked content attribute is removed, if the control does not have dirty checkedness,
|
||||
// the user agent must set the checkedness of the element to false.
|
||||
if (!m_dirty_checkedness)
|
||||
set_checked(false, ChangeSource::Programmatic, ShouldRunActivationBehavior::No);
|
||||
set_checked(false, ChangeSource::Programmatic);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -300,4 +286,93 @@ void HTMLInputElement::inserted()
|
|||
create_shadow_tree_if_needed();
|
||||
}
|
||||
|
||||
void HTMLInputElement::set_checked_within_group()
|
||||
{
|
||||
if (checked())
|
||||
return;
|
||||
|
||||
set_checked(true, ChangeSource::User);
|
||||
String name = this->name();
|
||||
|
||||
document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
|
||||
if (element.checked() && &element != this && element.name() == name)
|
||||
element.set_checked(false, ChangeSource::User);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
|
||||
void HTMLInputElement::legacy_pre_activation_behavior()
|
||||
{
|
||||
m_before_legacy_pre_activation_behavior_checked = checked();
|
||||
|
||||
// 1. If this element's type attribute is in the Checkbox state, then set
|
||||
// this element's checkedness to its opposite value (i.e. true if it is
|
||||
// false, false if it is true) and set this element's indeterminate IDL
|
||||
// attribute to false.
|
||||
// FIXME: Set indeterminate to false when that exists.
|
||||
if (type_state() == TypeAttributeState::Checkbox) {
|
||||
set_checked(!checked(), ChangeSource::User);
|
||||
}
|
||||
|
||||
// 2. If this element's type attribute is in the Radio Button state, then
|
||||
// get a reference to the element in this element's radio button group that
|
||||
// has its checkedness set to true, if any, and then set this element's
|
||||
// checkedness to true.
|
||||
if (type_state() == TypeAttributeState::RadioButton) {
|
||||
String name = this->name();
|
||||
|
||||
document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
|
||||
if (element.checked() && element.name() == name) {
|
||||
m_legacy_pre_activation_behavior_checked_element_in_group = element;
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
set_checked_within_group();
|
||||
}
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-canceled-activation-behavior
|
||||
void HTMLInputElement::legacy_cancelled_activation_behavior()
|
||||
{
|
||||
// 1. If the element's type attribute is in the Checkbox state, then set the
|
||||
// element's checkedness and the element's indeterminate IDL attribute back
|
||||
// to the values they had before the legacy-pre-activation behavior was run.
|
||||
if (type_state() == TypeAttributeState::Checkbox) {
|
||||
set_checked(m_before_legacy_pre_activation_behavior_checked, ChangeSource::Programmatic);
|
||||
}
|
||||
|
||||
// 2. If this element 's type attribute is in the Radio Button state, then
|
||||
// if the element to which a reference was obtained in the
|
||||
// legacy-pre-activation behavior, if any, is still in what is now this
|
||||
// element' s radio button group, if it still has one, and if so, setting
|
||||
// that element 's checkedness to true; or else, if there was no such
|
||||
// element, or that element is no longer in this element' s radio button
|
||||
// group, or if this element no longer has a radio button group, setting
|
||||
// this element's checkedness to false.
|
||||
if (type_state() == TypeAttributeState::RadioButton) {
|
||||
String name = this->name();
|
||||
bool did_reselect_previous_element = false;
|
||||
if (m_legacy_pre_activation_behavior_checked_element_in_group) {
|
||||
auto& element_in_group = *m_legacy_pre_activation_behavior_checked_element_in_group;
|
||||
if (name == element_in_group.name()) {
|
||||
element_in_group.set_checked_within_group();
|
||||
did_reselect_previous_element = true;
|
||||
}
|
||||
|
||||
m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
|
||||
}
|
||||
|
||||
if (!did_reselect_previous_element)
|
||||
set_checked(false, ChangeSource::User);
|
||||
}
|
||||
}
|
||||
|
||||
void HTMLInputElement::legacy_cancelled_activation_behavior_was_not_called()
|
||||
{
|
||||
m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue