2020-03-18 22:20:35 +03:00
/*
2022-02-15 21:15:52 +03:00
* Copyright ( c ) 2020 - 2022 , Andreas Kling < kling @ serenityos . org >
2020-03-18 22:20:35 +03:00
*
2021-04-22 11:24:48 +03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-03-18 22:20:35 +03:00
*/
2020-03-18 17:22:31 +03:00
# pragma once
2020-03-22 21:53:22 +03:00
# include <AK/FlyString.h>
2020-03-18 17:22:31 +03:00
# include <AK/Noncopyable.h>
# include <AK/Vector.h>
2020-09-06 15:28:41 +03:00
# include <LibJS/Forward.h>
2022-02-16 22:43:24 +03:00
# include <LibWeb/DOM/DOMEventListener.h>
2021-05-05 00:38:36 +03:00
# include <LibWeb/DOM/ExceptionOr.h>
2020-03-18 17:22:31 +03:00
# include <LibWeb/Forward.h>
2021-10-14 20:03:08 +03:00
# include <LibWeb/HTML/EventHandler.h>
2020-03-18 17:22:31 +03:00
2020-07-26 20:37:56 +03:00
namespace Web : : DOM {
2020-03-18 17:22:31 +03:00
class EventTarget {
AK_MAKE_NONCOPYABLE ( EventTarget ) ;
AK_MAKE_NONMOVABLE ( EventTarget ) ;
public :
virtual ~ EventTarget ( ) ;
void ref ( ) { ref_event_target ( ) ; }
void unref ( ) { unref_event_target ( ) ; }
2022-02-06 21:27:44 +03:00
virtual bool is_focusable ( ) const { return false ; }
2022-02-20 01:02:32 +03:00
void add_event_listener ( FlyString const & type , RefPtr < IDLEventListener > callback , Variant < AddEventListenerOptions , bool > const & options ) ;
void remove_event_listener ( FlyString const & type , RefPtr < IDLEventListener > callback , Variant < EventListenerOptions , bool > const & options ) ;
// NOTE: These are for internal use only. They operate as though addEventListener(type, callback) was called instead of addEventListener(type, callback, options).
void add_event_listener_without_options ( FlyString const & type , RefPtr < IDLEventListener > callback ) ;
void remove_event_listener_without_options ( FlyString const & type , RefPtr < IDLEventListener > callback ) ;
2020-11-21 21:32:39 +03:00
2021-09-26 00:11:51 +03:00
virtual bool dispatch_event ( NonnullRefPtr < Event > ) ;
2021-05-05 00:38:36 +03:00
ExceptionOr < bool > dispatch_event_binding ( NonnullRefPtr < Event > ) ;
2021-01-18 14:15:02 +03:00
virtual JS : : Object * create_wrapper ( JS : : GlobalObject & ) = 0 ;
2020-03-18 17:22:31 +03:00
2022-04-01 20:58:27 +03:00
virtual EventTarget * get_parent ( Event const & ) { return nullptr ; }
2020-11-21 21:32:39 +03:00
2022-02-16 22:43:24 +03:00
void add_an_event_listener ( NonnullRefPtr < DOMEventListener > ) ;
void remove_an_event_listener ( DOMEventListener & ) ;
void remove_from_event_listener_list ( DOMEventListener & ) ;
2020-03-18 17:22:31 +03:00
2022-02-16 22:43:24 +03:00
auto & event_listener_list ( ) { return m_event_listener_list ; }
auto const & event_listener_list ( ) const { return m_event_listener_list ; }
2020-03-18 17:22:31 +03:00
2022-04-01 20:58:27 +03:00
Function < void ( Event const & ) > activation_behavior ;
2020-11-21 21:32:39 +03:00
// NOTE: These only exist for checkbox and radio input elements.
2022-02-18 22:40:12 +03:00
virtual void legacy_pre_activation_behavior ( ) { }
virtual void legacy_cancelled_activation_behavior ( ) { }
2022-03-15 02:05:55 +03:00
virtual void legacy_cancelled_activation_behavior_was_not_called ( ) { }
2020-11-21 21:32:39 +03:00
2021-10-14 20:03:08 +03:00
Bindings : : CallbackType * event_handler_attribute ( FlyString const & name ) ;
void set_event_handler_attribute ( FlyString const & name , Optional < Bindings : : CallbackType > ) ;
2021-09-19 02:38:14 +03:00
2020-03-18 17:22:31 +03:00
protected :
2021-10-14 20:03:08 +03:00
EventTarget ( ) ;
2020-03-18 17:22:31 +03:00
virtual void ref_event_target ( ) = 0 ;
virtual void unref_event_target ( ) = 0 ;
2021-10-14 20:03:08 +03:00
void element_event_handler_attribute_changed ( FlyString const & local_name , String const & value ) ;
2020-09-20 20:22:44 +03:00
2021-10-14 20:03:08 +03:00
private :
2022-02-16 22:43:24 +03:00
Vector < NonnullRefPtr < DOMEventListener > > m_event_listener_list ;
2021-10-14 20:03: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.
HashMap < FlyString , HTML : : EventHandler > m_event_handler_map ;
enum class IsAttribute {
No ,
Yes ,
} ;
Bindings : : CallbackType * get_current_value_of_event_handler ( FlyString const & name ) ;
void activate_event_handler ( FlyString const & name , HTML : : EventHandler & event_handler , IsAttribute is_attribute ) ;
void deactivate_event_handler ( FlyString const & name ) ;
JS : : ThrowCompletionOr < void > process_event_handler_for_event ( FlyString const & name , Event & event ) ;
2020-03-18 17:22:31 +03:00
} ;
}