NavigateEvent.cpp 12 KB

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