2021-09-11 23:43:34 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
2025-04-11 17:24:50 +01:00
|
|
|
|
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
|
2021-09-11 23:43:34 +01:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/HistoryPrototype.h>
|
2022-09-25 16:38:21 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2021-09-11 23:43:34 +01:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
|
|
|
|
#include <LibWeb/HTML/History.h>
|
2024-08-06 14:18:40 +02:00
|
|
|
|
#include <LibWeb/HTML/Navigation.h>
|
2023-08-21 17:44:42 -06:00
|
|
|
|
#include <LibWeb/HTML/StructuredSerialize.h>
|
2023-08-24 23:30:21 +02:00
|
|
|
|
#include <LibWeb/HTML/TraversableNavigable.h>
|
2024-08-06 14:18:40 +02:00
|
|
|
|
#include <LibWeb/HTML/Window.h>
|
2021-09-11 23:43:34 +01:00
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(History);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2025-04-11 17:24:50 +01:00
|
|
|
|
GC::Ref<History> History::create(JS::Realm& realm)
|
2021-09-11 23:43:34 +01:00
|
|
|
|
{
|
2025-04-11 17:24:50 +01:00
|
|
|
|
return realm.create<History>(realm);
|
2022-09-02 13:50:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 17:24:50 +01:00
|
|
|
|
History::History(JS::Realm& realm)
|
2022-09-25 16:38:21 -06:00
|
|
|
|
: PlatformObject(realm)
|
2022-09-02 13:50:24 +02:00
|
|
|
|
{
|
2021-09-11 23:43:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
|
History::~History() = default;
|
2021-09-11 23:43:34 +01:00
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void History::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(History);
|
2025-04-20 16:22:57 +02:00
|
|
|
|
Base::initialize(realm);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 13:50:24 +02:00
|
|
|
|
void History::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2023-09-27 22:39:44 -06:00
|
|
|
|
visitor.visit(m_state);
|
2022-09-02 13:50:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 23:43:34 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate
|
2023-09-23 23:04:24 +02:00
|
|
|
|
// The pushState(data, unused, url) method steps are to run the shared history push/replace state steps given this, data, url, and "push".
|
2023-08-26 17:03:52 +12:00
|
|
|
|
WebIDL::ExceptionOr<void> History::push_state(JS::Value data, String const&, Optional<String> const& url)
|
2021-09-11 23:43:34 +01:00
|
|
|
|
{
|
2023-09-23 23:04:24 +02:00
|
|
|
|
return shared_history_push_replace_state(data, url, HistoryHandlingBehavior::Push);
|
2021-09-11 23:43:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/history.html#dom-history-replacestate
|
2023-09-23 23:04:24 +02:00
|
|
|
|
// The replaceState(data, unused, url) method steps are to run the shared history push/replace state steps given this, data, url, and "replace".
|
2023-08-26 17:03:52 +12:00
|
|
|
|
WebIDL::ExceptionOr<void> History::replace_state(JS::Value data, String const&, Optional<String> const& url)
|
2021-09-11 23:43:34 +01:00
|
|
|
|
{
|
2023-09-23 23:04:24 +02:00
|
|
|
|
return shared_history_push_replace_state(data, url, HistoryHandlingBehavior::Replace);
|
2021-09-11 23:43:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 22:43:04 +08:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/history.html#dom-history-length
|
|
|
|
|
WebIDL::ExceptionOr<u64> History::length() const
|
|
|
|
|
{
|
2023-09-23 22:57:04 +02:00
|
|
|
|
// 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
|
2025-04-11 17:24:50 +01:00
|
|
|
|
if (!as<Window>(relevant_global_object(*this)).associated_document().is_fully_active())
|
2024-10-12 20:56:21 +02:00
|
|
|
|
return WebIDL::SecurityError::create(realm(), "Cannot perform length on a document that isn't fully active."_string);
|
2022-10-13 22:43:04 +08:00
|
|
|
|
|
2023-09-23 22:57:04 +02:00
|
|
|
|
// 2. Return this's length.
|
|
|
|
|
return m_length;
|
2022-10-13 22:43:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 22:39:44 -06:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-state
|
|
|
|
|
WebIDL::ExceptionOr<JS::Value> History::state() const
|
|
|
|
|
{
|
|
|
|
|
// 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
|
2025-04-11 17:24:50 +01:00
|
|
|
|
if (!as<Window>(relevant_global_object(*this)).associated_document().is_fully_active())
|
2024-10-12 20:56:21 +02:00
|
|
|
|
return WebIDL::SecurityError::create(realm(), "Cannot perform state on a document that isn't fully active."_string);
|
2023-09-27 22:39:44 -06:00
|
|
|
|
|
|
|
|
|
// 2. Return this's state.
|
|
|
|
|
return m_state;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 21:25:31 -07:00
|
|
|
|
JS::Value History::unsafe_state() const
|
|
|
|
|
{
|
|
|
|
|
return m_state;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#delta-traverse
|
|
|
|
|
WebIDL::ExceptionOr<void> History::delta_traverse(WebIDL::Long delta)
|
2022-10-13 22:43:04 +08:00
|
|
|
|
{
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// 1. Let document be history's relevant global object's associated Document.
|
|
|
|
|
auto& document = as<Window>(relevant_global_object(*this)).associated_document();
|
2022-10-13 22:43:04 +08:00
|
|
|
|
|
|
|
|
|
// 2. If document is not fully active, then throw a "SecurityError" DOMException.
|
2025-04-11 17:24:50 +01:00
|
|
|
|
if (!document.is_fully_active())
|
2024-10-12 20:56:21 +02:00
|
|
|
|
return WebIDL::SecurityError::create(realm(), "Cannot perform go on a document that isn't fully active."_string);
|
2022-10-13 22:43:04 +08:00
|
|
|
|
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// 3. If delta is 0, then reload document's node navigable, and return.
|
|
|
|
|
if (delta == 0) {
|
|
|
|
|
VERIFY(document.navigable());
|
|
|
|
|
document.navigable()->reload();
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2023-08-24 23:30:21 +02:00
|
|
|
|
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// 4. Traverse the history by a delta given document's node navigable's traversable navigable, delta, and with
|
|
|
|
|
// sourceDocument set to document.
|
|
|
|
|
document.navigable()->traversable_navigable()->traverse_the_history_by_delta(delta, document);
|
2022-10-13 22:43:04 +08:00
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/history.html#dom-history-go
|
|
|
|
|
WebIDL::ExceptionOr<void> History::go(WebIDL::Long delta = 0)
|
|
|
|
|
{
|
|
|
|
|
// The go(delta) method steps are to delta traverse this given delta.
|
|
|
|
|
return delta_traverse(delta);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 22:43:04 +08:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/history.html#dom-history-back
|
|
|
|
|
WebIDL::ExceptionOr<void> History::back()
|
|
|
|
|
{
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// The back() method steps are to delta traverse this given −1.
|
|
|
|
|
return delta_traverse(-1);
|
2022-10-13 22:43:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/history.html#dom-history-forward
|
|
|
|
|
WebIDL::ExceptionOr<void> History::forward()
|
|
|
|
|
{
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// The forward() method steps are to delta traverse this given +1.
|
|
|
|
|
return delta_traverse(1);
|
2022-10-13 22:43:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-21 17:44:42 -06:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#can-have-its-url-rewritten
|
2024-03-18 16:22:27 +13:00
|
|
|
|
bool can_have_its_url_rewritten(DOM::Document const& document, URL::URL const& target_url)
|
2023-08-21 17:44:42 -06:00
|
|
|
|
{
|
|
|
|
|
// 1. Let documentURL be document's URL.
|
|
|
|
|
auto document_url = document.url();
|
|
|
|
|
|
|
|
|
|
// 2. If targetURL and documentURL differ in their scheme, username, password, host, or port components,
|
|
|
|
|
// then return false.
|
|
|
|
|
if (target_url.scheme() != document_url.scheme()
|
2024-08-04 22:02:02 +12:00
|
|
|
|
|| target_url.username() != document_url.username()
|
|
|
|
|
|| target_url.password() != document_url.password()
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|| target_url.host() != document_url.host()
|
|
|
|
|
|| target_url.port() != document_url.port())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// 3. If targetURL's scheme is an HTTP(S) scheme, then return true.
|
|
|
|
|
// (Differences in path, query, and fragment are allowed for http: and https: URLs.)
|
|
|
|
|
if (target_url.scheme() == "http"sv || target_url.scheme() == "https"sv)
|
|
|
|
|
return true;
|
|
|
|
|
|
2024-08-17 13:27:35 +01:00
|
|
|
|
// 4. If targetURL's scheme is "file", then:
|
|
|
|
|
// (Differences in query and fragment are allowed for file: URLs.)
|
2024-08-06 13:31:21 +02:00
|
|
|
|
if (target_url.scheme() == "file"sv) {
|
2024-08-17 13:27:35 +01:00
|
|
|
|
// 1. If targetURL and documentURL differ in their path component, then return false.
|
|
|
|
|
if (target_url.paths() != document_url.paths())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// 2. Return true.
|
|
|
|
|
return true;
|
2024-08-06 13:31:21 +02:00
|
|
|
|
}
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|
|
|
|
|
// 5. If targetURL and documentURL differ in their path component or query components, then return false.
|
|
|
|
|
// (Only differences in fragment are allowed for other types of URLs.)
|
2024-08-17 13:27:35 +01:00
|
|
|
|
if (target_url.paths() != document_url.paths() || target_url.query() != document_url.query())
|
2023-08-21 17:44:42 -06:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// 6. Return true.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 23:43:34 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/history.html#shared-history-push/replace-state-steps
|
2023-09-27 22:59:57 -06:00
|
|
|
|
WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value data, Optional<String> const& url, HistoryHandlingBehavior history_handling)
|
2021-09-11 23:43:34 +01:00
|
|
|
|
{
|
2023-09-27 22:59:57 -06:00
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// 1. Let document be history's relevant global object's associated Document.
|
|
|
|
|
auto& document = as<Window>(relevant_global_object(*this)).associated_document();
|
2021-09-11 23:43:34 +01:00
|
|
|
|
|
|
|
|
|
// 2. If document is not fully active, then throw a "SecurityError" DOMException.
|
2025-04-11 17:24:50 +01:00
|
|
|
|
if (!document.is_fully_active())
|
2024-10-12 20:56:21 +02:00
|
|
|
|
return WebIDL::SecurityError::create(realm(), "Cannot perform pushState or replaceState on a document that isn't fully active."_string);
|
2021-09-11 23:43:34 +01:00
|
|
|
|
|
2025-04-02 12:30:27 +01:00
|
|
|
|
// 3. Optionally, throw a "SecurityError" DOMException. (For example, the user agent might disallow calls to these
|
|
|
|
|
// methods that are invoked on a timer, or from event listeners that are not triggered in response to a clear
|
|
|
|
|
// user action, or that are invoked in rapid succession.)
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|
|
|
|
|
// 4. Let serializedData be StructuredSerializeForStorage(data). Rethrow any exceptions.
|
|
|
|
|
// FIXME: Actually rethrow exceptions here once we start using the serialized data.
|
|
|
|
|
// Throwing here on data types we don't yet serialize will regress sites that use push/replaceState.
|
2023-09-27 22:59:57 -06:00
|
|
|
|
auto serialized_data_or_error = structured_serialize_for_storage(vm, data);
|
|
|
|
|
auto serialized_data = serialized_data_or_error.is_error() ? MUST(structured_serialize_for_storage(vm, JS::js_null())) : serialized_data_or_error.release_value();
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|
|
|
|
|
// 5. Let newURL be document's URL.
|
2025-04-11 17:24:50 +01:00
|
|
|
|
auto new_url = document.url();
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|
|
|
|
|
// 6. If url is not null or the empty string, then:
|
2023-08-26 17:03:52 +12:00
|
|
|
|
if (url.has_value() && !url->is_empty()) {
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|
|
|
|
|
// 1. Parse url, relative to the relevant settings object of history.
|
2023-12-16 17:49:34 +03:30
|
|
|
|
auto parsed_url = relevant_settings_object(*this).parse_url(url->to_byte_string());
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|
|
|
|
|
// 2. If that fails, then throw a "SecurityError" DOMException.
|
2025-02-16 14:01:09 +13:00
|
|
|
|
if (!parsed_url.has_value())
|
2024-10-12 20:56:21 +02:00
|
|
|
|
return WebIDL::SecurityError::create(realm(), "Cannot pushState or replaceState to incompatible URL"_string);
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|
|
|
|
|
// 3. Set newURL to the resulting URL record.
|
2025-02-16 14:01:09 +13:00
|
|
|
|
new_url = parsed_url.release_value();
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|
|
|
|
|
// 4. If document cannot have its URL rewritten to newURL, then throw a "SecurityError" DOMException.
|
|
|
|
|
if (!can_have_its_url_rewritten(document, new_url))
|
2024-10-12 20:56:21 +02:00
|
|
|
|
return WebIDL::SecurityError::create(realm(), "Cannot pushState or replaceState to incompatible URL"_string);
|
2023-08-21 17:44:42 -06:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-06 14:18:40 +02:00
|
|
|
|
// 7. Let navigation be history's relevant global object's navigation API.
|
2025-01-21 09:12:05 -05:00
|
|
|
|
auto navigation = as<Window>(relevant_global_object(*this)).navigation();
|
2024-08-06 14:18:40 +02:00
|
|
|
|
|
|
|
|
|
// 8. Let continue be the result of firing a push/replace/reload navigate event at navigation
|
|
|
|
|
// with navigationType set to historyHandling, isSameDocument set to true, destinationURL set to newURL,
|
|
|
|
|
// and classicHistoryAPIState set to serializedData.
|
|
|
|
|
auto navigation_type = history_handling == HistoryHandlingBehavior::Push ? Bindings::NavigationType::Push : Bindings::NavigationType::Replace;
|
|
|
|
|
auto continue_ = navigation->fire_a_push_replace_reload_navigate_event(navigation_type, new_url, true, UserNavigationInvolvement::None, {}, {}, serialized_data);
|
|
|
|
|
// 9. If continue is false, then return.
|
|
|
|
|
if (!continue_)
|
|
|
|
|
return {};
|
2023-08-21 17:44:42 -06:00
|
|
|
|
|
2023-09-23 23:04:24 +02:00
|
|
|
|
// 10. Run the URL and history update steps given document and newURL, with serializedData set to
|
|
|
|
|
// serializedData and historyHandling set to historyHandling.
|
2023-09-27 22:59:57 -06:00
|
|
|
|
perform_url_and_history_update_steps(document, new_url, serialized_data, history_handling);
|
2023-09-23 23:04:24 +02:00
|
|
|
|
|
2021-09-11 23:43:34 +01:00
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 10:33:05 +02:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-scroll-restoration
|
|
|
|
|
WebIDL::ExceptionOr<Bindings::ScrollRestoration> History::scroll_restoration() const
|
|
|
|
|
{
|
|
|
|
|
// 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
|
2025-04-11 17:24:50 +01:00
|
|
|
|
auto& this_relevant_global_object = as<Window>(relevant_global_object(*this));
|
|
|
|
|
if (!this_relevant_global_object.associated_document().is_fully_active())
|
2024-10-12 20:56:21 +02:00
|
|
|
|
return WebIDL::SecurityError::create(realm(), "Cannot obtain scroll restoration mode for a document that isn't fully active."_string);
|
2024-10-02 10:33:05 +02:00
|
|
|
|
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// 2. Return this's relevant global object's navigable's active session history entry's scroll restoration mode.
|
|
|
|
|
auto scroll_restoration_mode = this_relevant_global_object.navigable()->active_session_history_entry()->scroll_restoration_mode();
|
2024-10-02 10:33:05 +02:00
|
|
|
|
switch (scroll_restoration_mode) {
|
|
|
|
|
case ScrollRestorationMode::Auto:
|
|
|
|
|
return Bindings::ScrollRestoration::Auto;
|
|
|
|
|
case ScrollRestorationMode::Manual:
|
|
|
|
|
return Bindings::ScrollRestoration::Manual;
|
|
|
|
|
}
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-scroll-restoration
|
|
|
|
|
WebIDL::ExceptionOr<void> History::set_scroll_restoration(Bindings::ScrollRestoration scroll_restoration)
|
|
|
|
|
{
|
|
|
|
|
// 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
|
2025-04-11 17:24:50 +01:00
|
|
|
|
auto& this_relevant_global_object = as<Window>(relevant_global_object(*this));
|
|
|
|
|
if (!this_relevant_global_object.associated_document().is_fully_active())
|
2024-10-12 20:56:21 +02:00
|
|
|
|
return WebIDL::SecurityError::create(realm(), "Cannot set scroll restoration mode for a document that isn't fully active."_string);
|
2024-10-02 10:33:05 +02:00
|
|
|
|
|
2025-04-11 17:24:50 +01:00
|
|
|
|
// 2. Set this's relevant global object's navigable's active session history entry's scroll restoration mode to the given value.
|
|
|
|
|
auto active_session_history_entry = this_relevant_global_object.navigable()->active_session_history_entry();
|
2024-10-02 10:33:05 +02:00
|
|
|
|
switch (scroll_restoration) {
|
|
|
|
|
case Bindings::ScrollRestoration::Auto:
|
|
|
|
|
active_session_history_entry->set_scroll_restoration_mode(ScrollRestorationMode::Auto);
|
|
|
|
|
break;
|
|
|
|
|
case Bindings::ScrollRestoration::Manual:
|
|
|
|
|
active_session_history_entry->set_scroll_restoration_mode(ScrollRestorationMode::Manual);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 23:43:34 +01:00
|
|
|
|
}
|