Преглед изворни кода

LibWeb: Support "useCapture" parameter to add/removeEventListener

This is not a complete implementation of API, since we're also supposed
to accept an options dictionary as the third argument. However, a lot of
web content uses the boolean variant, and it's trivial to support.
Andreas Kling пре 3 година
родитељ
комит
9521f71944

+ 4 - 4
Userland/Libraries/LibWeb/DOM/EventTarget.cpp

@@ -46,10 +46,10 @@ EventTarget::~EventTarget()
 }
 
 // https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener
-void EventTarget::add_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback)
+void EventTarget::add_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback, bool use_capture)
 {
     // FIXME: 1. Let capture, passive, once, and signal be the result of flattening more options.
-    bool capture = false;
+    bool capture = use_capture;
     bool passive = false;
     bool once = false;
     RefPtr<AbortSignal> signal = nullptr;
@@ -96,10 +96,10 @@ void EventTarget::add_an_event_listener(NonnullRefPtr<DOMEventListener> listener
 }
 
 // https://dom.spec.whatwg.org/#dom-eventtarget-removeeventlistener
-void EventTarget::remove_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback)
+void EventTarget::remove_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback, bool use_capture)
 {
     // FIXME: 1. Let capture be the result of flattening options.
-    bool capture = false;
+    bool capture = use_capture;
 
     // 2. If this’s event listener list contains an event listener whose type is type, callback is callback, and capture is capture,
     //    then remove an event listener with this and that event listener.

+ 2 - 2
Userland/Libraries/LibWeb/DOM/EventTarget.h

@@ -30,8 +30,8 @@ public:
 
     virtual bool is_focusable() const { return false; }
 
-    void add_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback);
-    void remove_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback);
+    void add_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback, bool use_capture = false);
+    void remove_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback, bool use_capture = false);
 
     virtual bool dispatch_event(NonnullRefPtr<Event>);
     ExceptionOr<bool> dispatch_event_binding(NonnullRefPtr<Event>);

+ 2 - 2
Userland/Libraries/LibWeb/DOM/EventTarget.idl

@@ -1,8 +1,8 @@
 interface EventTarget {
 
     // FIXME: Both of these should take in options
-    undefined addEventListener(DOMString type, EventListener? callback);
-    undefined removeEventListener(DOMString type, EventListener? callback);
+    undefined addEventListener(DOMString type, EventListener? callback, optional boolean useCapture = false);
+    undefined removeEventListener(DOMString type, EventListener? callback, optional boolean useCapture = false);
 
     [ImplementedAs=dispatch_event_binding] boolean dispatchEvent(Event event);