Skip to content

Commit

Permalink
LibWeb/HTML: Implement History.scrollRestoration
Browse files Browse the repository at this point in the history
Worked on implementing the specs for
[History.scrollRestoration](https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-scroll-restoration)
  • Loading branch information
hanpham32 committed Sep 24, 2024
1 parent 2d34e79 commit a83faf5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/Forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ enum class RequestPriority;
enum class RequestRedirect;
enum class ResizeObserverBoxOptions;
enum class ResponseType;
enum class ScrollRestoration;
enum class TextTrackKind;
enum class XMLHttpRequestResponseType;
}
Expand Down Expand Up @@ -521,6 +522,7 @@ class WorkerNavigator;
enum class AllowMultipleFiles;
enum class MediaSeekMode;
enum class SandboxingFlagSet;
enum class ScrollRestorationMode;

struct CrossOriginOpenerPolicy;
struct CrossOriginOpenerPolicyEnforcementResult;
Expand Down
29 changes: 29 additions & 0 deletions Userland/Libraries/LibWeb/HTML/History.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,35 @@ WebIDL::ExceptionOr<u64> History::length() const
return m_length;
}

// 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 perform scrollRestoration on a document that isn't fully active."_fly_string);

VERIFY(m_associated_document->navigable());

// 2. Return this's node navigable's active session history entry's scroll restoration mode.
auto mode = m_associated_document->navigable()->active_session_history_entry()->scroll_restoration_mode();
return mode == ScrollRestorationMode::Auto ? Bindings::ScrollRestoration::Auto : Bindings::ScrollRestoration::Manual;
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-scroll-restoration
WebIDL::ExceptionOr<void> History::set_scroll_restoration(Bindings::ScrollRestoration 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 perform scrollRestoration on a document that isn't fully active."_fly_string);

VERIFY(m_associated_document->navigable());

// 2. Set this's node navigable's active session history entry's scroll restoration mode to the given value.
m_associated_document->navigable()->active_session_history_entry()->set_scroll_restoration_mode(restoration == Bindings::ScrollRestoration::Auto ? ScrollRestorationMode::Auto : ScrollRestorationMode::Manual);

return {};
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-state
WebIDL::ExceptionOr<JS::Value> History::state() const
{
Expand Down
5 changes: 5 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 @@ -23,6 +24,9 @@ class History final : public Bindings::PlatformObject {

virtual ~History() override;

WebIDL::ExceptionOr<Bindings::ScrollRestoration> scroll_restoration() const;
WebIDL::ExceptionOr<void> set_scroll_restoration(Bindings::ScrollRestoration restoration);

WebIDL::ExceptionOr<void> push_state(JS::Value data, String const& unused, Optional<String> const& url = {});
WebIDL::ExceptionOr<void> replace_state(JS::Value data, String const& unused, Optional<String> const& url = {});
WebIDL::ExceptionOr<void> go(WebIDL::Long delta);
Expand All @@ -46,6 +50,7 @@ class History final : public Bindings::PlatformObject {
WebIDL::ExceptionOr<void> shared_history_push_replace_state(JS::Value data, Optional<String> const& url, HistoryHandlingBehavior);

JS::NonnullGCPtr<DOM::Document> m_associated_document;
Bindings::ScrollRestoration m_scroll_restoration { Bindings::ScrollRestoration::Auto };
JS::Value m_state { JS::js_null() };
};

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 a83faf5

Please sign in to comment.