LibWeb: Add NavigationTransition, a transient property of Navigation

This property is useful for web content to determine whether an ongoing
navigation has settled or not.
This commit is contained in:
Andrew Kaster 2023-08-23 15:34:02 -06:00 committed by Andrew Kaster
parent 4989375191
commit 3dd3b2019d
11 changed files with 118 additions and 1 deletions

View file

@ -127,6 +127,7 @@ source_set("HTML") {
"NavigationCurrentEntryChangeEvent.cpp",
"NavigationDestination.cpp",
"NavigationHistoryEntry.cpp",
"NavigationTransition.cpp",
"Navigator.cpp",
"NavigatorID.cpp",
"PageTransitionEvent.cpp",

View file

@ -186,6 +186,7 @@ standard_idl_files = [
"//Userland/Libraries/LibWeb/HTML/NavigationCurrentEntryChangeEvent.idl",
"//Userland/Libraries/LibWeb/HTML/NavigationDestination.idl",
"//Userland/Libraries/LibWeb/HTML/NavigationHistoryEntry.idl",
"//Userland/Libraries/LibWeb/HTML/NavigationTransition.idl",
"//Userland/Libraries/LibWeb/HTML/Navigator.idl",
"//Userland/Libraries/LibWeb/HTML/PageTransitionEvent.idl",
"//Userland/Libraries/LibWeb/HTML/Path2D.idl",

View file

@ -354,6 +354,7 @@ set(SOURCES
HTML/NavigationDestination.cpp
HTML/NavigationCurrentEntryChangeEvent.cpp
HTML/NavigationHistoryEntry.cpp
HTML/NavigationTransition.cpp
HTML/Navigator.cpp
HTML/NavigatorID.cpp
HTML/PageTransitionEvent.cpp

View file

@ -417,6 +417,7 @@ class NavigableContainer;
class Navigation;
class NavigationCurrentEntryChangeEvent;
class NavigationHistoryEntry;
class NavigationTransition;
class Navigator;
struct NavigationParams;
class Origin;

View file

@ -12,6 +12,7 @@
#include <LibWeb/HTML/Navigation.h>
#include <LibWeb/HTML/NavigationCurrentEntryChangeEvent.h>
#include <LibWeb/HTML/NavigationHistoryEntry.h>
#include <LibWeb/HTML/NavigationTransition.h>
#include <LibWeb/HTML/Window.h>
namespace Web::HTML {
@ -39,6 +40,7 @@ void Navigation::visit_edges(JS::Cell::Visitor& visitor)
Base::visit_edges(visitor);
for (auto& entry : m_entry_list)
visitor.visit(entry);
visitor.visit(m_transition);
}
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-entries

View file

@ -53,6 +53,9 @@ public:
bool can_go_back() const;
bool can_go_forward() const;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-transition
JS::GCPtr<NavigationTransition> transition() const { return m_transition; }
// Event Handlers
void set_onnavigate(WebIDL::CallbackType*);
WebIDL::CallbackType* onnavigate();
@ -85,6 +88,10 @@ private:
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry-index
// Each Navigation has an associated current entry index, an integer, initially 1.
i64 m_current_entry_index { -1 };
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigation-transition
// Each Navigation has a transition, which is a NavigationTransition or null, initially null.
JS::GCPtr<NavigationTransition> m_transition { nullptr };
};
}

View file

@ -1,6 +1,7 @@
#import <DOM/EventHandler.idl>
#import <DOM/EventTarget.idl>
#import <HTML/NavigationHistoryEntry.idl>
#import <HTML/NavigationTransition.idl>
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-interface
[Exposed=Window]
@ -8,7 +9,7 @@ interface Navigation : EventTarget {
sequence<NavigationHistoryEntry> entries();
readonly attribute NavigationHistoryEntry? currentEntry;
undefined updateCurrentEntry(NavigationUpdateCurrentEntryOptions options);
// FIXME: readonly attribute NavigationTransition? transition;
readonly attribute NavigationTransition? transition;
readonly attribute boolean canGoBack;
readonly attribute boolean canGoForward;

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Promise.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/NavigationTransitionPrototype.h>
#include <LibWeb/HTML/NavigationHistoryEntry.h>
#include <LibWeb/HTML/NavigationTransition.h>
namespace Web::HTML {
JS::NonnullGCPtr<NavigationTransition> NavigationTransition::create(JS::Realm& realm, Bindings::NavigationType navigation_type, JS::NonnullGCPtr<NavigationHistoryEntry> from_entry, JS::GCPtr<JS::Promise> finished_promise)
{
return realm.heap().allocate<NavigationTransition>(realm, realm, navigation_type, from_entry, finished_promise);
}
NavigationTransition::NavigationTransition(JS::Realm& realm, Bindings::NavigationType navigation_type, JS::NonnullGCPtr<NavigationHistoryEntry> from_entry, JS::GCPtr<JS::Promise> finished_promise)
: Bindings::PlatformObject(realm)
, m_navigation_type(navigation_type)
, m_from_entry(from_entry)
, m_finished_promise(finished_promise)
{
}
NavigationTransition::~NavigationTransition() = default;
void NavigationTransition::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::NavigationTransitionPrototype>(realm, "NavigationTransition"));
}
void NavigationTransition::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_from_entry);
visitor.visit(m_finished_promise);
}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/HTML/NavigationType.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationtransition
class NavigationTransition : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(NavigationTransition, Bindings::PlatformObject);
public:
[[nodiscard]] static JS::NonnullGCPtr<NavigationTransition> create(JS::Realm&, Bindings::NavigationType, JS::NonnullGCPtr<NavigationHistoryEntry>, JS::GCPtr<JS::Promise>);
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationtransition-navigationtype
Bindings::NavigationType navigation_type() const { return m_navigation_type; }
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationtransition-from
JS::NonnullGCPtr<NavigationHistoryEntry> from() const { return m_from_entry; }
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationtransition-finished
JS::GCPtr<JS::Promise> finished() const { return m_finished_promise; }
virtual ~NavigationTransition() override;
private:
NavigationTransition(JS::Realm&, Bindings::NavigationType, JS::NonnullGCPtr<NavigationHistoryEntry>, JS::GCPtr<JS::Promise>);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(JS::Cell::Visitor&) override;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationtransition-navigationtype
Bindings::NavigationType m_navigation_type;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationtransition-from
JS::NonnullGCPtr<NavigationHistoryEntry> m_from_entry;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationtransition-finished
JS::GCPtr<JS::Promise> m_finished_promise;
};
}

View file

@ -0,0 +1,9 @@
#import <HTML/NavigationType.idl>
#import <HTML/NavigationHistoryEntry.idl>
[Exposed=Window, UseNewAKString]
interface NavigationTransition {
readonly attribute NavigationType navigationType;
readonly attribute NavigationHistoryEntry from;
readonly attribute Promise<undefined> finished;
};

View file

@ -172,6 +172,7 @@ libweb_js_bindings(HTML/Navigation)
libweb_js_bindings(HTML/NavigationCurrentEntryChangeEvent)
libweb_js_bindings(HTML/NavigationDestination)
libweb_js_bindings(HTML/NavigationHistoryEntry)
libweb_js_bindings(HTML/NavigationTransition)
libweb_js_bindings(HTML/Navigator)
libweb_js_bindings(HTML/PageTransitionEvent)
libweb_js_bindings(HTML/Path2D)