Skip to content

Commit

Permalink
LibWeb: Move code for finding the parent element into a helper function
Browse files Browse the repository at this point in the history
This exact same loop is repeated a couple of times.
  • Loading branch information
gunnarbeutner authored and AtkinsSJ committed Nov 7, 2022
1 parent 0586730 commit e7a7895
Showing 1 changed file with 20 additions and 31 deletions.
51 changes: 20 additions & 31 deletions Userland/Libraries/LibWeb/Page/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ static JS::GCPtr<DOM::Node> dom_node_for_event_dispatch(Painting::Paintable cons
return nullptr;
}

static bool parent_element_for_event_dispatch(Painting::Paintable const& paintable, JS::GCPtr<DOM::Node>& node, Layout::Node const*& layout_node)
{
layout_node = &paintable.layout_node();
while (layout_node && node && !node->is_element() && layout_node->parent()) {
layout_node = layout_node->parent();
node = layout_node->dom_node();
}
return node && layout_node;
}

static Gfx::StandardCursor cursor_css_to_gfx(Optional<CSS::Cursor> cursor)
{
if (!cursor.has_value()) {
Expand Down Expand Up @@ -167,14 +177,9 @@ bool EventHandler::handle_mousewheel(Gfx::IntPoint const& position, unsigned but
}

// Search for the first parent of the hit target that's an element.
auto* layout_node = &paintable->layout_node();
while (layout_node && node && !node->is_element() && layout_node->parent()) {
layout_node = layout_node->parent();
node = layout_node->dom_node();
}
if (!node || !layout_node) {
Layout::Node const* layout_node;
if (!parent_element_for_event_dispatch(*paintable, node, layout_node))
return false;
}

auto offset = compute_mouse_event_offset(position, *layout_node);
if (node->dispatch_event(*UIEvents::WheelEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::wheel, offset.x(), offset.y(), position.x(), position.y(), wheel_delta_x, wheel_delta_y, buttons, button))) {
Expand Down Expand Up @@ -232,12 +237,8 @@ bool EventHandler::handle_mouseup(Gfx::IntPoint const& position, unsigned button
// Search for the first parent of the hit target that's an element.
// "The click event type MUST be dispatched on the topmost event target indicated by the pointer." (https://www.w3.org/TR/uievents/#event-type-click)
// "The topmost event target MUST be the element highest in the rendering order which is capable of being an event target." (https://www.w3.org/TR/uievents/#topmost-event-target)
auto* layout_node = &paintable->layout_node();
while (layout_node && node && !node->is_element() && layout_node->parent()) {
layout_node = layout_node->parent();
node = layout_node->dom_node();
}
if (!node || !layout_node) {
Layout::Node const* layout_node;
if (!parent_element_for_event_dispatch(*paintable, node, layout_node)) {
// FIXME: This is pretty ugly but we need to bail out here.
goto after_node_use;
}
Expand Down Expand Up @@ -358,12 +359,8 @@ bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned butt
// Search for the first parent of the hit target that's an element.
// "The click event type MUST be dispatched on the topmost event target indicated by the pointer." (https://www.w3.org/TR/uievents/#event-type-click)
// "The topmost event target MUST be the element highest in the rendering order which is capable of being an event target." (https://www.w3.org/TR/uievents/#topmost-event-target)
auto* layout_node = &paintable->layout_node();
while (layout_node && node && !node->is_element() && layout_node->parent()) {
layout_node = layout_node->parent();
node = layout_node->dom_node();
}
if (!node || !layout_node)
Layout::Node const* layout_node;
if (!parent_element_for_event_dispatch(*paintable, node, layout_node))
return false;

m_mousedown_target = node.ptr();
Expand Down Expand Up @@ -475,12 +472,8 @@ bool EventHandler::handle_mousemove(Gfx::IntPoint const& position, unsigned butt
// Search for the first parent of the hit target that's an element.
// "The click event type MUST be dispatched on the topmost event target indicated by the pointer." (https://www.w3.org/TR/uievents/#event-type-click)
// "The topmost event target MUST be the element highest in the rendering order which is capable of being an event target." (https://www.w3.org/TR/uievents/#topmost-event-target)
auto* layout_node = &paintable->layout_node();
while (layout_node && node && !node->is_element() && layout_node->parent()) {
layout_node = layout_node->parent();
node = layout_node->dom_node();
}
if (!node || !layout_node) {
Layout::Node const* layout_node;
if (!parent_element_for_event_dispatch(*paintable, node, layout_node)) {
// FIXME: This is pretty ugly but we need to bail out here.
goto after_node_use;
}
Expand Down Expand Up @@ -563,12 +556,8 @@ bool EventHandler::handle_doubleclick(Gfx::IntPoint const& position, unsigned bu

// Search for the first parent of the hit target that's an element.
// "The topmost event target MUST be the element highest in the rendering order which is capable of being an event target." (https://www.w3.org/TR/uievents/#topmost-event-target)
auto* layout_node = &paintable->layout_node();
while (layout_node && node && !node->is_element() && layout_node->parent()) {
layout_node = layout_node->parent();
node = layout_node->dom_node();
}
if (!node || !layout_node)
Layout::Node const* layout_node;
if (!parent_element_for_event_dispatch(*paintable, node, layout_node))
return false;

auto offset = compute_mouse_event_offset(position, *layout_node);
Expand Down

0 comments on commit e7a7895

Please sign in to comment.