Skip to content

Truly independent web browser

LibWeb: Implement pointer capture

Aliaksandr Kalenike50e9290f525
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index f8acd5439ca..059c2fd47d7 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -161,6 +161,7 @@ enum class InvalidateLayoutTreeReason {
X(EventHandlerHandleMouseMove, false) \
X(EventHandlerHandleMouseUp, false) \
X(EventHandlerHandleMouseWheel, false) \
+ X(EventHandlerPointerCaptureEvents, false) \
X(EventHandlerRunActivationBehavior, false) \
X(EventHandlerShowContextMenu, false) \
X(FontFaceSetReady, false) \
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index e7be77b746e..91751873de0 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -156,6 +156,7 @@
#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
#include <LibWeb/UIEvents/MouseEvent.h>
+#include <LibWeb/UIEvents/PointerEvent.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/DOMException.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
@@ -6788,23 +6789,74 @@ Optional<Gfx::DecodedImageFrame> Element::capture_the_image()
return Gfx::DecodedImageFrame { *bitmap };
}
-void Element::set_pointer_capture(WebIDL::Long pointer_id)
+// https://w3c.github.io/pointerevents/#dfn-active-pointer
+// A mouse connected to the device is always active, and it is the one pointer there is.
+static bool pointer_id_names_an_active_pointer(WebIDL::Long pointer_id)
{
- (void)pointer_id;
- dbgln("FIXME: Implement Element::setPointerCapture()");
+ return pointer_id == UIEvents::mouse_pointer_id;
}
-void Element::release_pointer_capture(WebIDL::Long pointer_id)
+// https://w3c.github.io/pointerevents/#setting-pointer-capture
+WebIDL::ExceptionOr<void> Element::set_pointer_capture(WebIDL::Long pointer_id)
{
- (void)pointer_id;
- dbgln("FIXME: Implement Element::releasePointerCapture()");
+ // 1. If the pointerId provided as the method's argument does not match any of the active pointers, then throw a
+ // "NotFoundError" DOMException.
+ if (!pointer_id_names_an_active_pointer(pointer_id))
+ return WebIDL::NotFoundError::create("No active pointer has the given pointerId"_utf16);
+
+ // 2. Let the pointer be the active pointer specified by the given pointerId.
+ // 3. If the element is not connected, throw an "InvalidStateError" DOMException.
+ if (!is_connected())
+ return WebIDL::InvalidStateError::create("Cannot set pointer capture on an element that is not connected"_utf16);
+
+ // FIXME: 4. If this method is invoked while the element's node document has a locked element, throw an
+ // "InvalidStateError" DOMException.
+
+ // 5. If the pointer is not in the active buttons state or the element's node document is not the active document
+ // of the pointer, then terminate these steps.
+ auto navigable = document().navigable();
+ if (!navigable)
+ return {};
+ auto& event_handler = navigable->event_handler();
+ if (!event_handler.is_mouse_pointer_in_active_buttons_state())
+ return {};
+
+ // 6. For the specified pointerId, set the pending pointer capture target override to the Element on which this
+ // method was invoked.
+ event_handler.set_pending_pointer_capture_target_override({}, *this);
+ return {};
}
-bool Element::has_pointer_capture(WebIDL::Long pointer_id)
+// https://w3c.github.io/pointerevents/#releasing-pointer-capture
+WebIDL::ExceptionOr<void> Element::release_pointer_capture(WebIDL::Long pointer_id)
{
- (void)pointer_id;
- dbgln("FIXME: Implement Element::hasPointerCapture()");
- return false;
+ // 1. If the pointerId provided as the method's argument does not match any of the active pointers and these steps
+ // are not being invoked as a result of the implicit release of pointer capture, then throw a "NotFoundError"
+ // DOMException.
+ if (!pointer_id_names_an_active_pointer(pointer_id))
+ return WebIDL::NotFoundError::create("No active pointer has the given pointerId"_utf16);
+
+ // 2. If hasPointerCapture is false for the Element with the specified pointerId, then terminate these steps.
+ if (!has_pointer_capture(pointer_id))
+ return {};
+
+ // 3. For the specified pointerId, clear the pending pointer capture target override, if set.
+ document().navigable()->event_handler().clear_pending_pointer_capture_target_override({});
+ return {};
+}
+
+// https://w3c.github.io/pointerevents/#dom-element-haspointercapture
+bool Element::has_pointer_capture(WebIDL::Long pointer_id) const
+{
+ // Indicates whether the element on which this method is invoked has pointer capture for the pointer identified by
+ // the argument pointerId. In particular, returns true if the pending pointer capture target override for pointerId
+ // is set to the element on which this method is invoked, and false otherwise.
+ if (!pointer_id_names_an_active_pointer(pointer_id))
+ return false;
+ auto navigable = document().navigable();
+ if (!navigable)
+ return false;
+ return navigable->event_handler().pending_pointer_capture_target_override().ptr() == this;
}
GC::Ptr<NamedNodeMap> Element::attributes()
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index d2e7bcc48db..3d19c8dd268 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -891,9 +891,9 @@ public:
// https://drafts.csswg.org/css-view-transitions-1/#capture-the-image
Optional<Gfx::DecodedImageFrame> capture_the_image();
- void set_pointer_capture(WebIDL::Long pointer_id);
- void release_pointer_capture(WebIDL::Long pointer_id);
- bool has_pointer_capture(WebIDL::Long pointer_id);
+ WebIDL::ExceptionOr<void> set_pointer_capture(WebIDL::Long pointer_id);
+ WebIDL::ExceptionOr<void> release_pointer_capture(WebIDL::Long pointer_id);
+ bool has_pointer_capture(WebIDL::Long pointer_id) const;
virtual bool contributes_a_script_blocking_style_sheet() const { return false; }
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 0ff3a33fa61..fecc208d993 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -395,7 +395,7 @@ EventResult EventHandler::handle_mousedown(CSSPixelPoint visual_viewport_positio
m_mousedown_click_count = click_count;
- if (!scrollbar_dragged_by_compositor.has_value()) {
+ if (!scrollbar_dragged_by_compositor.has_value() && !next_pointer_event_is_captured()) {
auto dispath_result = dispatch_event_to_nested_navigable(*this, *target_layout_node, node, visual_viewport_position, remote_target, [=](EventHandler& event_handler, CSSPixelPoint position) -> EventResult {
return event_handler.handle_mousedown(position, screen_position, button, buttons, modifiers, click_count, {}, remote_target);
});
@@ -424,7 +424,7 @@ EventResult EventHandler::handle_mousedown(CSSPixelPoint visual_viewport_positio
m_mousedown_visual_viewport_position = visual_viewport_position;
auto coordinates = compute_mouse_event_coordinates(visual_viewport_position, viewport_position, *layout_node);
- auto dispatch_result = dispatch_a_pointer_event_for_a_device_that_supports_hover(PointerEventType::PointerDown, *node, chrome_widget, coordinates, screen_position, {}, button, buttons, modifiers, click_count);
+ auto dispatch_result = dispatch_a_pointer_event_for_a_device_that_supports_hover(read, PointerEventType::PointerDown, *node, chrome_widget, coordinates, screen_position, {}, button, buttons, modifiers, click_count).result;
bool is_context_menu_trigger = button == UIEvents::MouseButton::Secondary;
#if defined(AK_OS_MACOS)
@@ -617,7 +617,7 @@ EventResult EventHandler::handle_mousemove(CSSPixelPoint visual_viewport_positio
return EventResult::Dropped;
// A move the lanes handed to a same-process iframe goes to it, whose lanes hovered what is under the pointer there.
- if (!goes_to_lane_hover_target || Painting::is_navigable_container_viewport_paintable(*target_layout_node)) {
+ if ((!goes_to_lane_hover_target || Painting::is_navigable_container_viewport_paintable(*target_layout_node)) && !next_pointer_event_is_captured()) {
auto nested_navigable = local_nested_navigable_at(*target_layout_node, node);
auto dispath_result = dispatch_event_to_nested_navigable(*this, *target_layout_node, node, visual_viewport_position, remote_target, [&](EventHandler& event_handler, CSSPixelPoint position) {
return event_handler.handle_mousemove(position, screen_position, buttons, modifiers, remote_target);
@@ -651,7 +651,7 @@ EventResult EventHandler::handle_mousemove(CSSPixelPoint visual_viewport_positio
auto movement = compute_mouse_event_movement(screen_position);
m_mousemove_previous_screen_position = screen_position;
- if (dispatch_a_pointer_event_for_a_device_that_supports_hover(PointerEventType::PointerMove, *node, chrome_widget, coordinates, screen_position, movement, UIEvents::MouseButton::Primary, buttons, modifiers) != PointerEventDispatchResult::RunDefaultActions)
+ if (dispatch_a_pointer_event_for_a_device_that_supports_hover(read, PointerEventType::PointerMove, *node, chrome_widget, coordinates, screen_position, movement, UIEvents::MouseButton::Primary, buttons, modifiers).result != PointerEventDispatchResult::RunDefaultActions)
return EventResult::Cancelled;
}
} else if (m_mousedown_target) {
@@ -662,7 +662,7 @@ EventResult EventHandler::handle_mousemove(CSSPixelPoint visual_viewport_positio
auto movement = compute_mouse_event_movement(screen_position);
m_mousemove_previous_screen_position = screen_position;
- if (dispatch_a_pointer_event_for_a_device_that_supports_hover(PointerEventType::PointerMove, document->html_element(), nullptr, coordinates, screen_position, movement, UIEvents::MouseButton::Primary, buttons, modifiers) != PointerEventDispatchResult::RunDefaultActions)
+ if (dispatch_a_pointer_event_for_a_device_that_supports_hover(read, PointerEventType::PointerMove, document->html_element(), nullptr, coordinates, screen_position, movement, UIEvents::MouseButton::Primary, buttons, modifiers).result != PointerEventDispatchResult::RunDefaultActions)
return EventResult::Cancelled;
}
@@ -763,7 +763,7 @@ EventResult EventHandler::handle_mouseup(CSSPixelPoint visual_viewport_position,
// chance to end a drag that went outside the window.
if (m_mousedown_target) {
auto coordinates = compute_mouse_event_coordinates(visual_viewport_position, viewport_position, *document->layout_node(read));
- dispatch_a_pointer_event_for_a_device_that_supports_hover(PointerEventType::PointerUp, document->html_element(), nullptr, coordinates, screen_position, {}, button, buttons, modifiers, click_count);
+ dispatch_a_pointer_event_for_a_device_that_supports_hover(read, PointerEventType::PointerUp, document->html_element(), nullptr, coordinates, screen_position, {}, button, buttons, modifiers, click_count);
return EventResult::Handled;
}
@@ -777,11 +777,13 @@ EventResult EventHandler::handle_mouseup(CSSPixelPoint visual_viewport_position,
if (!node)
return EventResult::Dropped;
- auto dispath_result = dispatch_event_to_nested_navigable(*this, *target_layout_node, node, visual_viewport_position, remote_target, [&](EventHandler& event_handler, CSSPixelPoint position) {
- return event_handler.handle_mouseup(position, screen_position, button, buttons, modifiers, remote_target);
- });
- if (dispath_result.has_value())
- return *dispath_result;
+ if (!next_pointer_event_is_captured()) {
+ auto dispath_result = dispatch_event_to_nested_navigable(*this, *target_layout_node, node, visual_viewport_position, remote_target, [&](EventHandler& event_handler, CSSPixelPoint position) {
+ return event_handler.handle_mouseup(position, screen_position, button, buttons, modifiers, remote_target);
+ });
+ if (dispath_result.has_value())
+ return *dispath_result;
+ }
// NB: Search for the first parent of the hit target that's an element.
//
@@ -796,17 +798,20 @@ EventResult EventHandler::handle_mouseup(CSSPixelPoint visual_viewport_position,
return EventResult::Dropped;
auto coordinates = compute_mouse_event_coordinates(visual_viewport_position, viewport_position, *layout_node);
- [[maybe_unused]] auto dispatch_result = dispatch_a_pointer_event_for_a_device_that_supports_hover(PointerEventType::PointerUp, *node, chrome_widget, coordinates, screen_position, {}, button, buttons, modifiers, click_count);
+ auto dispatched_pointer_up = dispatch_a_pointer_event_for_a_device_that_supports_hover(read, PointerEventType::PointerUp, *node, chrome_widget, coordinates, screen_position, {}, button, buttons, modifiers, click_count);
#if defined(AK_OS_MACOS)
// INTEROP: Blink, WebKit, and Gecko defer changing a selection clicked for a possible drag until an uncancelled
// mouseup. Blink and WebKit clear non-editable selections, while Gecko collapses them to a caret.
- if (m_mousedown_preserved_selection && dispatch_result == PointerEventDispatchResult::RunDefaultActions)
+ if (m_mousedown_preserved_selection && dispatched_pointer_up.result == PointerEventDispatchResult::RunDefaultActions)
finish_selection_from_preserved_mousedown(*document, visual_viewport_position);
#endif
+ // https://w3c.github.io/pointerevents/#the-click-auxclick-and-contextmenu-events
+ // The target of click and auxclick events is the pointer capture target override of the pointerup event, if the
+ // pointer was captured as it fired, even though lostpointercapture has fired by now.
// NB: If the middle button was used to scroll, suppress click and activation behavior.
- auto click_target = target_for_click_event(m_mousedown_target, *node);
+ auto click_target = dispatched_pointer_up.pointer_capture_target ? dispatched_pointer_up.pointer_capture_target : target_for_click_event(m_mousedown_target, *node);
if (click_target && !middle_button_autoscrolled) {
// NB: Mouseup listeners may have invalidated layout. Click offsets are relative to the click target,
// which may differ from the mouseup target.
@@ -1410,7 +1415,7 @@ EventResult EventHandler::handle_mousecancel()
// The values of the following properties of the pointercancel event MUST match the values of the last dispatched
// pointer event with the same pointerId: [...] and the coordinates inherited from Mouse Events.
auto const& last_position = *m_last_dispatched_pointer_event_position;
- dispatch_a_pointer_event_for_a_device_that_supports_hover(PointerEventType::PointerCancel, target, nullptr, last_position.coordinates, last_position.screen_position, {}, UIEvents::MouseButton::Primary, m_last_known_mouse_buttons, m_last_known_mouse_modifiers);
+ dispatch_a_pointer_event_for_a_device_that_supports_hover(read, PointerEventType::PointerCancel, target, nullptr, last_position.coordinates, last_position.screen_position, {}, UIEvents::MouseButton::Primary, m_last_known_mouse_buttons, m_last_known_mouse_modifiers);
// NB: Dispatching an event may have disturbed the world.
if (m_navigable->active_document() != document)
@@ -1423,7 +1428,7 @@ EventResult EventHandler::handle_mousecancel()
track_the_effective_position_of_the_legacy_mouse_pointer(nullptr);
// 4. Implicitly release the pointer capture if the pointer is currently captured.
- // NB: The press is released as this returns.
+ // NB: Firing the pointercancel event released the capture, and the press is released as this returns.
// FIXME: Also end a drag that a chrome widget, such as a scrollbar, has captured.
return EventResult::Handled;
@@ -4183,6 +4188,111 @@ void EventHandler::reset_mouse_input_tracking(Badge<Page>)
clear_mousedown_tracking();
stop_updating_selection();
m_wheel_scroll_latch.clear();
+
+ // The press ended in another document, so no pointerup here releases a capture it set, and the mouse holds no
+ // button that a capture needs.
+ m_buttons_of_last_dispatched_pointer_event = 0;
+ if (auto document = m_navigable->active_document(); document && (m_pending_pointer_capture_target_override.ptr() || m_pointer_capture_target_override.ptr())) {
+ Layout::ForcedReadScope read { *document };
+ release_pointer_capture_implicitly(read);
+ }
+}
+
+void EventHandler::set_pending_pointer_capture_target_override(Badge<DOM::Element>, DOM::Element& element)
+{
+ m_pending_pointer_capture_target_override = element;
+}
+
+void EventHandler::clear_pending_pointer_capture_target_override(Badge<DOM::Element>)
+{
+ m_pending_pointer_capture_target_override = nullptr;
+}
+
+bool EventHandler::next_pointer_event_is_captured() const
+{
+ // Processing the pending pointer capture before the event makes the pending override the override, or clears the
+ // override, and drops a pending override that is no longer connected.
+ auto pending_pointer_capture_target_override = m_pending_pointer_capture_target_override.ptr();
+ return pending_pointer_capture_target_override && pending_pointer_capture_target_override->is_connected();
+}
+
+// https://w3c.github.io/pointerevents/#process-pending-pointer-capture
+EventHandler::PointerCaptureEventsFired EventHandler::process_pending_pointer_capture(Layout::BegunRead const& read)
+{
+ // https://w3c.github.io/pointerevents/#implicit-release-of-pointer-capture
+ // When the pointer capture target override is no longer connected, the pointer capture target override SHOULD be
+ // set to the document. When the pending pointer capture target override is no longer connected, the pending
+ // pointer capture target override node SHOULD be cleared.
+ if (auto pending_pointer_capture_target_override = m_pending_pointer_capture_target_override.ptr(); pending_pointer_capture_target_override && !pending_pointer_capture_target_override->is_connected())
+ m_pending_pointer_capture_target_override = nullptr;
+ if (auto pointer_capture_target_override = m_pointer_capture_target_override.ptr(); pointer_capture_target_override && !pointer_capture_target_override->is_connected())
+ m_pointer_capture_target_override = GC::Ptr<DOM::Node> { &pointer_capture_target_override->document() };
+
+ GC::Ptr<DOM::Node> pointer_capture_target_override = m_pointer_capture_target_override.ptr();
+ GC::Ptr<DOM::Node> pending_pointer_capture_target_override = m_pending_pointer_capture_target_override.ptr();
+ auto events_fired = PointerCaptureEventsFired::No;
+
+ // 1. If the pointer capture target override for this pointer is set and is not equal to the pending pointer
+ // capture target override, then fire a pointer event named lostpointercapture at the pointer capture target
+ // override node.
+ if (pointer_capture_target_override && pointer_capture_target_override != pending_pointer_capture_target_override) {
+ fire_pointer_capture_event(read, UIEvents::EventNames::lostpointercapture, *pointer_capture_target_override);
+ events_fired = PointerCaptureEventsFired::Yes;
+ }
+
+ // 2. If the pending pointer capture target override for this pointer is set and is not equal to the pointer
+ // capture target override, then fire a pointer event named gotpointercapture at the pending pointer capture
+ // target override.
+ if (pending_pointer_capture_target_override && pending_pointer_capture_target_override != pointer_capture_target_override) {
+ fire_pointer_capture_event(read, UIEvents::EventNames::gotpointercapture, *pending_pointer_capture_target_override);
+ events_fired = PointerCaptureEventsFired::Yes;
+ }
+
+ // 3. Set the pointer capture target override to the pending pointer capture target override, if set. Otherwise,
+ // clear the pointer capture target override.
+ // NB: A listener of the events above may have set or released the capture again, which the override follows.
+ m_pointer_capture_target_override = GC::Ptr<DOM::Node> { m_pending_pointer_capture_target_override.ptr() };
+
+ // NB: A listener of the events above may have removed the capturing element as well. Its document then holds the
+ // capture, and loses it before the pointer event these steps precede rather than at the one after.
+ if (auto pointer_capture_target_override = m_pointer_capture_target_override.ptr(); pointer_capture_target_override && !pointer_capture_target_override->is_connected()) {
+ m_pending_pointer_capture_target_override = nullptr;
+ m_pointer_capture_target_override = nullptr;
+ fire_pointer_capture_event(read, UIEvents::EventNames::lostpointercapture, pointer_capture_target_override->document());
+ events_fired = PointerCaptureEventsFired::Yes;
+ }
+ return events_fired;
+}
+
+// https://w3c.github.io/pointerevents/#the-gotpointercapture-event
+// https://w3c.github.io/pointerevents/#the-lostpointercapture-event
+void EventHandler::fire_pointer_capture_event(Layout::BegunRead const& read, Utf16FlyString const& event_name, DOM::Node& target)
+{
+ // The event tells where the pointer is, which is where its last pointer event went.
+ MouseEventCoordinates coordinates;
+ CSSPixelPoint screen_position;
+ if (m_last_dispatched_pointer_event_position.has_value()) {
+ coordinates = m_last_dispatched_pointer_event_position->coordinates;
+ screen_position = m_last_dispatched_pointer_event_position->screen_position;
+ // NB: The listeners of the events fired before this one may have invalidated layout.
+ target.document().update_layout(DOM::UpdateLayoutReason::EventHandlerPointerCaptureEvents);
+ if (auto* target_layout_node = target.layout_node(read))
+ coordinates = compute_mouse_event_coordinates(coordinates.visual_viewport_position, coordinates.viewport_position, *target_layout_node);
+ }
+ auto event = MUST(UIEvents::PointerEvent::create_from_platform_event(HTML::relevant_global_object(target), m_navigable->active_window_proxy(), event_name, screen_position, coordinates.page_offset, coordinates.viewport_position, coordinates.offset, {}, UIEvents::MouseButton::None, m_buttons_of_last_dispatched_pointer_event, m_last_known_mouse_modifiers));
+ // Bubbles: Yes. Cancelable: No. Composed: Yes.
+ event->set_cancelable(false);
+ target.dispatch_event(event);
+}
+
+// https://w3c.github.io/pointerevents/#implicit-release-of-pointer-capture
+void EventHandler::release_pointer_capture_implicitly(Layout::BegunRead const& read)
+{
+ // Immediately after firing the pointerup or pointercancel events, the user agent MUST clear the pending pointer
+ // capture target override for the pointerId of the pointerup or pointercancel event that was just dispatched, and
+ // then run process pending pointer capture steps to fire lostpointercapture if necessary.
+ m_pending_pointer_capture_target_override = nullptr;
+ process_pending_pointer_capture(read);
}
// https://html.spec.whatwg.org/multipage/interactive-elements.html#run-light-dismiss-activities
@@ -4206,11 +4316,36 @@ static void set_node_and_ancestors_being_activated(GC::Ptr<DOM::Node> node, bool
}
// https://w3c.github.io/pointerevents/#mapping-for-devices-that-support-hover
-EventHandler::PointerEventDispatchResult EventHandler::dispatch_a_pointer_event_for_a_device_that_supports_hover(PointerEventType type, GC::Ptr<DOM::Node> node, RefPtr<Painting::ChromeWidget> chrome_widget, MouseEventCoordinates const& coordinates, CSSPixelPoint screen_position, CSSPixelPoint movement, unsigned button, unsigned buttons, unsigned modifiers, int click_count)
+EventHandler::DispatchedPointerEvent EventHandler::dispatch_a_pointer_event_for_a_device_that_supports_hover(Layout::BegunRead const& read, PointerEventType type, GC::Ptr<DOM::Node> hit_test_target, RefPtr<Painting::ChromeWidget> chrome_widget, MouseEventCoordinates coordinates, CSSPixelPoint screen_position, CSSPixelPoint movement, unsigned button, unsigned buttons, unsigned modifiers, int click_count)
{
auto& document = *m_navigable->active_document();
auto& relevant_global_object = HTML::relevant_global_object(document);
+ // https://w3c.github.io/pointerevents/#firing-events-using-the-pointerevent-interface
+ // 1. If the event is not a gotpointercapture, lostpointercapture, click, auxclick or contextmenu event, run the
+ // process pending pointer capture steps for this PointerEvent.
+ if (process_pending_pointer_capture(read) == PointerCaptureEventsFired::Yes) {
+ // NB: Dispatching an event may have disturbed the world.
+ if (m_navigable->active_document().ptr() != &document)
+ return { PointerEventDispatchResult::CancelledByPage, {} };
+ document.update_layout(DOM::UpdateLayoutReason::EventHandlerPointerCaptureEvents);
+ }
+
+ // 2. Determine the target at which the event is fired as follows:
+ // - If the pointer capture target override has been set for the pointer, set the target to pointer capture
+ // target override object.
+ // - Otherwise, set the target to the object returned by normal hit test mechanisms.
+ GC::Ptr<DOM::Node> node = hit_test_target;
+ GC::Ptr<DOM::Node> pointer_capture_target = m_pointer_capture_target_override.ptr();
+ auto hit_test_coordinates = coordinates;
+ if (pointer_capture_target) {
+ node = pointer_capture_target;
+ // NB: A chrome widget under the pointer, such as a scrollbar, is a hit test result as well.
+ chrome_widget = nullptr;
+ if (auto* pointer_capture_target_layout_node = node->layout_node(read))
+ coordinates = compute_mouse_event_coordinates(coordinates.visual_viewport_position, coordinates.viewport_position, *pointer_capture_target_layout_node);
+ }
+
auto pointer_event_name = [&] {
switch (type) {
case PointerEventType::PointerDown:
@@ -4263,7 +4398,7 @@ EventHandler::PointerEventDispatchResult EventHandler::dispatch_a_pointer_event_
if (chrome_widget || m_captured_chrome_widget)
document.update_layout(DOM::UpdateLayoutReason::EventHandlerDispatchChromeWidgetEvent);
if (!dispatch_chrome_widget_pointer_event(chrome_widget, pointer_event_name, button, coordinates.visual_viewport_position))
- return PointerEventDispatchResult::SwallowedByChromeWidget;
+ return { PointerEventDispatchResult::SwallowedByChromeWidget, pointer_capture_target };
// FIXME: This will be moved to the click event and need to track the targets of the pointerdown and pointerup events.
// https://github.com/w3c/pointerevents/pull/460
@@ -4271,6 +4406,7 @@ EventHandler::PointerEventDispatchResult EventHandler::dispatch_a_pointer_event_
// 3. Dispatch the pointer event.
m_last_dispatched_pointer_event_position = PointerEventPosition { coordinates, screen_position };
+ m_buttons_of_last_dispatched_pointer_event = type == PointerEventType::PointerCancel ? 0 : buttons;
bool pointer_event_cancelled = !node->dispatch_event(pointer_event);
// 4. If the pointer event dispatched was pointerdown and event's canceled flag is set, then set the PREVENT MOUSE
@@ -4278,6 +4414,29 @@ EventHandler::PointerEventDispatchResult EventHandler::dispatch_a_pointer_event_
if (type == PointerEventType::PointerDown && pointer_event_cancelled)
m_prevent_mouse_event = true;
+ // https://w3c.github.io/pointerevents/#implicit-release-of-pointer-capture
+ if (type == PointerEventType::PointerUp || type == PointerEventType::PointerCancel) {
+ release_pointer_capture_implicitly(read);
+
+ // After running process pending pointer capture steps, if the pointer supports hover, user agent MUST also
+ // send corresponding boundary events necessary to reflect the current position of the pointer with no capture.
+ // NB: A cancelled pointer leaves the window, which its caller moves it out of.
+ if (type == PointerEventType::PointerUp && pointer_capture_target && m_navigable->active_document().ptr() == &document) {
+ // NB: The listeners of the events above may have invalidated layout.
+ document.update_layout(DOM::UpdateLayoutReason::EventHandlerHandleMouseUp);
+ track_the_effective_position_of_the_legacy_mouse_pointer(hit_test_target, DOM::HoverEventData {
+ .screen_position = screen_position,
+ .page_offset = hit_test_coordinates.page_offset,
+ .viewport_position = hit_test_coordinates.viewport_position,
+ .offset = hit_test_coordinates.offset,
+ .movement = movement,
+ .button = button,
+ .buttons = buttons,
+ .modifiers = modifiers,
+ });
+ }
+ }
+
// 5. If the PREVENT MOUSE EVENT flag is not set for this pointerType and the pointer event dispatched was:
bool run_default_activation_behavior = false;
if (!m_prevent_mouse_event) {
@@ -4308,7 +4467,7 @@ EventHandler::PointerEventDispatchResult EventHandler::dispatch_a_pointer_event_
if (type == PointerEventType::PointerUp || type == PointerEventType::PointerCancel)
m_prevent_mouse_event = false;
- return run_default_activation_behavior ? PointerEventDispatchResult::RunDefaultActions : PointerEventDispatchResult::CancelledByPage;
+ return { run_default_activation_behavior ? PointerEventDispatchResult::RunDefaultActions : PointerEventDispatchResult::CancelledByPage, pointer_capture_target };
}
void EventHandler::track_the_effective_position_of_the_legacy_mouse_pointer(GC::Ptr<DOM::Node> target, Optional<DOM::HoverEventData> hover_event_data)
@@ -4398,6 +4557,10 @@ void EventHandler::reset_hover_for_document_replacement(Badge<HTML::LocalNavigab
// this document (an iframe the pointer once passed through, e.g.) is going away underneath it.
m_effective_legacy_mouse_pointer_position = nullptr;
m_wheel_scroll_latch.clear();
+ // A capture in the outgoing document ends with it, with no lostpointercapture event into that document.
+ m_pending_pointer_capture_target_override = nullptr;
+ m_pointer_capture_target_override = nullptr;
+ m_buttons_of_last_dispatched_pointer_event = 0;
if (m_navigable->page().hover_reporting_navigable() == m_navigable)
report_hovered_node_to_client(nullptr);
}
diff --git a/Libraries/LibWeb/Page/EventHandler.h b/Libraries/LibWeb/Page/EventHandler.h
index 564aa81605f..b042d3686c4 100644
--- a/Libraries/LibWeb/Page/EventHandler.h
+++ b/Libraries/LibWeb/Page/EventHandler.h
@@ -71,6 +71,13 @@ public:
EventResult handle_mousewheel(CSSPixelPoint, CSSPixelPoint screen_position, unsigned button, unsigned buttons, unsigned modifiers, double wheel_delta_x, double wheel_delta_y, double wheel_tick_120ths_x, double wheel_tick_120ths_y, Web::WheelDeltaPrecision = Web::WheelDeltaPrecision::Discrete, Web::ScrollGesturePhase = Web::ScrollGesturePhase::None, bool async_scroll_performed_default_action = false, Optional<AsyncScrollOperation>* async_scroll_operation = nullptr, Optional<RemoteInputEventTarget>* remote_target = nullptr);
EventResult handle_mouseleave();
EventResult handle_mousecancel();
+
+ // https://w3c.github.io/pointerevents/#pointer-capture
+ // The mouse is the one pointer there is. It is in its active buttons state while the page holds a button of it.
+ bool is_mouse_pointer_in_active_buttons_state() const { return m_buttons_of_last_dispatched_pointer_event != 0; }
+ GC::Ptr<DOM::Element> pending_pointer_capture_target_override() const { return m_pending_pointer_capture_target_override.ptr(); }
+ void set_pending_pointer_capture_target_override(Badge<DOM::Element>, DOM::Element&);
+ void clear_pending_pointer_capture_target_override(Badge<DOM::Element>);
EventResult insert_replacement_text(Utf16String const&);
#if defined(AK_OS_MACOS)
bool select_word_for_dictionary_lookup(CSSPixelPoint visual_viewport_position);
@@ -210,7 +217,22 @@ private:
CancelledByPage,
SwallowedByChromeWidget,
};
- PointerEventDispatchResult dispatch_a_pointer_event_for_a_device_that_supports_hover(PointerEventType, GC::Ptr<DOM::Node>, RefPtr<Painting::ChromeWidget>, MouseEventCoordinates const&, CSSPixelPoint screen_position, CSSPixelPoint movement, unsigned button, unsigned buttons, unsigned modifiers, int click_count = 0);
+ struct DispatchedPointerEvent {
+ PointerEventDispatchResult result;
+ // The node that captured the pointer as the event fired, which the event went to instead of the hit test
+ // target, if any.
+ GC::Ptr<DOM::Node> pointer_capture_target;
+ };
+ DispatchedPointerEvent dispatch_a_pointer_event_for_a_device_that_supports_hover(Layout::BegunRead const&, PointerEventType, GC::Ptr<DOM::Node> hit_test_target, RefPtr<Painting::ChromeWidget>, MouseEventCoordinates, CSSPixelPoint screen_position, CSSPixelPoint movement, unsigned button, unsigned buttons, unsigned modifiers, int click_count = 0);
+ // Whether the next pointer event goes to a capturing element rather than to the hit test target.
+ bool next_pointer_event_is_captured() const;
+ enum class PointerCaptureEventsFired : u8 {
+ No,
+ Yes,
+ };
+ PointerCaptureEventsFired process_pending_pointer_capture(Layout::BegunRead const&);
+ void fire_pointer_capture_event(Layout::BegunRead const&, Utf16FlyString const& event_name, DOM::Node& target);
+ void release_pointer_capture_implicitly(Layout::BegunRead const&);
void track_the_effective_position_of_the_legacy_mouse_pointer(GC::Ptr<DOM::Node>, Optional<DOM::HoverEventData> = {});
void report_hovered_node_to_client(GC::Ptr<DOM::Node>);
@@ -268,6 +290,15 @@ private:
CSSPixelPoint screen_position;
};
Optional<PointerEventPosition> m_last_dispatched_pointer_event_position;
+ // The buttons of the last pointerdown, pointermove or pointerup dispatched, which say whether the mouse is in its
+ // active buttons state.
+ unsigned m_buttons_of_last_dispatched_pointer_event { 0 };
+
+ // https://w3c.github.io/pointerevents/#dfn-pending-pointer-capture-target-override
+ GC::Weak<DOM::Element> m_pending_pointer_capture_target_override;
+ // https://w3c.github.io/pointerevents/#dfn-pointer-capture-target-override
+ // The document once the capturing element is removed from it, which lostpointercapture then fires at.
+ GC::Weak<DOM::Node> m_pointer_capture_target_override;
OwnPtr<AutoScrollHandler> m_auto_scroll_handler;
OwnPtr<MiddleButtonScrollHandler> m_middle_button_scroll_handler;
diff --git a/Libraries/LibWeb/UIEvents/PointerEvent.cpp b/Libraries/LibWeb/UIEvents/PointerEvent.cpp
index 2779b01ef77..b56742fd9c1 100644
--- a/Libraries/LibWeb/UIEvents/PointerEvent.cpp
+++ b/Libraries/LibWeb/UIEvents/PointerEvent.cpp
@@ -53,6 +53,7 @@ WebIDL::ExceptionOr<GC::Ref<PointerEvent>> PointerEvent::create_from_platform_ev
event_options.client_x = client.x().to_double();
event_options.client_y = client.y().to_double();
event_options.view = window_proxy;
+ event_options.pointer_id = mouse_pointer_id;
event_options.is_primary = true;
event_options.pointer_type = PointerTypes::Mouse;
if (movement.has_value()) {
@@ -81,6 +82,7 @@ WebIDL::ExceptionOr<GC::Ref<PointerEvent>> PointerEvent::create_from_platform_ev
event_options.client_x = client.x().to_double();
event_options.client_y = client.y().to_double();
event_options.view = window_proxy;
+ event_options.pointer_id = mouse_pointer_id;
event_options.is_primary = true;
event_options.pointer_type = PointerTypes::Mouse;
if (movement.has_value()) {
diff --git a/Libraries/LibWeb/UIEvents/PointerEvent.h b/Libraries/LibWeb/UIEvents/PointerEvent.h
index 579b699226e..b4cc110face 100644
--- a/Libraries/LibWeb/UIEvents/PointerEvent.h
+++ b/Libraries/LibWeb/UIEvents/PointerEvent.h
@@ -28,8 +28,12 @@ namespace Web::UIEvents {
class PointerEvent;
+// https://w3c.github.io/pointerevents/#dom-pointerevent-pointerid
+// The mouse is the one pointer there is, and this is its id in every pointer event of it.
+constexpr WebIDL::Long mouse_pointer_id = 0;
+
struct PointerEventOptions : public MouseEventOptions {
- WebIDL::Long pointer_id { 0 };
+ WebIDL::Long pointer_id { mouse_pointer_id };
double width { 1 };
double height { 1 };
float pressure { 0 };
diff --git a/Tests/LibWeb/Text/expected/UIEvents/pointer-capture-lost-by-document-when-capturing-element-is-removed.txt b/Tests/LibWeb/Text/expected/UIEvents/pointer-capture-lost-by-document-when-capturing-element-is-removed.txt
new file mode 100644
index 00000000000..c03d1c98c21
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/UIEvents/pointer-capture-lost-by-document-when-capturing-element-is-removed.txt
@@ -0,0 +1,7 @@
+press: (no events)
+first move after the press: gotpointercapture@target, lostpointercapture@document, pointermove@other
+second move: pointermove@other
+release: pointerup@other
+press and move with a capture: gotpointercapture@replacement (own listener), gotpointercapture@replacement, pointermove@replacement (own listener), pointermove@replacement
+move after the capturing element was removed: lostpointercapture@document, pointermove@other
+release: pointerup@other
diff --git a/Tests/LibWeb/Text/expected/UIEvents/pointer-capture-method-errors-and-no-ops.txt b/Tests/LibWeb/Text/expected/UIEvents/pointer-capture-method-errors-and-no-ops.txt
new file mode 100644
index 00000000000..b759e2d2baa
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/UIEvents/pointer-capture-method-errors-and-no-ops.txt
@@ -0,0 +1,13 @@
+setPointerCapture with an unknown pointerId: NotFoundError
+releasePointerCapture with an unknown pointerId: NotFoundError
+hasPointerCapture with an unknown pointerId: false
+setPointerCapture on an element that is not connected: InvalidStateError
+setPointerCapture while no button is held: no exception
+hasPointerCapture while no button is held: false
+releasePointerCapture while not captured: no exception
+move after setPointerCapture while no button was held: pointermove@other
+hasPointerCapture after setPointerCapture in pointerdown: true
+hasPointerCapture after releasePointerCapture in pointerdown: false
+press, move and release after a capture set and released in pointerdown: pointermove@other
+hasPointerCapture after setPointerCapture in pointerup: false
+move after a capture set in pointerup: pointermove@other
diff --git a/Tests/LibWeb/Text/expected/UIEvents/pointer-capture-retargets-events-to-capturing-element.txt b/Tests/LibWeb/Text/expected/UIEvents/pointer-capture-retargets-events-to-capturing-element.txt
new file mode 100644
index 00000000000..f7bc30de302
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/UIEvents/pointer-capture-retargets-events-to-capturing-element.txt
@@ -0,0 +1,69 @@
+move onto the thumb:
+ pointerover@thumb
+ mouseover@thumb
+ pointerenter@track
+ mouseenter@track
+ pointerenter@thumb
+ mouseenter@thumb
+ pointermove@thumb client=160,110 offset=10,10
+ mousemove@thumb
+hasPointerCapture right after setPointerCapture: true
+press the thumb:
+ pointerdown@thumb
+ mousedown@thumb
+drag along the track:
+ gotpointercapture@thumb
+ pointermove@thumb client=250,110 offset=100,10
+ mousemove@thumb
+drag over the other element:
+ pointermove@thumb client=200,325 offset=50,225
+ mousemove@thumb
+hasPointerCapture during pointerup: true
+release over the other element:
+ pointerup@thumb
+ lostpointercapture@thumb
+ pointerout@thumb
+ mouseout@thumb
+ pointerleave@thumb
+ pointerleave@track
+ mouseleave@thumb
+ mouseleave@track
+ pointerover@other
+ mouseover@other
+ pointerenter@other
+ mouseenter@other
+ mouseup@thumb
+ click@thumb client=200,325 offset=50,225
+hasPointerCapture after the release: false
+press the other element, which hands the pointer to the thumb:
+ pointerdown@other
+ mousedown@other
+move as the thumb takes the capture:
+ gotpointercapture@thumb
+ pointerout@other
+ mouseout@other
+ pointerleave@other
+ mouseleave@other
+ pointerover@thumb
+ mouseover@thumb
+ pointerenter@track
+ mouseenter@track
+ pointerenter@thumb
+ mouseenter@thumb
+ pointermove@thumb client=210,330 offset=60,230
+ mousemove@thumb
+release:
+ pointerup@thumb
+ lostpointercapture@thumb
+ pointerout@thumb
+ mouseout@thumb
+ pointerleave@thumb
+ pointerleave@track
+ mouseleave@thumb
+ mouseleave@track
+ pointerover@other
+ mouseover@other
+ pointerenter@other
+ mouseenter@other
+ mouseup@thumb
+ click@thumb client=210,330 offset=60,230
diff --git a/Tests/LibWeb/Text/input/UIEvents/pointer-capture-lost-by-document-when-capturing-element-is-removed.html b/Tests/LibWeb/Text/input/UIEvents/pointer-capture-lost-by-document-when-capturing-element-is-removed.html
new file mode 100644
index 00000000000..e5e0971e7d2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/UIEvents/pointer-capture-lost-by-document-when-capturing-element-is-removed.html
@@ -0,0 +1,71 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+ #target {
+ position: absolute;
+ left: 0;
+ top: 0;
+ width: 100px;
+ height: 100px;
+ background: gray;
+ }
+ #other {
+ position: absolute;
+ left: 200px;
+ top: 200px;
+ width: 100px;
+ height: 100px;
+ background: lightblue;
+ }
+</style>
+<div id="target"></div>
+<div id="other"></div>
+<script>
+ test(() => {
+ const target = document.getElementById("target");
+ const log = [];
+ const describe = e => `${e.type}@${e.target === document ? "document" : e.target.id || e.target.nodeName}`;
+ for (const type of ["gotpointercapture", "lostpointercapture", "pointermove", "pointerup"])
+ document.addEventListener(type, e => log.push(describe(e)));
+ for (const type of ["lostpointercapture", "pointermove", "pointerup"])
+ target.addEventListener(type, e => log.push(`${describe(e)} (own listener)`));
+ const flush = label => {
+ println(`${label}: ${log.join(", ") || "(no events)"}`);
+ log.length = 0;
+ };
+
+ // Removing the capturing element hands its capture to the document, which loses it before the next pointer
+ // event. That event goes to the element under the pointer, not to the removed one.
+ target.addEventListener("pointerdown", e => target.setPointerCapture(e.pointerId), { once: true });
+ target.addEventListener("gotpointercapture", () => target.remove(), { once: true });
+ internals.mouseDown(50, 50);
+ flush("press");
+ internals.mouseMove(250, 250);
+ flush("first move after the press");
+ internals.mouseMove(260, 260);
+ flush("second move");
+ internals.mouseUp(260, 260);
+ flush("release");
+
+ // The same, with the capturing element removed while it holds the capture: the document loses it at the next
+ // pointer event, which goes to the element under the pointer.
+ const replacement = document.createElement("div");
+ replacement.id = "replacement";
+ replacement.style.cssText = "position: absolute; left: 0; top: 0; width: 100px; height: 100px; background: gray";
+ document.body.appendChild(replacement);
+ for (const type of ["gotpointercapture", "lostpointercapture", "pointermove", "pointerup"])
+ replacement.addEventListener(type, e => log.push(`${describe(e)} (own listener)`), { capture: false });
+ replacement.addEventListener("pointerdown", e => replacement.setPointerCapture(e.pointerId), { once: true });
+ internals.mouseDown(50, 50);
+ internals.mouseMove(60, 60);
+ flush("press and move with a capture");
+ replacement.remove();
+ internals.mouseMove(250, 250);
+ flush("move after the capturing element was removed");
+ internals.mouseUp(250, 250);
+ flush("release");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/UIEvents/pointer-capture-method-errors-and-no-ops.html b/Tests/LibWeb/Text/input/UIEvents/pointer-capture-method-errors-and-no-ops.html
new file mode 100644
index 00000000000..85ce624a10f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/UIEvents/pointer-capture-method-errors-and-no-ops.html
@@ -0,0 +1,83 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+ #target {
+ position: absolute;
+ left: 0;
+ top: 0;
+ width: 100px;
+ height: 100px;
+ background: gray;
+ }
+ #other {
+ position: absolute;
+ left: 200px;
+ top: 200px;
+ width: 100px;
+ height: 100px;
+ background: lightblue;
+ }
+</style>
+<div id="target"></div>
+<div id="other"></div>
+<script>
+ test(() => {
+ const target = document.getElementById("target");
+ const log = [];
+ for (const type of ["gotpointercapture", "lostpointercapture", "pointermove"])
+ document.addEventListener(type, e => log.push(`${e.type}@${e.target.id}`));
+ const flush = label => {
+ println(`${label}: ${log.join(", ") || "(no events)"}`);
+ log.length = 0;
+ };
+ const tryCall = (label, call) => {
+ try {
+ call();
+ println(`${label}: no exception`);
+ } catch (e) {
+ println(`${label}: ${e.name}`);
+ }
+ };
+
+ tryCall("setPointerCapture with an unknown pointerId", () => target.setPointerCapture(123));
+ tryCall("releasePointerCapture with an unknown pointerId", () => target.releasePointerCapture(123));
+ println(`hasPointerCapture with an unknown pointerId: ${target.hasPointerCapture(123)}`);
+ tryCall("setPointerCapture on an element that is not connected", () => document.createElement("div").setPointerCapture(0));
+
+ // Neither call does anything while no button is held, so the pointer stays uncaptured.
+ tryCall("setPointerCapture while no button is held", () => target.setPointerCapture(0));
+ println(`hasPointerCapture while no button is held: ${target.hasPointerCapture(0)}`);
+ tryCall("releasePointerCapture while not captured", () => target.releasePointerCapture(0));
+ internals.mouseMove(250, 250);
+ flush("move after setPointerCapture while no button was held");
+
+ // A capture released by the pointerdown listener that set it never takes effect.
+ target.addEventListener("pointerdown", e => {
+ target.setPointerCapture(e.pointerId);
+ println(`hasPointerCapture after setPointerCapture in pointerdown: ${target.hasPointerCapture(e.pointerId)}`);
+ target.releasePointerCapture(e.pointerId);
+ println(`hasPointerCapture after releasePointerCapture in pointerdown: ${target.hasPointerCapture(e.pointerId)}`);
+ }, { once: true });
+ internals.mouseMove(50, 50);
+ log.length = 0;
+ internals.mouseDown(50, 50);
+ internals.mouseMove(250, 250);
+ internals.mouseUp(250, 250);
+ flush("press, move and release after a capture set and released in pointerdown");
+
+ // The pointer is no longer in its active buttons state as pointerup fires, so a capture set then is dropped.
+ target.addEventListener("pointerup", e => {
+ target.setPointerCapture(e.pointerId);
+ println(`hasPointerCapture after setPointerCapture in pointerup: ${target.hasPointerCapture(e.pointerId)}`);
+ }, { once: true });
+ internals.mouseMove(50, 50);
+ internals.mouseDown(50, 50);
+ internals.mouseUp(50, 50);
+ log.length = 0;
+ internals.mouseMove(250, 250);
+ flush("move after a capture set in pointerup");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/UIEvents/pointer-capture-retargets-events-to-capturing-element.html b/Tests/LibWeb/Text/input/UIEvents/pointer-capture-retargets-events-to-capturing-element.html
new file mode 100644
index 00000000000..fa6f4d0f8be
--- /dev/null
+++ b/Tests/LibWeb/Text/input/UIEvents/pointer-capture-retargets-events-to-capturing-element.html
@@ -0,0 +1,96 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+ #track {
+ position: absolute;
+ left: 100px;
+ top: 100px;
+ width: 200px;
+ height: 20px;
+ background: gray;
+ }
+ #thumb {
+ position: absolute;
+ left: 50px;
+ top: 0;
+ width: 20px;
+ height: 20px;
+ background: black;
+ }
+ #other {
+ position: absolute;
+ left: 100px;
+ top: 300px;
+ width: 200px;
+ height: 50px;
+ background: lightblue;
+ }
+</style>
+<div id="track"><div id="thumb"></div></div>
+<div id="other"></div>
+<script>
+ test(() => {
+ const track = document.getElementById("track");
+ const thumb = document.getElementById("thumb");
+ const other = document.getElementById("other");
+ const log = [];
+ const describe = e => {
+ const target = e.target === document ? "document" : e.target.id || e.target.nodeName;
+ let description = `${e.type}@${target}`;
+ if (e.type === "pointermove" || e.type === "click")
+ description += ` client=${e.clientX},${e.clientY} offset=${e.offsetX},${e.offsetY}`;
+ return description;
+ };
+ const bubblingTypes = [
+ "pointerdown", "pointermove", "pointerup", "gotpointercapture", "lostpointercapture",
+ "pointerover", "pointerout", "mousedown", "mousemove", "mouseup", "mouseover", "mouseout", "click",
+ ];
+ for (const type of bubblingTypes)
+ document.addEventListener(type, e => log.push(describe(e)));
+ for (const element of [track, thumb, other])
+ for (const type of ["pointerenter", "pointerleave", "mouseenter", "mouseleave"])
+ element.addEventListener(type, e => log.push(describe(e)));
+ const flush = label => {
+ println(`${label}:`);
+ for (const line of log)
+ println(` ${line}`);
+ log.length = 0;
+ };
+
+ // A slider thumb captures the pointer that presses it, so the moves of the drag and its release go to the
+ // thumb wherever the pointer is, and the pointer leaves the thumb only once released.
+ let pointerId;
+ thumb.addEventListener("pointerdown", e => {
+ pointerId = e.pointerId;
+ thumb.setPointerCapture(pointerId);
+ println(`hasPointerCapture right after setPointerCapture: ${thumb.hasPointerCapture(pointerId)}`);
+ }, { once: true });
+ thumb.addEventListener("pointerup", e => {
+ println(`hasPointerCapture during pointerup: ${thumb.hasPointerCapture(pointerId)}`);
+ }, { once: true });
+ internals.mouseMove(160, 110);
+ flush("move onto the thumb");
+ internals.mouseDown(160, 110);
+ flush("press the thumb");
+ internals.mouseMove(250, 110);
+ flush("drag along the track");
+ internals.mouseMove(200, 325);
+ flush("drag over the other element");
+ internals.mouseUp(200, 325);
+ flush("release over the other element");
+ println(`hasPointerCapture after the release: ${thumb.hasPointerCapture(pointerId)}`);
+
+ // An element other than the pressed one can capture the pointer. The pointer moves into it as the capture
+ // takes effect, and the click of the press goes to it.
+ other.addEventListener("pointerdown", e => thumb.setPointerCapture(e.pointerId), { once: true });
+ internals.mouseDown(200, 325);
+ flush("press the other element, which hands the pointer to the thumb");
+ internals.mouseMove(210, 330);
+ flush("move as the thumb takes the capture");
+ internals.mouseUp(210, 330);
+ flush("release");
+ });
+</script>