NavigateEvent.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/Bindings/NavigateEventPrototype.h>
  8. #include <LibWeb/DOM/Event.h>
  9. #include <LibWeb/HTML/NavigationType.h>
  10. #include <LibWeb/HTML/StructuredSerialize.h>
  11. namespace Web::HTML {
  12. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigateeventinit
  13. struct NavigateEventInit : public DOM::EventInit {
  14. Bindings::NavigationType navigation_type = Bindings::NavigationType::Push;
  15. JS::GCPtr<NavigationDestination> destination;
  16. bool can_intercept = false;
  17. bool user_initiated = false;
  18. bool hash_change = false;
  19. JS::GCPtr<DOM::AbortSignal> signal;
  20. JS::GCPtr<XHR::FormData> form_data = nullptr;
  21. Optional<String> download_request = {};
  22. Optional<JS::Value> info;
  23. bool has_ua_visual_transition = false;
  24. };
  25. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationintercepthandler
  26. using NavigationInterceptHandler = JS::NonnullGCPtr<WebIDL::CallbackType>;
  27. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationinterceptoptions
  28. struct NavigationInterceptOptions {
  29. JS::GCPtr<WebIDL::CallbackType> handler;
  30. Optional<Bindings::NavigationFocusReset> focus_reset;
  31. Optional<Bindings::NavigationScrollBehavior> scroll;
  32. };
  33. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigateevent
  34. class NavigateEvent : public DOM::Event {
  35. WEB_PLATFORM_OBJECT(NavigateEvent, DOM::Event);
  36. public:
  37. [[nodiscard]] static JS::NonnullGCPtr<NavigateEvent> construct_impl(JS::Realm&, FlyString const& event_name, NavigateEventInit const&);
  38. // The navigationType, destination, canIntercept, userInitiated, hashChange, signal, formData,
  39. // downloadRequest, info, and hasUAVisualTransition attributes must return the values they are initialized to.
  40. Bindings::NavigationType navigation_type() const { return m_navigation_type; }
  41. JS::NonnullGCPtr<NavigationDestination> destination() const { return m_destination; }
  42. bool can_intercept() const { return m_can_intercept; }
  43. bool user_initiated() const { return m_user_initiated; }
  44. bool hash_change() const { return m_hash_change; }
  45. JS::NonnullGCPtr<DOM::AbortSignal> signal() const { return m_signal; }
  46. JS::GCPtr<XHR::FormData> form_data() const { return m_form_data; }
  47. Optional<String> download_request() const { return m_download_request; }
  48. JS::Value info() const { return m_info; }
  49. bool has_ua_visual_transition() const { return m_has_ua_visual_transition; }
  50. WebIDL::ExceptionOr<void> intercept(NavigationInterceptOptions const&);
  51. WebIDL::ExceptionOr<void> scroll();
  52. virtual ~NavigateEvent() override;
  53. JS::NonnullGCPtr<DOM::AbortController> abort_controller() const { return *m_abort_controller; }
  54. void finish(bool did_fulfill);
  55. private:
  56. NavigateEvent(JS::Realm&, FlyString const& event_name, NavigateEventInit const& event_init);
  57. virtual void initialize(JS::Realm&) override;
  58. virtual void visit_edges(Cell::Visitor&) override;
  59. WebIDL::ExceptionOr<void> perform_shared_checks();
  60. void process_scroll_behavior();
  61. void potentially_process_scroll_behavior();
  62. void potentially_reset_the_focus();
  63. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-interception-state
  64. enum class InterceptionState {
  65. None,
  66. Intercepted,
  67. Committed,
  68. Scrolled,
  69. Finished
  70. };
  71. InterceptionState m_interception_state = InterceptionState::None;
  72. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-navigation-handler-list
  73. Vector<NavigationInterceptHandler> m_navigation_handler_list;
  74. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-focusreset
  75. Optional<Bindings::NavigationFocusReset> m_focus_reset_behavior = {};
  76. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-scroll
  77. Optional<Bindings::NavigationScrollBehavior> m_scroll_behavior = {};
  78. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-abort-controller
  79. JS::GCPtr<DOM::AbortController> m_abort_controller = { nullptr };
  80. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-classic-history-api-state
  81. Optional<SerializationRecord> m_classic_history_api_state = {};
  82. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-navigationtype
  83. Bindings::NavigationType m_navigation_type = { Bindings::NavigationType::Push };
  84. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-destination
  85. JS::NonnullGCPtr<NavigationDestination> m_destination;
  86. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-canintercept
  87. bool m_can_intercept = { false };
  88. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-userinitiated
  89. bool m_user_initiated = { false };
  90. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-hashchange
  91. bool m_hash_change = { false };
  92. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-signal
  93. JS::NonnullGCPtr<DOM::AbortSignal> m_signal;
  94. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-formdata
  95. JS::GCPtr<XHR::FormData> m_form_data;
  96. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-downloadrequest
  97. Optional<String> m_download_request;
  98. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-info
  99. JS::Value m_info;
  100. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-hasuavisualtransition
  101. bool m_has_ua_visual_transition { false };
  102. };
  103. }
  104. namespace AK {
  105. template<>
  106. struct Formatter<Web::Bindings::NavigationScrollBehavior> : Formatter<StringView> {
  107. ErrorOr<void> format(FormatBuilder& builder, Web::Bindings::NavigationScrollBehavior const& value)
  108. {
  109. return Formatter<StringView>::format(builder, Web::Bindings::idl_enum_to_string(value));
  110. }
  111. };
  112. template<>
  113. struct Formatter<Web::Bindings::NavigationFocusReset> : Formatter<StringView> {
  114. ErrorOr<void> format(FormatBuilder& builder, Web::Bindings::NavigationFocusReset const& value)
  115. {
  116. return Formatter<StringView>::format(builder, Web::Bindings::idl_enum_to_string(value));
  117. }
  118. };
  119. }