2020-03-18 16:20:35 -03:00
/*
2024-10-04 08:19:50 -03:00
* Copyright ( c ) 2020 - 2022 , Andreas Kling < andreas @ ladybird . org >
2020-03-18 16:20:35 -03:00
*
2021-04-22 05:24:48 -03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-03-18 16:20:35 -03:00
*/
2020-03-18 11:22:31 -03:00
# pragma once
# include <AK/Noncopyable.h>
# include <AK/Vector.h>
2020-09-06 09:28:41 -03:00
# include <LibJS/Forward.h>
2022-08-28 08:42:07 -03:00
# include <LibWeb/Bindings/PlatformObject.h>
2022-02-16 16:43:24 -03:00
# include <LibWeb/DOM/DOMEventListener.h>
2025-07-19 23:35:33 -03:00
# include <LibWeb/Export.h>
2020-03-18 11:22:31 -03:00
# include <LibWeb/Forward.h>
2021-10-14 14:03:08 -03:00
# include <LibWeb/HTML/EventHandler.h>
2022-09-25 13:03:42 -03:00
# include <LibWeb/WebIDL/ExceptionOr.h>
2020-03-18 11:22:31 -03:00
2020-07-26 14:37:56 -03:00
namespace Web : : DOM {
2020-03-18 11:22:31 -03:00
2025-07-19 23:35:33 -03:00
class WEB_API EventTarget : public Bindings : : PlatformObject {
2022-08-28 08:42:07 -03:00
WEB_PLATFORM_OBJECT ( EventTarget , Bindings : : PlatformObject ) ;
2024-11-14 12:01:23 -03:00
GC_DECLARE_ALLOCATOR ( EventTarget ) ;
2020-03-18 11:22:31 -03:00
public :
2022-09-25 19:15:49 -03:00
virtual ~ EventTarget ( ) override ;
2020-03-18 11:22:31 -03:00
2024-11-14 12:01:23 -03:00
static WebIDL : : ExceptionOr < GC : : Ref < EventTarget > > construct_impl ( JS : : Realm & ) ;
2023-05-03 17:10:26 -03:00
2022-02-06 15:27:44 -03:00
virtual bool is_focusable ( ) const { return false ; }
2023-04-09 04:52:27 -03: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 19:02:32 -03: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 04:52:27 -03: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 15:32:39 -03:00
2022-08-08 17:29:40 -03:00
virtual bool dispatch_event ( Event & ) ;
2022-09-25 13:03:42 -03:00
WebIDL : : ExceptionOr < bool > dispatch_event_binding ( Event & ) ;
2021-05-04 18:38:36 -03:00
2022-04-01 14:58:27 -03:00
virtual EventTarget * get_parent ( Event const & ) { return nullptr ; }
2020-11-21 15:32:39 -03:00
2022-08-08 09:12:01 -03:00
void add_an_event_listener ( DOMEventListener & ) ;
2022-02-16 16:43:24 -03:00
void remove_an_event_listener ( DOMEventListener & ) ;
2020-03-18 11:22:31 -03:00
2024-11-14 12:01:23 -03:00
Vector < GC : : Root < DOMEventListener > > event_listener_list ( ) ;
2020-03-18 11:22:31 -03:00
2023-11-18 06:48:09 -03:00
virtual bool has_activation_behavior ( ) const ;
virtual void activation_behavior ( Event const & ) ;
2020-11-21 15:32:39 -03:00
// NOTE: These only exist for checkbox and radio input elements.
2022-02-18 16:40:12 -03:00
virtual void legacy_pre_activation_behavior ( ) { }
virtual void legacy_cancelled_activation_behavior ( ) { }
2022-03-14 20:05:55 -03:00
virtual void legacy_cancelled_activation_behavior_was_not_called ( ) { }
2020-11-21 15:32:39 -03:00
2023-04-06 02:25:18 -03:00
WebIDL : : CallbackType * event_handler_attribute ( FlyString const & name ) ;
void set_event_handler_attribute ( FlyString const & name , WebIDL : : CallbackType * ) ;
2021-09-18 20:38:14 -03:00
2023-04-09 04:52:27 -03:00
bool has_event_listener ( FlyString const & type ) const ;
2023-02-28 15:39:11 -03:00
bool has_event_listeners ( ) const ;
2022-10-24 09:56:29 -03:00
2025-04-18 05:21:36 -03:00
virtual bool is_window_or_worker_global_scope_mixin ( ) const { return false ; }
2020-03-18 11:22:31 -03:00
protected :
2023-11-09 04:29:52 -03:00
explicit EventTarget ( JS : : Realm & , MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess : : No ) ;
2020-03-18 11:22:31 -03:00
2023-04-09 04:52:27 -03:00
void element_event_handler_attribute_changed ( FlyString const & local_name , Optional < String > const & value ) ;
2020-09-20 14:22:44 -03:00
2023-08-07 03:41:28 -03:00
virtual void initialize ( JS : : Realm & ) override ;
2022-08-28 08:42:07 -03:00
virtual void visit_edges ( Cell : : Visitor & ) override ;
2021-10-14 14:03:08 -03:00
private :
2023-11-18 07:15:08 -03:00
struct Data {
2024-11-14 12:01:23 -03:00
Vector < GC : : Ref < DOMEventListener > > event_listener_list ;
2021-10-14 14:03:08 -03:00
2023-11-18 07:15:08 -03: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-11-14 12:01:23 -03:00
HashMap < FlyString , GC : : Ref < HTML : : EventHandler > > event_handler_map ;
2023-11-18 07:15:08 -03:00
} ;
Data & ensure_data ( ) ;
OwnPtr < Data > m_data ;
2021-10-14 14:03:08 -03:00
2023-04-09 04:52:27 -03: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 11:22:31 -03:00
} ;
2023-04-06 02:25:18 -03:00
bool is_window_reflecting_body_element_event_handler ( FlyString const & name ) ;
2022-06-27 15:20:09 -03:00
2020-03-18 11:22:31 -03:00
}