LibWeb: Implement history.scrollRestoration

This commit is contained in:
Jelle Raaijmakers 2024-10-02 10:33:05 +02:00 committed by Andrew Kaster
parent 5a35ee08d3
commit 2106617f5b
Notes: github-actions[bot] 2024-10-02 23:09:11 +00:00
5 changed files with 59 additions and 1 deletions

View file

@ -0,0 +1,4 @@
auto
auto
manual
auto

View file

@ -0,0 +1,12 @@
<script src="../include.js"></script>
<script>
test(() => {
println(history.scrollRestoration);
history.scrollRestoration = 'bad value';
println(history.scrollRestoration);
history.scrollRestoration = 'manual';
println(history.scrollRestoration);
history.scrollRestoration = 'auto';
println(history.scrollRestoration);
});
</script>

View file

@ -229,4 +229,43 @@ WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value d
return {};
}
// 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.
if (!m_associated_document->is_fully_active())
return WebIDL::SecurityError::create(realm(), "Cannot obtain scroll restoration mode for a document that isn't fully active."_fly_string);
// 2. Return this's node navigable's active session history entry's scroll restoration mode.
auto scroll_restoration_mode = m_associated_document->navigable()->active_session_history_entry()->scroll_restoration_mode();
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.
if (!m_associated_document->is_fully_active())
return WebIDL::SecurityError::create(realm(), "Cannot set scroll restoration mode for a document that isn't fully active."_fly_string);
// 2. Set this's node navigable's active session history entry's scroll restoration mode to the given value.
auto active_session_history_entry = m_associated_document->navigable()->active_session_history_entry();
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 {};
}
}

View file

@ -7,6 +7,7 @@
#pragma once
#include <LibWeb/Bindings/HistoryPrototype.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/HTML/HistoryHandlingBehavior.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
@ -29,6 +30,8 @@ public:
WebIDL::ExceptionOr<void> back();
WebIDL::ExceptionOr<void> forward();
WebIDL::ExceptionOr<u64> length() const;
WebIDL::ExceptionOr<Bindings::ScrollRestoration> scroll_restoration() const;
WebIDL::ExceptionOr<void> set_scroll_restoration(Bindings::ScrollRestoration);
WebIDL::ExceptionOr<JS::Value> state() const;
u64 m_index { 0 };

View file

@ -5,7 +5,7 @@ enum ScrollRestoration { "auto", "manual" };
[Exposed=Window]
interface History {
readonly attribute unsigned long length;
[FIXME] attribute ScrollRestoration scrollRestoration;
attribute ScrollRestoration scrollRestoration;
readonly attribute any state;
undefined go(optional long delta = 0);
undefined back();