2020-03-18 19:20:35 +00:00
/*
2022-02-15 18:15:52 +00:00
* Copyright ( c ) 2020 - 2022 , Andreas Kling < kling @ serenityos . org >
2020-03-18 19:20:35 +00:00
*
2021-04-22 08:24:48 +00:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-03-18 19:20:35 +00:00
*/
2020-03-18 14:22:31 +00:00
# pragma once
# include <AK/Noncopyable.h>
# include <AK/Vector.h>
2020-09-06 12:28:41 +00:00
# include <LibJS/Forward.h>
2022-08-28 11:42:07 +00:00
# include <LibWeb/Bindings/PlatformObject.h>
2022-02-16 19:43:24 +00:00
# include <LibWeb/DOM/DOMEventListener.h>
2020-03-18 14:22:31 +00:00
# include <LibWeb/Forward.h>
2021-10-14 17:03:08 +00:00
# include <LibWeb/HTML/EventHandler.h>
2022-09-25 16:03:42 +00:00
# include <LibWeb/WebIDL/ExceptionOr.h>
2020-03-18 14:22:31 +00:00
2020-07-26 17:37:56 +00:00
namespace Web : : DOM {
2020-03-18 14:22:31 +00:00
2022-08-28 11:42:07 +00:00
class EventTarget : public Bindings : : PlatformObject {
WEB_PLATFORM_OBJECT ( EventTarget , Bindings : : PlatformObject ) ;
2024-04-06 17:16:04 +00:00
JS_DECLARE_ALLOCATOR ( EventTarget ) ;
2020-03-18 14:22:31 +00:00
public :
2022-09-25 22:15:49 +00:00
virtual ~ EventTarget ( ) override ;
2020-03-18 14:22:31 +00:00
2023-05-03 20:10:26 +00:00
static WebIDL : : ExceptionOr < JS : : NonnullGCPtr < EventTarget > > construct_impl ( JS : : Realm & ) ;
2022-02-06 18:27:44 +00:00
virtual bool is_focusable ( ) const { return false ; }
2023-04-09 07:52:27 +00:00
void add_event_listener ( FlyString const & type , IDLEventListener * callback , Variant < AddEventListenerOptions , bool > const & options ) ;
void remove_event_listener ( FlyString const & type , IDLEventListener * callback , Variant < EventListenerOptions , bool > const & options ) ;
2022-02-19 22:02:32 +00:00
// NOTE: These are for internal use only. They operate as though addEventListener(type, callback) was called instead of addEventListener(type, callback, options).
2023-04-09 07:52:27 +00:00
void add_event_listener_without_options ( FlyString const & type , IDLEventListener & callback ) ;
void remove_event_listener_without_options ( FlyString const & type , IDLEventListener & callback ) ;
2020-11-21 18:32:39 +00:00
2022-08-08 20:29:40 +00:00
virtual bool dispatch_event ( Event & ) ;
2022-09-25 16:03:42 +00:00
WebIDL : : ExceptionOr < bool > dispatch_event_binding ( Event & ) ;
2021-05-04 21:38:36 +00:00
2022-04-01 17:58:27 +00:00
virtual EventTarget * get_parent ( Event const & ) { return nullptr ; }
2020-11-21 18:32:39 +00:00
2022-08-08 12:12:01 +00:00
void add_an_event_listener ( DOMEventListener & ) ;
2022-02-16 19:43:24 +00:00
void remove_an_event_listener ( DOMEventListener & ) ;
void remove_from_event_listener_list ( DOMEventListener & ) ;
2020-03-18 14:22:31 +00:00
2022-08-28 11:42:07 +00:00
Vector < JS : : Handle < DOMEventListener > > event_listener_list ( ) ;
2020-03-18 14:22:31 +00:00
2023-11-18 09:48:09 +00:00
virtual bool has_activation_behavior ( ) const ;
virtual void activation_behavior ( Event const & ) ;
2020-11-21 18:32:39 +00:00
// NOTE: These only exist for checkbox and radio input elements.
2022-02-18 19:40:12 +00:00
virtual void legacy_pre_activation_behavior ( ) { }
virtual void legacy_cancelled_activation_behavior ( ) { }
2022-03-14 23:05:55 +00:00
virtual void legacy_cancelled_activation_behavior_was_not_called ( ) { }
2020-11-21 18:32:39 +00:00
2023-04-06 05:25:18 +00:00
WebIDL : : CallbackType * event_handler_attribute ( FlyString const & name ) ;
void set_event_handler_attribute ( FlyString const & name , WebIDL : : CallbackType * ) ;
2021-09-18 23:38:14 +00:00
2023-04-09 07:52:27 +00:00
bool has_event_listener ( FlyString const & type ) const ;
2023-02-28 18:39:11 +00:00
bool has_event_listeners ( ) const ;
2022-10-24 12:56:29 +00:00
2020-03-18 14:22:31 +00:00
protected :
2023-11-09 07:29:52 +00:00
explicit EventTarget ( JS : : Realm & , MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess : : No ) ;
2020-03-18 14:22:31 +00:00
2023-04-09 07:52:27 +00:00
void element_event_handler_attribute_changed ( FlyString const & local_name , Optional < String > const & value ) ;
2020-09-20 17:22:44 +00:00
2023-08-07 06:41:28 +00:00
virtual void initialize ( JS : : Realm & ) override ;
2022-08-28 11:42:07 +00:00
virtual void visit_edges ( Cell : : Visitor & ) override ;
2021-10-14 17:03:08 +00:00
private :
2023-11-18 10:15:08 +00:00
struct Data {
Vector < JS : : NonnullGCPtr < DOMEventListener > > event_listener_list ;
2021-10-14 17:03:08 +00:00
2023-11-18 10:15:08 +00:00
// https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-map
// Spec Note: The order of the entries of event handler map could be arbitrary. It is not observable through any algorithms that operate on the map.
2024-03-09 19:58:01 +00:00
HashMap < FlyString , JS : : NonnullGCPtr < HTML : : EventHandler > > event_handler_map ;
2023-11-18 10:15:08 +00:00
} ;
Data & ensure_data ( ) ;
OwnPtr < Data > m_data ;
2021-10-14 17:03:08 +00:00
2023-04-09 07:52:27 +00:00
WebIDL : : CallbackType * get_current_value_of_event_handler ( FlyString const & name ) ;
void activate_event_handler ( FlyString const & name , HTML : : EventHandler & event_handler ) ;
void deactivate_event_handler ( FlyString const & name ) ;
JS : : ThrowCompletionOr < void > process_event_handler_for_event ( FlyString const & name , Event & event ) ;
2020-03-18 14:22:31 +00:00
} ;
2023-04-06 05:25:18 +00:00
bool is_window_reflecting_body_element_event_handler ( FlyString const & name ) ;
2022-06-27 18:20:09 +00:00
2020-03-18 14:22:31 +00:00
}