Event.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibWeb/DOM/Event.h>
  9. #include <LibWeb/DOM/Node.h>
  10. #include <LibWeb/DOM/ShadowRoot.h>
  11. namespace Web::DOM {
  12. // https://dom.spec.whatwg.org/#concept-event-path-append
  13. void Event::append_to_path(EventTarget& invocation_target, RefPtr<EventTarget> shadow_adjusted_target, RefPtr<EventTarget> related_target, TouchTargetList& touch_targets, bool slot_in_closed_tree)
  14. {
  15. // 1. Let invocationTargetInShadowTree be false.
  16. bool invocation_target_in_shadow_tree = false;
  17. // 3. Let root-of-closed-tree be false.
  18. bool root_of_closed_tree = false;
  19. // 2. If invocationTarget is a node and its root is a shadow root, then set invocationTargetInShadowTree to true.
  20. if (is<Node>(invocation_target)) {
  21. auto& invocation_target_node = verify_cast<Node>(invocation_target);
  22. if (is<ShadowRoot>(invocation_target_node.root()))
  23. invocation_target_in_shadow_tree = true;
  24. if (is<ShadowRoot>(invocation_target_node)) {
  25. auto& invocation_target_shadow_root = verify_cast<ShadowRoot>(invocation_target_node);
  26. // 4. If invocationTarget is a shadow root whose mode is "closed", then set root-of-closed-tree to true.
  27. root_of_closed_tree = invocation_target_shadow_root.closed();
  28. }
  29. }
  30. // 5. Append a new struct to event’s path whose invocation target is invocationTarget, invocation-target-in-shadow-tree is invocationTargetInShadowTree,
  31. // shadow-adjusted target is shadowAdjustedTarget, relatedTarget is relatedTarget, touch target list is touchTargets, root-of-closed-tree is root-of-closed-tree,
  32. // and slot-in-closed-tree is slot-in-closed-tree.
  33. 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() });
  34. }
  35. void Event::set_cancelled_flag()
  36. {
  37. if (m_cancelable && !m_in_passive_listener)
  38. m_cancelled = true;
  39. }
  40. // https://dom.spec.whatwg.org/#concept-event-initialize
  41. void Event::initialize(String const& type, bool bubbles, bool cancelable)
  42. {
  43. // 1. Set event’s initialized flag.
  44. m_initialized = true;
  45. // 2. Unset event’s stop propagation flag, stop immediate propagation flag, and canceled flag.
  46. m_stop_propagation = false;
  47. m_stop_immediate_propagation = false;
  48. m_cancelled = false;
  49. // 3. Set event’s isTrusted attribute to false.
  50. m_is_trusted = false;
  51. // 4. Set event’s target to null.
  52. m_target = nullptr;
  53. // 5. Set event’s type attribute to type.
  54. m_type = type;
  55. // 6. Set event’s bubbles attribute to bubbles.
  56. m_bubbles = bubbles;
  57. // 8. Set event’s cancelable attribute to cancelable.
  58. m_cancelable = cancelable;
  59. }
  60. // https://dom.spec.whatwg.org/#dom-event-initevent
  61. void Event::init_event(String const& type, bool bubbles, bool cancelable)
  62. {
  63. // 1. If this’s dispatch flag is set, then return.
  64. if (m_dispatch)
  65. return;
  66. // 2. Initialize this with type, bubbles, and cancelable.
  67. initialize(type, bubbles, cancelable);
  68. }
  69. // https://dom.spec.whatwg.org/#dom-event-timestamp
  70. double Event::time_stamp() const
  71. {
  72. return m_time_stamp;
  73. }
  74. // https://dom.spec.whatwg.org/#dom-event-composedpath
  75. NonnullRefPtrVector<EventTarget> Event::composed_path() const
  76. {
  77. // 1. Let composedPath be an empty list.
  78. NonnullRefPtrVector<EventTarget> composed_path;
  79. // 2. Let path be this’s path. (NOTE: Not necessary)
  80. // 3. If path is empty, then return composedPath.
  81. if (m_path.is_empty())
  82. return composed_path;
  83. // 4. Let currentTarget be this’s currentTarget attribute value. (NOTE: Not necessary)
  84. // 5. Append currentTarget to composedPath.
  85. // NOTE: If path is not empty, then the event is being dispatched and will have a currentTarget.
  86. VERIFY(m_current_target);
  87. composed_path.append(*m_current_target);
  88. // 6. Let currentTargetIndex be 0.
  89. size_t current_target_index = 0;
  90. // 7. Let currentTargetHiddenSubtreeLevel be 0.
  91. size_t current_target_hidden_subtree_level = 0;
  92. // 8. Let index be path’s size − 1.
  93. // 9. While index is greater than or equal to 0:
  94. for (ssize_t index = m_path.size() - 1; index >= 0; --index) {
  95. auto& path_entry = m_path.at(index);
  96. // 1. If path[index]'s root-of-closed-tree is true, then increase currentTargetHiddenSubtreeLevel by 1.
  97. if (path_entry.root_of_closed_tree)
  98. ++current_target_hidden_subtree_level;
  99. // 2. If path[index]'s invocation target is currentTarget, then set currentTargetIndex to index and break.
  100. if (path_entry.invocation_target == m_current_target) {
  101. current_target_index = index;
  102. break;
  103. }
  104. // 3. If path[index]'s slot-in-closed-tree is true, then decrease currentTargetHiddenSubtreeLevel by 1.
  105. if (path_entry.slot_in_closed_tree)
  106. --current_target_hidden_subtree_level;
  107. // 4. Decrease index by 1.
  108. }
  109. // 10. Let currentHiddenLevel and maxHiddenLevel be currentTargetHiddenSubtreeLevel.
  110. size_t current_hidden_level = current_target_hidden_subtree_level;
  111. size_t max_hidden_level = current_target_hidden_subtree_level;
  112. // 11. Set index to currentTargetIndex − 1.
  113. // 12. While index is greater than or equal to 0:
  114. for (ssize_t index = current_target_index - 1; index >= 0; --index) {
  115. auto& path_entry = m_path.at(index);
  116. // 1. If path[index]'s root-of-closed-tree is true, then increase currentHiddenLevel by 1.
  117. if (path_entry.root_of_closed_tree)
  118. ++current_hidden_level;
  119. // 2. If currentHiddenLevel is less than or equal to maxHiddenLevel, then prepend path[index]'s invocation target to composedPath.
  120. if (current_hidden_level <= max_hidden_level) {
  121. VERIFY(path_entry.invocation_target);
  122. composed_path.prepend(*path_entry.invocation_target);
  123. }
  124. // 3. If path[index]'s slot-in-closed-tree is true, then:
  125. if (path_entry.slot_in_closed_tree) {
  126. // 1. Decrease currentHiddenLevel by 1.
  127. --current_hidden_level;
  128. // 2. If currentHiddenLevel is less than maxHiddenLevel, then set maxHiddenLevel to currentHiddenLevel.
  129. if (current_hidden_level < max_hidden_level)
  130. max_hidden_level = current_hidden_level;
  131. }
  132. // 4. Decrease index by 1.
  133. }
  134. // 13. Set currentHiddenLevel and maxHiddenLevel to currentTargetHiddenSubtreeLevel.
  135. current_hidden_level = current_target_hidden_subtree_level;
  136. max_hidden_level = current_target_hidden_subtree_level;
  137. // 14. Set index to currentTargetIndex + 1.
  138. // 15. While index is less than path’s size:
  139. for (size_t index = current_target_index + 1; index < m_path.size(); ++index) {
  140. auto& path_entry = m_path.at(index);
  141. // 1. If path[index]'s slot-in-closed-tree is true, then increase currentHiddenLevel by 1.
  142. if (path_entry.slot_in_closed_tree)
  143. ++current_hidden_level;
  144. // 2. If currentHiddenLevel is less than or equal to maxHiddenLevel, then append path[index]'s invocation target to composedPath.
  145. if (current_hidden_level <= max_hidden_level) {
  146. VERIFY(path_entry.invocation_target);
  147. composed_path.append(*path_entry.invocation_target);
  148. }
  149. // 3. If path[index]'s root-of-closed-tree is true, then:
  150. if (path_entry.root_of_closed_tree) {
  151. // 1. Decrease currentHiddenLevel by 1.
  152. --current_hidden_level;
  153. // 2. If currentHiddenLevel is less than maxHiddenLevel, then set maxHiddenLevel to currentHiddenLevel.
  154. if (current_hidden_level < max_hidden_level)
  155. max_hidden_level = current_hidden_level;
  156. }
  157. // 4. Increase index by 1.
  158. }
  159. // 16. Return composedPath.
  160. return composed_path;
  161. }
  162. }