History.cpp 9.6 KB

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