History.cpp 8.9 KB

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