ladybird/Libraries/LibWeb/Painting/TextPaintable.cpp
Aliaksandr Kalenik c0e90a2a68 LibWeb: Dispatch "click" event on input control associated with <label>
For example, in the following HTML:
```html
<label>
    <input type="radio" name="fruit" value="apple" id="radio1">
    <span class="box"></span>
</label>
```

When any descendant of a <label> element is clicked, a "click" event
must be dispatched on the <input> element nested within the <label>, in
addition to the "click" event dispatched on the clicked descendant.

Previously, this behavior was implemented only for text node descendants
by "overriding" the mouse event target using `mouse_event_target()` in
the TextPaintable. This approach was incorrect because it was limited to
text nodes, whereas the behavior should apply to any box. Moreover, the
"click" event for the input control must be dispatched *in addition* to
the event on the clicked element, rather than redirecting it.
2024-11-21 16:11:03 +01:00

63 lines
2.3 KiB
C++

/*
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/Layout/Label.h>
#include <LibWeb/Layout/LabelableNode.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Painting/TextPaintable.h>
namespace Web::Painting {
GC_DEFINE_ALLOCATOR(TextPaintable);
GC::Ref<TextPaintable> TextPaintable::create(Layout::TextNode const& layout_node, String const& text_for_rendering)
{
return layout_node.heap().allocate<TextPaintable>(layout_node, text_for_rendering);
}
TextPaintable::TextPaintable(Layout::TextNode const& layout_node, String const& text_for_rendering)
: Paintable(layout_node)
, m_text_for_rendering(text_for_rendering)
{
}
bool TextPaintable::wants_mouse_events() const
{
return layout_node().first_ancestor_of_type<Layout::Label>();
}
TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousedown(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
{
auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
if (!label)
return DispatchEventOfSameName::No;
const_cast<Layout::Label*>(label)->handle_mousedown_on_label({}, position, button);
const_cast<HTML::Navigable&>(*navigable()).event_handler().set_mouse_event_tracking_paintable(this);
return DispatchEventOfSameName::Yes;
}
TextPaintable::DispatchEventOfSameName TextPaintable::handle_mouseup(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
{
auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
if (!label)
return DispatchEventOfSameName::No;
const_cast<Layout::Label*>(label)->handle_mouseup_on_label({}, position, button);
const_cast<HTML::Navigable&>(*navigable()).event_handler().set_mouse_event_tracking_paintable(nullptr);
return DispatchEventOfSameName::Yes;
}
TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousemove(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
{
auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
if (!label)
return DispatchEventOfSameName::No;
const_cast<Layout::Label*>(label)->handle_mousemove_on_label({}, position, button);
return DispatchEventOfSameName::Yes;
}
}