History.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HistoryPrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/History.h>
  10. #include <LibWeb/HTML/Navigation.h>
  11. #include <LibWeb/HTML/StructuredSerialize.h>
  12. #include <LibWeb/HTML/TraversableNavigable.h>
  13. #include <LibWeb/HTML/Window.h>
  14. namespace Web::HTML {
  15. GC_DEFINE_ALLOCATOR(History);
  16. GC::Ref<History> History::create(JS::Realm& realm, DOM::Document& document)
  17. {
  18. return realm.create<History>(realm, document);
  19. }
  20. History::History(JS::Realm& realm, DOM::Document& document)
  21. : PlatformObject(realm)
  22. , m_associated_document(document)
  23. {
  24. }
  25. History::~History() = default;
  26. void History::initialize(JS::Realm& realm)
  27. {
  28. Base::initialize(realm);
  29. WEB_SET_PROTOTYPE_FOR_INTERFACE(History);
  30. }
  31. void History::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_associated_document);
  35. visitor.visit(m_state);
  36. }
  37. // https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate
  38. // The pushState(data, unused, url) method steps are to run the shared history push/replace state steps given this, data, url, and "push".
  39. WebIDL::ExceptionOr<void> History::push_state(JS::Value data, String const&, Optional<String> const& url)
  40. {
  41. return shared_history_push_replace_state(data, url, HistoryHandlingBehavior::Push);
  42. }
  43. // https://html.spec.whatwg.org/multipage/history.html#dom-history-replacestate
  44. // The replaceState(data, unused, url) method steps are to run the shared history push/replace state steps given this, data, url, and "replace".
  45. WebIDL::ExceptionOr<void> History::replace_state(JS::Value data, String const&, Optional<String> const& url)
  46. {
  47. return shared_history_push_replace_state(data, url, HistoryHandlingBehavior::Replace);
  48. }
  49. // https://html.spec.whatwg.org/multipage/history.html#dom-history-length
  50. WebIDL::ExceptionOr<u64> History::length() const
  51. {
  52. // 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
  53. if (!m_associated_document->is_fully_active())
  54. return WebIDL::SecurityError::create(realm(), "Cannot perform length on a document that isn't fully active."_string);
  55. // 2. Return this's length.
  56. return m_length;
  57. }
  58. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-state
  59. WebIDL::ExceptionOr<JS::Value> History::state() const
  60. {
  61. // 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
  62. if (!m_associated_document->is_fully_active())
  63. return WebIDL::SecurityError::create(realm(), "Cannot perform state on a document that isn't fully active."_string);
  64. // 2. Return this's state.
  65. return m_state;
  66. }
  67. JS::Value History::unsafe_state() const
  68. {
  69. return m_state;
  70. }
  71. // https://html.spec.whatwg.org/multipage/history.html#dom-history-go
  72. WebIDL::ExceptionOr<void> History::go(WebIDL::Long delta = 0)
  73. {
  74. // 1. Let document be this's associated Document.
  75. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  76. if (!m_associated_document->is_fully_active())
  77. return WebIDL::SecurityError::create(realm(), "Cannot perform go on a document that isn't fully active."_string);
  78. VERIFY(m_associated_document->navigable());
  79. // 3. If delta is 0, then reload document's node navigable.
  80. if (delta == 0)
  81. m_associated_document->navigable()->reload();
  82. // 4. Traverse the history by a delta given document's node navigable's traversable navigable, delta, and with sourceDocument set to document.
  83. auto traversable = m_associated_document->navigable()->traversable_navigable();
  84. traversable->traverse_the_history_by_delta(delta);
  85. return {};
  86. }
  87. // https://html.spec.whatwg.org/multipage/history.html#dom-history-back
  88. WebIDL::ExceptionOr<void> History::back()
  89. {
  90. // 1. Let document be this's associated Document.
  91. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  92. // NOTE: We already did this check in `go` method, so skip the fully active check here.
  93. // 3. Traverse the history by a delta with −1 and document's browsing context.
  94. return go(-1);
  95. }
  96. // https://html.spec.whatwg.org/multipage/history.html#dom-history-forward
  97. WebIDL::ExceptionOr<void> History::forward()
  98. {
  99. // 1. Let document be this's associated Document.
  100. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  101. // NOTE: We already did this check in `go` method, so skip the fully active check here.
  102. // 3. Traverse the history by a delta with +1 and document's browsing context.
  103. return go(1);
  104. }
  105. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#can-have-its-url-rewritten
  106. bool can_have_its_url_rewritten(DOM::Document const& document, URL::URL const& target_url)
  107. {
  108. // 1. Let documentURL be document's URL.
  109. auto document_url = document.url();
  110. // 2. If targetURL and documentURL differ in their scheme, username, password, host, or port components,
  111. // then return false.
  112. if (target_url.scheme() != document_url.scheme()
  113. || target_url.username() != document_url.username()
  114. || target_url.password() != document_url.password()
  115. || target_url.host() != document_url.host()
  116. || target_url.port() != document_url.port())
  117. return false;
  118. // 3. If targetURL's scheme is an HTTP(S) scheme, then return true.
  119. // (Differences in path, query, and fragment are allowed for http: and https: URLs.)
  120. if (target_url.scheme() == "http"sv || target_url.scheme() == "https"sv)
  121. return true;
  122. // 4. If targetURL's scheme is "file", then:
  123. // (Differences in query and fragment are allowed for file: URLs.)
  124. if (target_url.scheme() == "file"sv) {
  125. // 1. If targetURL and documentURL differ in their path component, then return false.
  126. if (target_url.paths() != document_url.paths())
  127. return false;
  128. // 2. Return true.
  129. return true;
  130. }
  131. // 5. If targetURL and documentURL differ in their path component or query components, then return false.
  132. // (Only differences in fragment are allowed for other types of URLs.)
  133. if (target_url.paths() != document_url.paths() || target_url.query() != document_url.query())
  134. return false;
  135. // 6. Return true.
  136. return true;
  137. }
  138. // https://html.spec.whatwg.org/multipage/history.html#shared-history-push/replace-state-steps
  139. WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value data, Optional<String> const& url, HistoryHandlingBehavior history_handling)
  140. {
  141. auto& vm = this->vm();
  142. // 1. Let document be history's associated Document.
  143. auto& document = m_associated_document;
  144. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  145. if (!document->is_fully_active())
  146. return WebIDL::SecurityError::create(realm(), "Cannot perform pushState or replaceState on a document that isn't fully active."_string);
  147. // 3. Optionally, return. (For example, the user agent might disallow calls to these methods that are invoked on a timer,
  148. // or from event listeners that are not triggered in response to a clear user action, or that are invoked in rapid succession.)
  149. // 4. Let serializedData be StructuredSerializeForStorage(data). Rethrow any exceptions.
  150. // FIXME: Actually rethrow exceptions here once we start using the serialized data.
  151. // Throwing here on data types we don't yet serialize will regress sites that use push/replaceState.
  152. auto serialized_data_or_error = structured_serialize_for_storage(vm, data);
  153. auto serialized_data = serialized_data_or_error.is_error() ? MUST(structured_serialize_for_storage(vm, JS::js_null())) : serialized_data_or_error.release_value();
  154. // 5. Let newURL be document's URL.
  155. auto new_url = document->url();
  156. // 6. If url is not null or the empty string, then:
  157. if (url.has_value() && !url->is_empty()) {
  158. // 1. Parse url, relative to the relevant settings object of history.
  159. auto parsed_url = relevant_settings_object(*this).parse_url(url->to_byte_string());
  160. // 2. If that fails, then throw a "SecurityError" DOMException.
  161. if (!parsed_url.is_valid())
  162. return WebIDL::SecurityError::create(realm(), "Cannot pushState or replaceState to incompatible URL"_string);
  163. // 3. Set newURL to the resulting URL record.
  164. new_url = parsed_url;
  165. // 4. If document cannot have its URL rewritten to newURL, then throw a "SecurityError" DOMException.
  166. if (!can_have_its_url_rewritten(document, new_url))
  167. return WebIDL::SecurityError::create(realm(), "Cannot pushState or replaceState to incompatible URL"_string);
  168. }
  169. // 7. Let navigation be history's relevant global object's navigation API.
  170. auto navigation = verify_cast<Window>(relevant_global_object(*this)).navigation();
  171. // 8. Let continue be the result of firing a push/replace/reload navigate event at navigation
  172. // with navigationType set to historyHandling, isSameDocument set to true, destinationURL set to newURL,
  173. // and classicHistoryAPIState set to serializedData.
  174. auto navigation_type = history_handling == HistoryHandlingBehavior::Push ? Bindings::NavigationType::Push : Bindings::NavigationType::Replace;
  175. auto continue_ = navigation->fire_a_push_replace_reload_navigate_event(navigation_type, new_url, true, UserNavigationInvolvement::None, {}, {}, serialized_data);
  176. // 9. If continue is false, then return.
  177. if (!continue_)
  178. return {};
  179. // 10. Run the URL and history update steps given document and newURL, with serializedData set to
  180. // serializedData and historyHandling set to historyHandling.
  181. perform_url_and_history_update_steps(document, new_url, serialized_data, history_handling);
  182. return {};
  183. }
  184. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-scroll-restoration
  185. WebIDL::ExceptionOr<Bindings::ScrollRestoration> History::scroll_restoration() const
  186. {
  187. // 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
  188. if (!m_associated_document->is_fully_active())
  189. return WebIDL::SecurityError::create(realm(), "Cannot obtain scroll restoration mode for a document that isn't fully active."_string);
  190. // 2. Return this's node navigable's active session history entry's scroll restoration mode.
  191. auto scroll_restoration_mode = m_associated_document->navigable()->active_session_history_entry()->scroll_restoration_mode();
  192. switch (scroll_restoration_mode) {
  193. case ScrollRestorationMode::Auto:
  194. return Bindings::ScrollRestoration::Auto;
  195. case ScrollRestorationMode::Manual:
  196. return Bindings::ScrollRestoration::Manual;
  197. }
  198. VERIFY_NOT_REACHED();
  199. }
  200. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-scroll-restoration
  201. WebIDL::ExceptionOr<void> History::set_scroll_restoration(Bindings::ScrollRestoration scroll_restoration)
  202. {
  203. // 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
  204. if (!m_associated_document->is_fully_active())
  205. return WebIDL::SecurityError::create(realm(), "Cannot set scroll restoration mode for a document that isn't fully active."_string);
  206. // 2. Set this's node navigable's active session history entry's scroll restoration mode to the given value.
  207. auto active_session_history_entry = m_associated_document->navigable()->active_session_history_entry();
  208. switch (scroll_restoration) {
  209. case Bindings::ScrollRestoration::Auto:
  210. active_session_history_entry->set_scroll_restoration_mode(ScrollRestorationMode::Auto);
  211. break;
  212. case Bindings::ScrollRestoration::Manual:
  213. active_session_history_entry->set_scroll_restoration_mode(ScrollRestorationMode::Manual);
  214. break;
  215. }
  216. return {};
  217. }
  218. }