NavigateEvent.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/NavigateEvent.h>
  17. #include <LibWeb/HTML/NavigationDestination.h>
  18. #include <LibWeb/XHR/FormData.h>
  19. namespace Web::HTML {
  20. JS::NonnullGCPtr<NavigateEvent> NavigateEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, NavigateEventInit const& event_init)
  21. {
  22. return realm.heap().allocate<NavigateEvent>(realm, realm, event_name, event_init);
  23. }
  24. NavigateEvent::NavigateEvent(JS::Realm& realm, FlyString const& event_name, NavigateEventInit const& event_init)
  25. : DOM::Event(realm, event_name, event_init)
  26. , m_navigation_type(event_init.navigation_type)
  27. , m_destination(*event_init.destination)
  28. , m_can_intercept(event_init.can_intercept)
  29. , m_user_initiated(event_init.user_initiated)
  30. , m_hash_change(event_init.hash_change)
  31. , m_signal(*event_init.signal)
  32. , m_form_data(event_init.form_data)
  33. , m_download_request(event_init.download_request)
  34. , m_info(event_init.info.value_or(JS::js_undefined()))
  35. , m_has_ua_visual_transition(event_init.has_ua_visual_transition)
  36. {
  37. }
  38. NavigateEvent::~NavigateEvent() = default;
  39. void NavigateEvent::initialize(JS::Realm& realm)
  40. {
  41. Base::initialize(realm);
  42. set_prototype(&Bindings::ensure_web_prototype<Bindings::NavigateEventPrototype>(realm, "NavigateEvent"));
  43. }
  44. void NavigateEvent::visit_edges(JS::Cell::Visitor& visitor)
  45. {
  46. Base::visit_edges(visitor);
  47. for (auto& handler : m_navigation_handler_list)
  48. visitor.visit(handler);
  49. visitor.visit(m_abort_controller);
  50. visitor.visit(m_destination);
  51. visitor.visit(m_signal);
  52. visitor.visit(m_form_data);
  53. visitor.visit(m_info);
  54. }
  55. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-intercept
  56. WebIDL::ExceptionOr<void> NavigateEvent::intercept(NavigationInterceptOptions const& options)
  57. {
  58. auto& realm = this->realm();
  59. auto& vm = this->vm();
  60. // The intercept(options) method steps are:
  61. // 1. Perform shared checks given this.
  62. TRY(perform_shared_checks());
  63. // 2. If this's canIntercept attribute was initialized to false, then throw a "SecurityError" DOMException.
  64. if (!m_can_intercept)
  65. return WebIDL::SecurityError::create(realm, "NavigateEvent cannot be intercepted"_fly_string);
  66. // 3. If this's dispatch flag is unset, then throw an "InvalidStateError" DOMException.
  67. if (!this->dispatched())
  68. return WebIDL::InvalidStateError::create(realm, "NavigationEvent is not dispatched yet"_fly_string);
  69. // 4. Assert: this's interception state is either "none" or "intercepted".
  70. VERIFY(m_interception_state == InterceptionState::None || m_interception_state == InterceptionState::Intercepted);
  71. // 5. Set this's interception state to "intercepted".
  72. m_interception_state = InterceptionState::Intercepted;
  73. // 6. If options["handler"] exists, then append it to this's navigation handler list.
  74. if (options.handler != nullptr)
  75. TRY_OR_THROW_OOM(vm, m_navigation_handler_list.try_append(*options.handler));
  76. // 7. If options["focusReset"] exists, then:
  77. if (options.focus_reset.has_value()) {
  78. // 1. If this's focus reset behavior is not null, and it is not equal to options["focusReset"],
  79. // then the user agent may report a warning to the console indicating that the focusReset option
  80. // for a previous call to intercept() was overridden by this new value, and the previous value
  81. // will be ignored.
  82. if (m_focus_reset_behavior.has_value() && *m_focus_reset_behavior != *options.focus_reset) {
  83. auto& console = realm.intrinsics().console_object()->console();
  84. console.output_debug_message(JS::Console::LogLevel::Warn,
  85. TRY_OR_THROW_OOM(vm, String::formatted("focusReset behavior on NavigationEvent overriden (was: {}, now: {})", *m_focus_reset_behavior, *options.focus_reset)));
  86. }
  87. // 2. Set this's focus reset behavior to options["focusReset"].
  88. m_focus_reset_behavior = options.focus_reset;
  89. }
  90. // 8. If options["scroll"] exists, then:
  91. if (options.scroll.has_value()) {
  92. // 1. If this's scroll behavior is not null, and it is not equal to options["scroll"], then the user
  93. // agent may report a warning to the console indicating that the scroll option for a previous call
  94. // to intercept() was overridden by this new value, and the previous value will be ignored.
  95. if (m_scroll_behavior.has_value() && *m_scroll_behavior != *options.scroll) {
  96. auto& console = realm.intrinsics().console_object()->console();
  97. console.output_debug_message(JS::Console::LogLevel::Warn,
  98. TRY_OR_THROW_OOM(vm, String::formatted("scroll option on NavigationEvent overriden (was: {}, now: {})", *m_scroll_behavior, *options.scroll)));
  99. }
  100. // 2. Set this's scroll behavior to options["scroll"].
  101. m_scroll_behavior = options.scroll;
  102. }
  103. return {};
  104. }
  105. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-scroll
  106. WebIDL::ExceptionOr<void> NavigateEvent::scroll()
  107. {
  108. // The scroll() method steps are:
  109. // 1. Perform shared checks given this.
  110. TRY(perform_shared_checks());
  111. // 2. If this's interception state is not "committed", then throw an "InvalidStateError" DOMException.
  112. if (m_interception_state != InterceptionState::Committed)
  113. return WebIDL::InvalidStateError::create(realm(), "Cannot scroll NavigationEvent that is not committed"_fly_string);
  114. // 3. Process scroll behavior given this.
  115. process_scroll_behavior();
  116. return {};
  117. }
  118. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigateevent-perform-shared-checks
  119. WebIDL::ExceptionOr<void> NavigateEvent::perform_shared_checks()
  120. {
  121. // To perform shared checks for a NavigateEvent event:
  122. // 1. If event's relevant global object's associated Document is not fully active,
  123. // then throw an "InvalidStateError" DOMException.
  124. auto& associated_document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  125. if (!associated_document.is_fully_active())
  126. return WebIDL::InvalidStateError::create(realm(), "Document is not fully active"_fly_string);
  127. // 2. If event's isTrusted attribute was initialized to false, then throw a "SecurityError" DOMException.
  128. if (!this->is_trusted())
  129. return WebIDL::SecurityError::create(realm(), "NavigateEvent is not trusted"_fly_string);
  130. // 3. If event's canceled flag is set, then throw an "InvalidStateError" DOMException.
  131. if (this->cancelled())
  132. return WebIDL::InvalidStateError::create(realm(), "NavigateEvent already cancelled"_fly_string);
  133. return {};
  134. }
  135. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#process-scroll-behavior
  136. void NavigateEvent::process_scroll_behavior()
  137. {
  138. // To process scroll behavior given a NavigateEvent event:
  139. // 1. Assert: event's interception state is "committed".
  140. VERIFY(m_interception_state == InterceptionState::Committed);
  141. // 2. Set event's interception state to "scrolled".
  142. m_interception_state = InterceptionState::Scrolled;
  143. // FIXME: 3. If event's navigationType was initialized to "traverse" or "reload", then restore scroll position data
  144. // given event's relevant global object's navigable's active session history entry.
  145. if (m_navigation_type == Bindings::NavigationType::Traverse || m_navigation_type == Bindings::NavigationType::Reload) {
  146. dbgln("FIXME: restore scroll position data after traversal or reload navigation");
  147. }
  148. // 4. Otherwise:
  149. else {
  150. // 1. Let document be event's relevant global object's associated Document.
  151. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  152. // 2. If document's indicated part is null, then scroll to the beginning of the document given document. [CSSOMVIEW]
  153. auto indicated_part = document.determine_the_indicated_part();
  154. if (indicated_part.has<DOM::Element*>() && indicated_part.get<DOM::Element*>() == nullptr) {
  155. document.scroll_to_the_beginning_of_the_document();
  156. }
  157. // 3. Otherwise, scroll to the fragment given document.
  158. else {
  159. // FIXME: This will re-determine the indicated part. Can we avoid this extra work?
  160. document.scroll_to_the_fragment();
  161. }
  162. }
  163. }
  164. }