NavigateEvent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Console.h>
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibJS/Runtime/ConsoleObject.h>
  9. #include <LibJS/Runtime/Promise.h>
  10. #include <LibJS/Runtime/Realm.h>
  11. #include <LibWeb/Bindings/Intrinsics.h>
  12. #include <LibWeb/Bindings/NavigateEventPrototype.h>
  13. #include <LibWeb/DOM/AbortController.h>
  14. #include <LibWeb/DOM/AbortSignal.h>
  15. #include <LibWeb/DOM/Document.h>
  16. #include <LibWeb/HTML/Focus.h>
  17. #include <LibWeb/HTML/NavigateEvent.h>
  18. #include <LibWeb/HTML/Navigation.h>
  19. #include <LibWeb/HTML/NavigationDestination.h>
  20. #include <LibWeb/XHR/FormData.h>
  21. namespace Web::HTML {
  22. JS::NonnullGCPtr<NavigateEvent> NavigateEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, NavigateEventInit const& event_init)
  23. {
  24. return realm.heap().allocate<NavigateEvent>(realm, realm, event_name, event_init);
  25. }
  26. NavigateEvent::NavigateEvent(JS::Realm& realm, FlyString const& event_name, NavigateEventInit const& event_init)
  27. : DOM::Event(realm, event_name, event_init)
  28. , m_navigation_type(event_init.navigation_type)
  29. , m_destination(*event_init.destination)
  30. , m_can_intercept(event_init.can_intercept)
  31. , m_user_initiated(event_init.user_initiated)
  32. , m_hash_change(event_init.hash_change)
  33. , m_signal(*event_init.signal)
  34. , m_form_data(event_init.form_data)
  35. , m_download_request(event_init.download_request)
  36. , m_info(event_init.info.value_or(JS::js_undefined()))
  37. , m_has_ua_visual_transition(event_init.has_ua_visual_transition)
  38. {
  39. }
  40. NavigateEvent::~NavigateEvent() = default;
  41. void NavigateEvent::initialize(JS::Realm& realm)
  42. {
  43. Base::initialize(realm);
  44. set_prototype(&Bindings::ensure_web_prototype<Bindings::NavigateEventPrototype>(realm, "NavigateEvent"));
  45. }
  46. void NavigateEvent::visit_edges(JS::Cell::Visitor& visitor)
  47. {
  48. Base::visit_edges(visitor);
  49. for (auto& handler : m_navigation_handler_list)
  50. visitor.visit(handler);
  51. visitor.visit(m_abort_controller);
  52. visitor.visit(m_destination);
  53. visitor.visit(m_signal);
  54. visitor.visit(m_form_data);
  55. visitor.visit(m_info);
  56. }
  57. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-intercept
  58. WebIDL::ExceptionOr<void> NavigateEvent::intercept(NavigationInterceptOptions const& options)
  59. {
  60. auto& realm = this->realm();
  61. auto& vm = this->vm();
  62. // The intercept(options) method steps are:
  63. // 1. Perform shared checks given this.
  64. TRY(perform_shared_checks());
  65. // 2. If this's canIntercept attribute was initialized to false, then throw a "SecurityError" DOMException.
  66. if (!m_can_intercept)
  67. return WebIDL::SecurityError::create(realm, "NavigateEvent cannot be intercepted"_fly_string);
  68. // 3. If this's dispatch flag is unset, then throw an "InvalidStateError" DOMException.
  69. if (!this->dispatched())
  70. return WebIDL::InvalidStateError::create(realm, "NavigationEvent is not dispatched yet"_fly_string);
  71. // 4. Assert: this's interception state is either "none" or "intercepted".
  72. VERIFY(m_interception_state == InterceptionState::None || m_interception_state == InterceptionState::Intercepted);
  73. // 5. Set this's interception state to "intercepted".
  74. m_interception_state = InterceptionState::Intercepted;
  75. // 6. If options["handler"] exists, then append it to this's navigation handler list.
  76. if (options.handler != nullptr)
  77. TRY_OR_THROW_OOM(vm, m_navigation_handler_list.try_append(*options.handler));
  78. // 7. If options["focusReset"] exists, then:
  79. if (options.focus_reset.has_value()) {
  80. // 1. If this's focus reset behavior is not null, and it is not equal to options["focusReset"],
  81. // then the user agent may report a warning to the console indicating that the focusReset option
  82. // for a previous call to intercept() was overridden by this new value, and the previous value
  83. // will be ignored.
  84. if (m_focus_reset_behavior.has_value() && *m_focus_reset_behavior != *options.focus_reset) {
  85. auto& console = realm.intrinsics().console_object()->console();
  86. console.output_debug_message(JS::Console::LogLevel::Warn,
  87. TRY_OR_THROW_OOM(vm, String::formatted("focusReset behavior on NavigationEvent overriden (was: {}, now: {})", *m_focus_reset_behavior, *options.focus_reset)));
  88. }
  89. // 2. Set this's focus reset behavior to options["focusReset"].
  90. m_focus_reset_behavior = options.focus_reset;
  91. }
  92. // 8. If options["scroll"] exists, then:
  93. if (options.scroll.has_value()) {
  94. // 1. If this's scroll behavior is not null, and it is not equal to options["scroll"], then the user
  95. // agent may report a warning to the console indicating that the scroll option for a previous call
  96. // to intercept() was overridden by this new value, and the previous value will be ignored.
  97. if (m_scroll_behavior.has_value() && *m_scroll_behavior != *options.scroll) {
  98. auto& console = realm.intrinsics().console_object()->console();
  99. console.output_debug_message(JS::Console::LogLevel::Warn,
  100. TRY_OR_THROW_OOM(vm, String::formatted("scroll option on NavigationEvent overriden (was: {}, now: {})", *m_scroll_behavior, *options.scroll)));
  101. }
  102. // 2. Set this's scroll behavior to options["scroll"].
  103. m_scroll_behavior = options.scroll;
  104. }
  105. return {};
  106. }
  107. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-scroll
  108. WebIDL::ExceptionOr<void> NavigateEvent::scroll()
  109. {
  110. // The scroll() method steps are:
  111. // 1. Perform shared checks given this.
  112. TRY(perform_shared_checks());
  113. // 2. If this's interception state is not "committed", then throw an "InvalidStateError" DOMException.
  114. if (m_interception_state != InterceptionState::Committed)
  115. return WebIDL::InvalidStateError::create(realm(), "Cannot scroll NavigationEvent that is not committed"_fly_string);
  116. // 3. Process scroll behavior given this.
  117. process_scroll_behavior();
  118. return {};
  119. }
  120. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigateevent-perform-shared-checks
  121. WebIDL::ExceptionOr<void> NavigateEvent::perform_shared_checks()
  122. {
  123. // To perform shared checks for a NavigateEvent event:
  124. // 1. If event's relevant global object's associated Document is not fully active,
  125. // then throw an "InvalidStateError" DOMException.
  126. auto& associated_document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  127. if (!associated_document.is_fully_active())
  128. return WebIDL::InvalidStateError::create(realm(), "Document is not fully active"_fly_string);
  129. // 2. If event's isTrusted attribute was initialized to false, then throw a "SecurityError" DOMException.
  130. if (!this->is_trusted())
  131. return WebIDL::SecurityError::create(realm(), "NavigateEvent is not trusted"_fly_string);
  132. // 3. If event's canceled flag is set, then throw an "InvalidStateError" DOMException.
  133. if (this->cancelled())
  134. return WebIDL::InvalidStateError::create(realm(), "NavigateEvent already cancelled"_fly_string);
  135. return {};
  136. }
  137. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#process-scroll-behavior
  138. void NavigateEvent::process_scroll_behavior()
  139. {
  140. // To process scroll behavior given a NavigateEvent event:
  141. // 1. Assert: event's interception state is "committed".
  142. VERIFY(m_interception_state == InterceptionState::Committed);
  143. // 2. Set event's interception state to "scrolled".
  144. m_interception_state = InterceptionState::Scrolled;
  145. // FIXME: 3. If event's navigationType was initialized to "traverse" or "reload", then restore scroll position data
  146. // given event's relevant global object's navigable's active session history entry.
  147. if (m_navigation_type == Bindings::NavigationType::Traverse || m_navigation_type == Bindings::NavigationType::Reload) {
  148. dbgln("FIXME: restore scroll position data after traversal or reload navigation");
  149. }
  150. // 4. Otherwise:
  151. else {
  152. // 1. Let document be event's relevant global object's associated Document.
  153. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  154. // 2. If document's indicated part is null, then scroll to the beginning of the document given document. [CSSOMVIEW]
  155. auto indicated_part = document.determine_the_indicated_part();
  156. if (indicated_part.has<DOM::Element*>() && indicated_part.get<DOM::Element*>() == nullptr) {
  157. document.scroll_to_the_beginning_of_the_document();
  158. }
  159. // 3. Otherwise, scroll to the fragment given document.
  160. else {
  161. // FIXME: This will re-determine the indicated part. Can we avoid this extra work?
  162. document.scroll_to_the_fragment();
  163. }
  164. }
  165. }
  166. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#potentially-process-scroll-behavior
  167. void NavigateEvent::potentially_process_scroll_behavior()
  168. {
  169. // 1. Assert: event's interception state is "committed" or "scrolled".
  170. VERIFY(m_interception_state != InterceptionState::Committed && m_interception_state != InterceptionState::Scrolled);
  171. // 2. If event's interception state is "scrolled", then return.
  172. if (m_interception_state == InterceptionState::Scrolled)
  173. return;
  174. // 3. If event's scroll behavior is "manual", then return.
  175. // NOTE: If it was left as null, then we treat that as "after-transition", and continue onward.
  176. if (m_scroll_behavior == Bindings::NavigationScrollBehavior::Manual)
  177. return;
  178. // 4. Process scroll behavior given event.
  179. process_scroll_behavior();
  180. }
  181. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#potentially-reset-the-focus
  182. void NavigateEvent::potentially_reset_the_focus()
  183. {
  184. // 1. Assert: event's interception state is "committed" or "scrolled".
  185. VERIFY(m_interception_state == InterceptionState::Committed || m_interception_state == InterceptionState::Scrolled);
  186. // 2. Let navigation be event's relevant global object's navigation API.
  187. auto& relevant_global_object = verify_cast<Window>(HTML::relevant_global_object(*this));
  188. auto navigation = relevant_global_object.navigation();
  189. // 3. Let focusChanged be navigation's focus changed during ongoing navigation.
  190. auto focus_changed = navigation->focus_changed_during_ongoing_navigation();
  191. // 4. Set navigation's focus changed during ongoing navigation to false.
  192. navigation->set_focus_changed_during_ongoing_navigation(false);
  193. // 5. If focusChanged is true, then return.
  194. if (focus_changed)
  195. return;
  196. // 6. If event's focus reset behavior is "manual", then return.
  197. // NOTE: If it was left as null, then we treat that as "after-transition", and continue onward.
  198. if (m_focus_reset_behavior == Bindings::NavigationFocusReset::Manual)
  199. return;
  200. // 7. Let document be event's relevant global object's associated Document.
  201. auto& document = relevant_global_object.associated_document();
  202. // 8. FIXME: Let focusTarget be the autofocus delegate for document.
  203. JS::GCPtr<DOM::Node> focus_target = nullptr;
  204. // 9. If focusTarget is null, then set focusTarget to document's body element.
  205. if (focus_target == nullptr)
  206. focus_target = document.body();
  207. // 10. If focusTarget is null, then set focusTarget to document's document element.
  208. if (focus_target == nullptr)
  209. focus_target = document.document_element();
  210. // FIXME: 11. Run the focusing steps for focusTarget, with document's viewport as the fallback target.
  211. run_focusing_steps(focus_target, nullptr);
  212. // FIXME: 12. Move the sequential focus navigation starting point to focusTarget.
  213. }
  214. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigateevent-finish
  215. void NavigateEvent::finish(bool did_fulfill)
  216. {
  217. // 1. Assert: event's interception state is not "intercepted" or "finished".
  218. VERIFY(m_interception_state != InterceptionState::Intercepted && m_interception_state != InterceptionState::Finished);
  219. // 2. If event's interception state is "none", then return.
  220. if (m_interception_state == InterceptionState::None)
  221. return;
  222. // 3. Potentially reset the focus given event.
  223. potentially_reset_the_focus();
  224. // 4. If didFulfill is true, then potentially process scroll behavior given event.
  225. if (did_fulfill)
  226. potentially_process_scroll_behavior();
  227. // 5. Set event's interception state to "finished".
  228. m_interception_state = InterceptionState::Finished;
  229. }
  230. }