History.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/History.h>
  9. #include <LibWeb/HTML/StructuredSerialize.h>
  10. #include <LibWeb/HTML/TraversableNavigable.h>
  11. #include <LibWeb/Page/Page.h>
  12. namespace Web::HTML {
  13. JS::NonnullGCPtr<History> History::create(JS::Realm& realm, DOM::Document& document)
  14. {
  15. return realm.heap().allocate<History>(realm, realm, document);
  16. }
  17. History::History(JS::Realm& realm, DOM::Document& document)
  18. : PlatformObject(realm)
  19. , m_associated_document(document)
  20. {
  21. }
  22. History::~History() = default;
  23. void History::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. set_prototype(&Bindings::ensure_web_prototype<Bindings::HistoryPrototype>(realm, "History"));
  27. }
  28. void History::visit_edges(Cell::Visitor& visitor)
  29. {
  30. Base::visit_edges(visitor);
  31. visitor.visit(m_associated_document.ptr());
  32. }
  33. // https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate
  34. // The pushState(data, unused, url) method steps are to run the shared history push/replace state steps given this, data, url, and "push".
  35. WebIDL::ExceptionOr<void> History::push_state(JS::Value data, String const&, Optional<String> const& url)
  36. {
  37. return shared_history_push_replace_state(data, url, HistoryHandlingBehavior::Push);
  38. }
  39. // https://html.spec.whatwg.org/multipage/history.html#dom-history-replacestate
  40. // The replaceState(data, unused, url) method steps are to run the shared history push/replace state steps given this, data, url, and "replace".
  41. WebIDL::ExceptionOr<void> History::replace_state(JS::Value data, String const&, Optional<String> const& url)
  42. {
  43. return shared_history_push_replace_state(data, url, HistoryHandlingBehavior::Replace);
  44. }
  45. // https://html.spec.whatwg.org/multipage/history.html#dom-history-length
  46. WebIDL::ExceptionOr<u64> History::length() const
  47. {
  48. // 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
  49. if (!m_associated_document->is_fully_active())
  50. return WebIDL::SecurityError::create(realm(), "Cannot perform length on a document that isn't fully active."_fly_string);
  51. // 2. Return this's length.
  52. return m_length;
  53. }
  54. // https://html.spec.whatwg.org/multipage/history.html#dom-history-go
  55. WebIDL::ExceptionOr<void> History::go(long delta = 0)
  56. {
  57. // 1. Let document be this's associated Document.
  58. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  59. if (!m_associated_document->is_fully_active())
  60. return WebIDL::SecurityError::create(realm(), "Cannot perform go on a document that isn't fully active."_fly_string);
  61. VERIFY(m_associated_document->navigable());
  62. // 3. If delta is 0, then reload document's node navigable.
  63. m_associated_document->navigable()->reload();
  64. // 4. Traverse the history by a delta given document's node navigable's traversable navigable, delta, and with sourceDocument set to document.
  65. auto traversable = m_associated_document->navigable()->traversable_navigable();
  66. traversable->traverse_the_history_by_delta(delta);
  67. return {};
  68. }
  69. // https://html.spec.whatwg.org/multipage/history.html#dom-history-back
  70. WebIDL::ExceptionOr<void> History::back()
  71. {
  72. // 1. Let document be this's associated Document.
  73. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  74. // NOTE: We already did this check in `go` method, so skip the fully active check here.
  75. // 3. Traverse the history by a delta with −1 and document's browsing context.
  76. return go(-1);
  77. }
  78. // https://html.spec.whatwg.org/multipage/history.html#dom-history-forward
  79. WebIDL::ExceptionOr<void> History::forward()
  80. {
  81. // 1. Let document be this's associated Document.
  82. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  83. // NOTE: We already did this check in `go` method, so skip the fully active check here.
  84. // 3. Traverse the history by a delta with +1 and document's browsing context.
  85. return go(1);
  86. }
  87. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#can-have-its-url-rewritten
  88. bool can_have_its_url_rewritten(DOM::Document const& document, AK::URL const& target_url)
  89. {
  90. // 1. Let documentURL be document's URL.
  91. auto document_url = document.url();
  92. // 2. If targetURL and documentURL differ in their scheme, username, password, host, or port components,
  93. // then return false.
  94. if (target_url.scheme() != document_url.scheme()
  95. || target_url.raw_username() != document_url.raw_username()
  96. || target_url.raw_password() != document_url.raw_password()
  97. || target_url.host() != document_url.host()
  98. || target_url.port() != document_url.port())
  99. return false;
  100. // 3. If targetURL's scheme is an HTTP(S) scheme, then return true.
  101. // (Differences in path, query, and fragment are allowed for http: and https: URLs.)
  102. if (target_url.scheme() == "http"sv || target_url.scheme() == "https"sv)
  103. return true;
  104. // 4. If targetURL's scheme is "file", and targetURL and documentURL differ in their path component,
  105. // then return false. (Differences in query and fragment are allowed for file: URLs.)
  106. // FIXME: Don't create temporary strings to compare paths
  107. auto target_url_path = target_url.serialize_path();
  108. auto document_url_path = document_url.serialize_path();
  109. if (target_url.scheme() == "file"sv
  110. && target_url_path != document_url_path)
  111. return false;
  112. // 5. If targetURL and documentURL differ in their path component or query components, then return false.
  113. // (Only differences in fragment are allowed for other types of URLs.)
  114. if (target_url_path != document_url_path
  115. || target_url.query() != document_url.query())
  116. return false;
  117. // 6. Return true.
  118. return true;
  119. }
  120. // https://html.spec.whatwg.org/multipage/history.html#shared-history-push/replace-state-steps
  121. WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value value, Optional<String> const& url, HistoryHandlingBehavior history_handling)
  122. {
  123. // 1. Let document be history's associated Document.
  124. auto& document = m_associated_document;
  125. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  126. if (!document->is_fully_active())
  127. return WebIDL::SecurityError::create(realm(), "Cannot perform pushState or replaceState on a document that isn't fully active."_fly_string);
  128. // 3. Optionally, return. (For example, the user agent might disallow calls to these methods that are invoked on a timer,
  129. // or from event listeners that are not triggered in response to a clear user action, or that are invoked in rapid succession.)
  130. // 4. Let serializedData be StructuredSerializeForStorage(data). Rethrow any exceptions.
  131. // FIXME: Actually rethrow exceptions here once we start using the serialized data.
  132. // Throwing here on data types we don't yet serialize will regress sites that use push/replaceState.
  133. [[maybe_unused]] auto serialized_data_or_error = structured_serialize_for_storage(vm(), value);
  134. // 5. Let newURL be document's URL.
  135. auto new_url = document->url();
  136. // 6. If url is not null or the empty string, then:
  137. if (url.has_value() && !url->is_empty()) {
  138. // 1. Parse url, relative to the relevant settings object of history.
  139. auto parsed_url = relevant_settings_object(*this).parse_url(url->to_deprecated_string());
  140. // 2. If that fails, then throw a "SecurityError" DOMException.
  141. if (!parsed_url.is_valid())
  142. return WebIDL::SecurityError::create(realm(), "Cannot pushState or replaceState to incompatible URL"_fly_string);
  143. // 3. Set newURL to the resulting URL record.
  144. new_url = parsed_url;
  145. // 4. If document cannot have its URL rewritten to newURL, then throw a "SecurityError" DOMException.
  146. if (!can_have_its_url_rewritten(document, new_url))
  147. return WebIDL::SecurityError::create(realm(), "Cannot pushState or replaceState to incompatible URL"_fly_string);
  148. }
  149. // FIXME: 7. Let navigation be history's relevant global object's navigation API.
  150. // FIXME: 8. Let continue be the result of firing a push/replace/reload navigate event at navigation
  151. /// with navigationType set to historyHandling, isSameDocument set to true, destinationURL set to newURL,
  152. // and classicHistoryAPIState set to serializedData.
  153. // FIXME: 9. If continue is false, then return.
  154. auto navigable = document->navigable();
  155. if (navigable->is_top_level_traversable()) {
  156. if (auto* page = navigable->active_browsing_context()->page())
  157. page->client().page_did_start_loading(new_url, false);
  158. }
  159. // 10. Run the URL and history update steps given document and newURL, with serializedData set to
  160. // serializedData and historyHandling set to historyHandling.
  161. perform_url_and_history_update_steps(document, new_url, history_handling);
  162. return {};
  163. }
  164. }