Skip to content

Truly independent web browser

LibWeb: Give trusted pointermove events a coalesced event

Andreas Kling9f48dd72062e
diff --git a/Libraries/LibWeb/DOM/EventDispatcher.cpp b/Libraries/LibWeb/DOM/EventDispatcher.cpp
index 987ad3a2a0b..853fd113301 100644
--- a/Libraries/LibWeb/DOM/EventDispatcher.cpp
+++ b/Libraries/LibWeb/DOM/EventDispatcher.cpp
@@ -34,6 +34,7 @@
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/MouseEvent.h>
+#include <LibWeb/UIEvents/PointerEvent.h>
#include <LibWeb/UIEvents/TouchEvent.h>
namespace Web::DOM {
@@ -172,6 +173,12 @@ void EventDispatcher::invoke(Event::PathEntry& struct_, Event& event, Event::Pha
// that is either struct or preceding struct, whose shadow-adjusted target is non-null.
event.set_target(last_valid_shadow_adjusted_target.value().shadow_adjusted_target.ptr());
+ // AD-HOC: Coalesced events have the same target as the trusted pointer event they were coalesced into.
+ if (event.is_trusted()) {
+ if (auto* pointer_event = as_if<UIEvents::PointerEvent>(event))
+ pointer_event->set_target_of_coalesced_events(event.target());
+ }
+
// 2. Set event’s relatedTarget to struct’s relatedTarget.
event.set_related_target(struct_.related_target.ptr());
@@ -598,6 +605,13 @@ bool EventDispatcher::dispatch(GC::Ref<EventTarget> target, Event& event, bool l
event.clear_touch_target_list();
}
+ // AD-HOC: The coalesced events of a trusted pointer event keep its final target, also when script dispatches the
+ // event again.
+ if (event.is_trusted()) {
+ if (auto* pointer_event = as_if<UIEvents::PointerEvent>(event))
+ pointer_event->set_target_of_coalesced_events(event.target());
+ }
+
// 12. If activationTarget is non-null, then:
if (activation_target) {
// 1. If event’s canceled flag is unset, then run activationTarget’s activation behavior with event.
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index d2e06637125..ba1ed3c140e 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -4936,8 +4936,7 @@ GC::Ref<UIEvents::PointerEvent> EventHandler::create_pointer_event(PointerState&
options.bubbles = true;
options.cancelable = true;
options.composed = true;
- auto event = UIEvents::PointerEvent::create(name, move(options), coordinates.page_offset.x().to_double(), coordinates.page_offset.y().to_double(), coordinates.offset.x().to_double(), coordinates.offset.y().to_double(), HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(target)));
- event->set_is_trusted(true);
+ auto event = UIEvents::PointerEvent::create_trusted(name, move(options), coordinates.page_offset.x().to_double(), coordinates.page_offset.y().to_double(), coordinates.offset.x().to_double(), coordinates.offset.y().to_double(), HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(target)));
pointer.last_event = PointerEventSnapshot {
.position = { coordinates, screen_position },
.properties = pointer.properties,
diff --git a/Libraries/LibWeb/UIEvents/PointerEvent.cpp b/Libraries/LibWeb/UIEvents/PointerEvent.cpp
index 021901a844c..9c0cf335a0a 100644
--- a/Libraries/LibWeb/UIEvents/PointerEvent.cpp
+++ b/Libraries/LibWeb/UIEvents/PointerEvent.cpp
@@ -106,12 +106,10 @@ WebIDL::ExceptionOr<GC::Ref<PointerEvent>> PointerEvent::create_from_platform_ev
}
event_options.button = mouse_button_to_button_code(static_cast<MouseButton>(button));
event_options.buttons = buttons;
- auto event = PointerEvent::create(event_name, move(event_options), page.x().to_double(), page.y().to_double(), offset.x().to_double(), offset.y().to_double(), HighResolutionTime::current_high_resolution_time(relevant_global_object));
- event->set_is_trusted(true);
- event->set_bubbles(true);
- event->set_cancelable(true);
- event->set_composed(true);
- return event;
+ event_options.bubbles = true;
+ event_options.cancelable = true;
+ event_options.composed = true;
+ return create_trusted(event_name, move(event_options), page.x().to_double(), page.y().to_double(), offset.x().to_double(), offset.y().to_double(), HighResolutionTime::current_high_resolution_time(relevant_global_object));
}
PointerEvent::PointerEvent(FlyString const& type, PointerEventOptions&& options, double page_x, double page_y, double offset_x, double offset_y, HighResolutionTime::DOMHighResTimeStamp time_stamp)
@@ -159,8 +157,37 @@ GC::Ref<PointerEvent> PointerEvent::create(Utf16FlyString const& type, PointerEv
return GC::Heap::the().allocate<PointerEvent>(type, move(options), page_x, page_y, offset_x, offset_y, time_stamp);
}
+GC::Ref<PointerEvent> PointerEvent::create_trusted(Utf16FlyString const& type, PointerEventOptions&& options, double page_x, double page_y, double offset_x, double offset_y, HighResolutionTime::DOMHighResTimeStamp time_stamp)
+{
+ // https://w3c.github.io/pointerevents/#coalesced-events
+ // For trusted pointermove and pointerrawupdate events, the list is a sequence of all PointerEvents that were
+ // coalesced into this event. [...] As a result, the coalesced events lists for these events always contain at
+ // least one event. For all other trusted event types, it is an empty list.
+ // NB: We do not coalesce platform events, so the only coalesced event is a copy of the dispatched event itself.
+ if (type == EventNames::pointermove || type == EventNames::pointerrawupdate) {
+ PointerEventOptions coalesced_options = options;
+ coalesced_options.bubbles = false;
+ coalesced_options.cancelable = false;
+ coalesced_options.coalesced_events.clear();
+ coalesced_options.predicted_events.clear();
+ auto coalesced_event = create(type, move(coalesced_options), page_x, page_y, offset_x, offset_y, time_stamp);
+ coalesced_event->set_is_trusted(true);
+ options.coalesced_events.append(coalesced_event);
+ }
+
+ auto event = create(type, move(options), page_x, page_y, offset_x, offset_y, time_stamp);
+ event->set_is_trusted(true);
+ return event;
+}
+
PointerEvent::~PointerEvent() = default;
+void PointerEvent::set_target_of_coalesced_events(GC::Ptr<DOM::EventTarget> target)
+{
+ for (auto& coalesced_event : m_coalesced_events)
+ coalesced_event->set_target(target.ptr());
+}
+
void PointerEvent::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
diff --git a/Libraries/LibWeb/UIEvents/PointerEvent.h b/Libraries/LibWeb/UIEvents/PointerEvent.h
index d0777f1e49d..cdab8d3d026 100644
--- a/Libraries/LibWeb/UIEvents/PointerEvent.h
+++ b/Libraries/LibWeb/UIEvents/PointerEvent.h
@@ -64,6 +64,10 @@ public:
Utf16FlyString const& type, PointerEventOptions&&,
double page_x, double page_y, double offset_x, double offset_y,
HighResolutionTime::DOMHighResTimeStamp);
+ [[nodiscard]] static GC::Ref<PointerEvent> create_trusted(
+ Utf16FlyString const& type, PointerEventOptions&&,
+ double page_x, double page_y, double offset_x, double offset_y,
+ HighResolutionTime::DOMHighResTimeStamp);
[[nodiscard]] static WebIDL::ExceptionOr<GC::Ref<PointerEvent>> create_for_constructor(FlyString const&, Bindings::PointerEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
[[nodiscard]] static WebIDL::ExceptionOr<GC::Ref<PointerEvent>> create_for_constructor(Utf16String const&, Bindings::PointerEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
[[nodiscard]] static WebIDL::ExceptionOr<GC::Ref<PointerEvent>> create_from_platform_event(JS::Object const& relevant_global_object, GC::Ptr<HTML::WindowProxy>, FlyString const& event_name, CSSPixelPoint screen, CSSPixelPoint page, CSSPixelPoint client, CSSPixelPoint offset, Optional<CSSPixelPoint> movement, unsigned button, unsigned buttons, unsigned modifiers, int detail = 0);
@@ -103,6 +107,7 @@ public:
bool is_primary() const { return m_is_primary; }
WebIDL::Long persistent_device_id() const { return m_persistent_device_id; }
AK::ReadonlySpan<GC::Ref<PointerEvent>> get_coalesced_events() const { return m_coalesced_events; }
+ void set_target_of_coalesced_events(GC::Ptr<DOM::EventTarget>);
AK::ReadonlySpan<GC::Ref<PointerEvent>> get_predicted_events() const { return m_predicted_events; }
// https://w3c.github.io/pointerevents/#dom-pointerevent-pressure
diff --git a/Tests/LibWeb/Text/expected/UIEvents/pointermove-coalesced-events.txt b/Tests/LibWeb/Text/expected/UIEvents/pointermove-coalesced-events.txt
index b1a26191831..94d3c725889 100644
--- a/Tests/LibWeb/Text/expected/UIEvents/pointermove-coalesced-events.txt
+++ b/Tests/LibWeb/Text/expected/UIEvents/pointermove-coalesced-events.txt
@@ -1,7 +1,19 @@
pointerdown: 0 coalesced event(s)
-pointermove: 0 coalesced event(s)
-pointermove: 0 coalesced event(s)
+pointermove: 1 coalesced event(s)
+ type=pointermove clientX=20 clientY=30 buttons=1
+ isTrusted=true bubbles=false cancelable=false
+ same target=true same pointerId=true same pointerType=true same isPrimary=true
+ own coalesced=0 own predicted=0
+pointermove: 1 coalesced event(s)
+ type=pointermove clientX=40 clientY=50 buttons=1
+ isTrusted=true bubbles=false cancelable=false
+ same target=true same pointerId=true same pointerType=true same isPrimary=true
+ own coalesced=0 own predicted=0
pointerup: 0 coalesced event(s)
redispatched: isTrusted=false same target=true
-redispatched unread: 0 coalesced event(s)
-host listener: no coalesced event kept
+ coalesced isTrusted=true target is original=true
+redispatched unread: 1 coalesced event(s)
+ coalesced target is original=true
+shadow tree listener: coalesced target is inner=true
+host listener: kept coalesced target is host=true
+after dispatch: kept coalesced target is host=true