NavigateEvent.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. private:
  54. NavigateEvent(JS::Realm&, FlyString const& event_name, NavigateEventInit const& event_init);
  55. virtual void initialize(JS::Realm&) override;
  56. virtual void visit_edges(Cell::Visitor&) override;
  57. WebIDL::ExceptionOr<void> perform_shared_checks();
  58. void process_scroll_behavior();
  59. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-interception-state
  60. enum class InterceptionState {
  61. None,
  62. Intercepted,
  63. Committed,
  64. Scrolled,
  65. Finished
  66. };
  67. InterceptionState m_interception_state = InterceptionState::None;
  68. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-navigation-handler-list
  69. Vector<NavigationInterceptHandler> m_navigation_handler_list;
  70. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-focusreset
  71. Optional<Bindings::NavigationFocusReset> m_focus_reset_behavior = {};
  72. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-scroll
  73. Optional<Bindings::NavigationScrollBehavior> m_scroll_behavior = {};
  74. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-abort-controller
  75. JS::GCPtr<DOM::AbortController> m_abort_controller = { nullptr };
  76. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigateevent-classic-history-api-state
  77. Optional<SerializationRecord> m_classic_history_api_state = {};
  78. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-navigationtype
  79. Bindings::NavigationType m_navigation_type = { Bindings::NavigationType::Push };
  80. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-destination
  81. JS::NonnullGCPtr<NavigationDestination> m_destination;
  82. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-canintercept
  83. bool m_can_intercept = { false };
  84. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-userinitiated
  85. bool m_user_initiated = { false };
  86. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-hashchange
  87. bool m_hash_change = { false };
  88. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-signal
  89. JS::NonnullGCPtr<DOM::AbortSignal> m_signal;
  90. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-formdata
  91. JS::GCPtr<XHR::FormData> m_form_data;
  92. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-downloadrequest
  93. Optional<String> m_download_request;
  94. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-info
  95. JS::Value m_info;
  96. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-hasuavisualtransition
  97. bool m_has_ua_visual_transition { false };
  98. };
  99. }
  100. namespace AK {
  101. template<>
  102. struct Formatter<Web::Bindings::NavigationScrollBehavior> : Formatter<StringView> {
  103. ErrorOr<void> format(FormatBuilder& builder, Web::Bindings::NavigationScrollBehavior const& value)
  104. {
  105. return Formatter<StringView>::format(builder, Web::Bindings::idl_enum_to_string(value));
  106. }
  107. };
  108. template<>
  109. struct Formatter<Web::Bindings::NavigationFocusReset> : Formatter<StringView> {
  110. ErrorOr<void> format(FormatBuilder& builder, Web::Bindings::NavigationFocusReset const& value)
  111. {
  112. return Formatter<StringView>::format(builder, Web::Bindings::idl_enum_to_string(value));
  113. }
  114. };
  115. }