2020-11-21 18:32:39 +00:00
/*
* Copyright ( c ) 2020 , the SerenityOS developers .
2021-10-16 05:25:53 +01:00
* Copyright ( c ) 2021 , Luke Wilde < lukew @ serenityos . org >
2024-10-04 13:19:50 +02:00
* Copyright ( c ) 2022 , Andreas Kling < andreas @ ladybird . org >
2020-11-21 18:32:39 +00:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-11-21 18:32:39 +00:00
*/
# include <AK/TypeCasts.h>
2024-04-27 12:09:58 +12:00
# include <LibWeb/Bindings/EventPrototype.h>
2022-09-25 16:15:49 -06:00
# include <LibWeb/Bindings/Intrinsics.h>
2020-11-21 18:32:39 +00:00
# include <LibWeb/DOM/Event.h>
# include <LibWeb/DOM/Node.h>
# include <LibWeb/DOM/ShadowRoot.h>
2024-04-11 22:43:39 +02:00
# include <LibWeb/HighResolutionTime/TimeOrigin.h>
2020-11-21 18:32:39 +00:00
namespace Web : : DOM {
2024-11-15 04:01:23 +13:00
GC_DEFINE_ALLOCATOR ( Event ) ;
2023-11-19 19:47:52 +01:00
2024-08-30 09:39:07 +09:00
// https://dom.spec.whatwg.org/#concept-event-create
2024-11-15 04:01:23 +13:00
GC : : Ref < Event > Event : : create ( JS : : Realm & realm , FlyString const & event_name , EventInit const & event_init )
2022-08-08 22:29:40 +02:00
{
2024-11-14 05:50:17 +13:00
auto event = realm . create < Event > ( realm , event_name , event_init ) ;
2024-08-30 09:39:07 +09:00
// 4. Initialize event’ s isTrusted attribute to true.
event - > m_is_trusted = true ;
return event ;
2022-08-08 22:29:40 +02:00
}
2024-11-15 04:01:23 +13:00
WebIDL : : ExceptionOr < GC : : Ref < Event > > Event : : construct_impl ( JS : : Realm & realm , FlyString const & event_name , EventInit const & event_init )
2022-09-25 16:15:49 -06:00
{
2024-11-14 05:50:17 +13:00
return realm . create < Event > ( realm , event_name , event_init ) ;
2022-09-25 16:15:49 -06:00
}
2024-04-11 22:43:39 +02:00
// https://dom.spec.whatwg.org/#inner-event-creation-steps
2023-04-06 16:12:33 +02:00
Event : : Event ( JS : : Realm & realm , FlyString const & type )
2023-01-10 06:56:59 -05:00
: PlatformObject ( realm )
2022-08-08 22:29:40 +02:00
, m_type ( type )
, m_initialized ( true )
2024-04-11 22:43:39 +02:00
, m_time_stamp ( HighResolutionTime : : current_high_resolution_time ( HTML : : relevant_global_object ( * this ) ) )
2022-08-08 22:29:40 +02:00
{
}
2024-04-11 22:43:39 +02:00
// https://dom.spec.whatwg.org/#inner-event-creation-steps
2023-04-06 16:12:33 +02:00
Event : : Event ( JS : : Realm & realm , FlyString const & type , EventInit const & event_init )
2023-01-10 06:56:59 -05:00
: PlatformObject ( realm )
2022-08-08 22:29:40 +02:00
, m_type ( type )
, m_bubbles ( event_init . bubbles )
, m_cancelable ( event_init . cancelable )
, m_composed ( event_init . composed )
, m_initialized ( true )
2024-04-11 22:43:39 +02:00
, m_time_stamp ( HighResolutionTime : : current_high_resolution_time ( HTML : : relevant_global_object ( * this ) ) )
2022-08-08 22:29:40 +02:00
{
}
2023-08-07 08:41:28 +02:00
void Event : : initialize ( JS : : Realm & realm )
2023-01-10 06:56:59 -05:00
{
2023-08-07 08:41:28 +02:00
Base : : initialize ( realm ) ;
2024-03-16 13:13:08 +01:00
WEB_SET_PROTOTYPE_FOR_INTERFACE ( Event ) ;
2023-01-10 06:56:59 -05:00
}
2022-09-13 16:05:51 +02:00
void Event : : visit_edges ( Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
2023-11-19 16:18:00 +13:00
visitor . visit ( m_target ) ;
visitor . visit ( m_related_target ) ;
visitor . visit ( m_current_target ) ;
2022-09-13 16:05:51 +02:00
for ( auto & it : m_path ) {
2023-11-19 16:18:00 +13:00
visitor . visit ( it . invocation_target ) ;
visitor . visit ( it . shadow_adjusted_target ) ;
visitor . visit ( it . related_target ) ;
2024-04-15 13:58:21 +02:00
visitor . visit ( it . touch_target_list ) ;
2022-09-13 16:05:51 +02:00
}
2024-04-15 13:58:21 +02:00
visitor . visit ( m_touch_target_list ) ;
2022-09-13 16:05:51 +02:00
}
2022-04-19 23:58:24 +02:00
// https://dom.spec.whatwg.org/#concept-event-path-append
2024-11-15 04:01:23 +13:00
void Event : : append_to_path ( EventTarget & invocation_target , GC : : Ptr < EventTarget > shadow_adjusted_target , GC : : Ptr < EventTarget > related_target , TouchTargetList & touch_targets , bool slot_in_closed_tree )
2020-11-21 18:32:39 +00:00
{
2022-04-19 23:58:24 +02:00
// 1. Let invocationTargetInShadowTree be false.
2020-11-21 18:32:39 +00:00
bool invocation_target_in_shadow_tree = false ;
2022-04-19 23:58:24 +02:00
// 3. Let root-of-closed-tree be false.
2020-11-21 18:32:39 +00:00
bool root_of_closed_tree = false ;
2022-04-19 23:58:24 +02:00
// 2. If invocationTarget is a node and its root is a shadow root, then set invocationTargetInShadowTree to true.
2020-11-21 18:32:39 +00:00
if ( is < Node > ( invocation_target ) ) {
2025-01-21 09:12:05 -05:00
auto & invocation_target_node = as < Node > ( invocation_target ) ;
2021-01-01 18:12:33 +01:00
if ( is < ShadowRoot > ( invocation_target_node . root ( ) ) )
2020-11-21 18:32:39 +00:00
invocation_target_in_shadow_tree = true ;
if ( is < ShadowRoot > ( invocation_target_node ) ) {
2025-01-21 09:12:05 -05:00
auto & invocation_target_shadow_root = as < ShadowRoot > ( invocation_target_node ) ;
2022-04-19 23:58:24 +02:00
// 4. If invocationTarget is a shadow root whose mode is "closed", then set root-of-closed-tree to true.
2023-01-28 20:31:56 +01:00
root_of_closed_tree = invocation_target_shadow_root . mode ( ) = = Bindings : : ShadowRootMode : : Closed ;
2020-11-21 18:32:39 +00:00
}
}
2022-04-19 23:58:24 +02:00
// 5. Append a new struct to event’ s path whose invocation target is invocationTarget, invocation-target-in-shadow-tree is invocationTargetInShadowTree,
// shadow-adjusted target is shadowAdjustedTarget, relatedTarget is relatedTarget, touch target list is touchTargets, root-of-closed-tree is root-of-closed-tree,
// and slot-in-closed-tree is slot-in-closed-tree.
2020-11-21 18:32:39 +00:00
m_path . append ( { invocation_target , invocation_target_in_shadow_tree , shadow_adjusted_target , related_target , touch_targets , root_of_closed_tree , slot_in_closed_tree , m_path . size ( ) } ) ;
}
void Event : : set_cancelled_flag ( )
{
if ( m_cancelable & & ! m_in_passive_listener )
m_cancelled = true ;
}
2021-04-10 20:11:04 +01:00
// https://dom.spec.whatwg.org/#concept-event-initialize
2023-04-06 16:12:33 +02:00
void Event : : initialize_event ( String const & type , bool bubbles , bool cancelable )
2021-04-10 20:11:04 +01:00
{
2022-04-19 23:58:24 +02:00
// 1. Set event’ s initialized flag.
2021-04-10 20:11:04 +01:00
m_initialized = true ;
2022-04-19 23:58:24 +02:00
// 2. Unset event’ s stop propagation flag, stop immediate propagation flag, and canceled flag.
2021-04-10 20:11:04 +01:00
m_stop_propagation = false ;
m_stop_immediate_propagation = false ;
m_cancelled = false ;
2022-04-19 23:58:24 +02:00
// 3. Set event’ s isTrusted attribute to false.
2021-04-10 20:11:04 +01:00
m_is_trusted = false ;
2022-04-19 23:58:24 +02:00
// 4. Set event’ s target to null.
2021-04-10 20:11:04 +01:00
m_target = nullptr ;
2022-04-19 23:58:24 +02:00
// 5. Set event’ s type attribute to type.
2021-04-10 20:11:04 +01:00
m_type = type ;
2022-04-19 23:58:24 +02:00
// 6. Set event’ s bubbles attribute to bubbles.
2021-04-10 20:11:04 +01:00
m_bubbles = bubbles ;
2022-04-19 23:58:24 +02:00
// 8. Set event’ s cancelable attribute to cancelable.
2021-04-10 20:11:04 +01:00
m_cancelable = cancelable ;
}
// https://dom.spec.whatwg.org/#dom-event-initevent
2023-04-06 16:12:33 +02:00
void Event : : init_event ( String const & type , bool bubbles , bool cancelable )
2021-04-10 20:11:04 +01:00
{
2022-04-19 23:58:24 +02:00
// 1. If this’ s dispatch flag is set, then return.
2021-04-10 20:11:04 +01:00
if ( m_dispatch )
return ;
2022-04-19 23:58:24 +02:00
// 2. Initialize this with type, bubbles, and cancelable.
2022-08-08 22:29:40 +02:00
initialize_event ( type , bubbles , cancelable ) ;
2021-04-10 20:11:04 +01:00
}
2021-10-16 05:25:53 +01:00
// https://dom.spec.whatwg.org/#dom-event-composedpath
2024-11-15 04:01:23 +13:00
Vector < GC : : Root < EventTarget > > Event : : composed_path ( ) const
2021-10-16 05:25:53 +01:00
{
// 1. Let composedPath be an empty list.
2024-11-15 04:01:23 +13:00
Vector < GC : : Root < EventTarget > > composed_path ;
2021-10-16 05:25:53 +01:00
// 2. Let path be this’ s path. (NOTE: Not necessary)
// 3. If path is empty, then return composedPath.
if ( m_path . is_empty ( ) )
return composed_path ;
// 4. Let currentTarget be this’ s currentTarget attribute value. (NOTE: Not necessary)
2025-02-12 16:49:46 +00:00
// 5. Assert: currentTarget is an EventTarget object.
2021-10-16 05:25:53 +01:00
VERIFY ( m_current_target ) ;
2025-02-12 16:49:46 +00:00
// 6. Append currentTarget to composedPath.
// NOTE: If path is not empty, then the event is being dispatched and will have a currentTarget.
2022-08-28 13:42:07 +02:00
composed_path . append ( const_cast < EventTarget * > ( m_current_target . ptr ( ) ) ) ;
2021-10-16 05:25:53 +01:00
2025-02-12 16:49:46 +00:00
// 7. Let currentTargetIndex be 0.
2021-10-16 05:25:53 +01:00
size_t current_target_index = 0 ;
2025-02-12 16:49:46 +00:00
// 8. Let currentTargetHiddenSubtreeLevel be 0.
2021-10-16 05:25:53 +01:00
size_t current_target_hidden_subtree_level = 0 ;
2025-02-12 16:49:46 +00:00
// 9. Let index be path’ s size − 1.
// 10. While index is greater than or equal to 0:
2021-10-16 05:25:53 +01:00
for ( ssize_t index = m_path . size ( ) - 1 ; index > = 0 ; - - index ) {
auto & path_entry = m_path . at ( index ) ;
// 1. If path[index]'s root-of-closed-tree is true, then increase currentTargetHiddenSubtreeLevel by 1.
if ( path_entry . root_of_closed_tree )
+ + current_target_hidden_subtree_level ;
// 2. If path[index]'s invocation target is currentTarget, then set currentTargetIndex to index and break.
if ( path_entry . invocation_target = = m_current_target ) {
current_target_index = index ;
break ;
}
// 3. If path[index]'s slot-in-closed-tree is true, then decrease currentTargetHiddenSubtreeLevel by 1.
if ( path_entry . slot_in_closed_tree )
- - current_target_hidden_subtree_level ;
// 4. Decrease index by 1.
}
2025-02-12 16:49:46 +00:00
// 11. Let currentHiddenLevel and maxHiddenLevel be currentTargetHiddenSubtreeLevel.
2021-10-16 05:25:53 +01:00
size_t current_hidden_level = current_target_hidden_subtree_level ;
size_t max_hidden_level = current_target_hidden_subtree_level ;
2025-02-12 16:49:46 +00:00
// 12. Set index to currentTargetIndex − 1.
// 13. While index is greater than or equal to 0:
2021-10-16 05:25:53 +01:00
for ( ssize_t index = current_target_index - 1 ; index > = 0 ; - - index ) {
auto & path_entry = m_path . at ( index ) ;
// 1. If path[index]'s root-of-closed-tree is true, then increase currentHiddenLevel by 1.
if ( path_entry . root_of_closed_tree )
+ + current_hidden_level ;
// 2. If currentHiddenLevel is less than or equal to maxHiddenLevel, then prepend path[index]'s invocation target to composedPath.
if ( current_hidden_level < = max_hidden_level ) {
VERIFY ( path_entry . invocation_target ) ;
2022-08-28 13:42:07 +02:00
composed_path . prepend ( const_cast < EventTarget * > ( path_entry . invocation_target . ptr ( ) ) ) ;
2021-10-16 05:25:53 +01:00
}
// 3. If path[index]'s slot-in-closed-tree is true, then:
if ( path_entry . slot_in_closed_tree ) {
// 1. Decrease currentHiddenLevel by 1.
- - current_hidden_level ;
// 2. If currentHiddenLevel is less than maxHiddenLevel, then set maxHiddenLevel to currentHiddenLevel.
if ( current_hidden_level < max_hidden_level )
max_hidden_level = current_hidden_level ;
}
// 4. Decrease index by 1.
}
2025-02-12 16:49:46 +00:00
// 14. Set currentHiddenLevel and maxHiddenLevel to currentTargetHiddenSubtreeLevel.
2021-10-16 05:25:53 +01:00
current_hidden_level = current_target_hidden_subtree_level ;
max_hidden_level = current_target_hidden_subtree_level ;
2025-02-12 16:49:46 +00:00
// 15. Set index to currentTargetIndex + 1.
// 16. While index is less than path’ s size:
2021-10-16 05:25:53 +01:00
for ( size_t index = current_target_index + 1 ; index < m_path . size ( ) ; + + index ) {
auto & path_entry = m_path . at ( index ) ;
// 1. If path[index]'s slot-in-closed-tree is true, then increase currentHiddenLevel by 1.
if ( path_entry . slot_in_closed_tree )
+ + current_hidden_level ;
// 2. If currentHiddenLevel is less than or equal to maxHiddenLevel, then append path[index]'s invocation target to composedPath.
if ( current_hidden_level < = max_hidden_level ) {
VERIFY ( path_entry . invocation_target ) ;
2022-08-28 13:42:07 +02:00
composed_path . append ( const_cast < EventTarget * > ( path_entry . invocation_target . ptr ( ) ) ) ;
2021-10-16 05:25:53 +01:00
}
// 3. If path[index]'s root-of-closed-tree is true, then:
if ( path_entry . root_of_closed_tree ) {
// 1. Decrease currentHiddenLevel by 1.
- - current_hidden_level ;
// 2. If currentHiddenLevel is less than maxHiddenLevel, then set maxHiddenLevel to currentHiddenLevel.
if ( current_hidden_level < max_hidden_level )
max_hidden_level = current_hidden_level ;
}
// 4. Increase index by 1.
}
2025-02-12 16:49:46 +00:00
// 17. Return composedPath.
2021-10-16 05:25:53 +01:00
return composed_path ;
}
2020-11-21 18:32:39 +00:00
}