Skip to content

Commit

Permalink
LibWeb: Implement history.scrollRestoration
Browse files Browse the repository at this point in the history
  • Loading branch information
gmta authored and ADKaster committed Oct 2, 2024
1 parent 5a35ee0 commit 2106617
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Tests/LibWeb/Text/expected/HTML/History-scrollRestoration.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
auto
auto
manual
auto
12 changes: 12 additions & 0 deletions Tests/LibWeb/Text/input/HTML/History-scrollRestoration.html
Original file line number Diff line number Diff line change
@@ -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>
39 changes: 39 additions & 0 deletions Userland/Libraries/LibWeb/HTML/History.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}

}
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/HTML/History.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -29,6 +30,8 @@ class History final : public Bindings::PlatformObject {
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 };
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/History.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2106617

Please sign in to comment.