From 49893751915dba55878ec7b15026a3b8c5038454 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Wed, 23 Aug 2023 13:35:25 -0600 Subject: [PATCH] LibWeb: Add NavigationDestination, used for NavigateEvents This class will be used in the algorithms for the navigate event firing algorithms to populate the destination field of the NavigateEvent. --- .../Userland/Libraries/LibWeb/HTML/BUILD.gn | 1 + .../Userland/Libraries/LibWeb/idl_files.gni | 1 + Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../LibWeb/HTML/NavigationDestination.cpp | 91 +++++++++++++++++++ .../LibWeb/HTML/NavigationDestination.h | 59 ++++++++++++ .../LibWeb/HTML/NavigationDestination.idl | 11 +++ Userland/Libraries/LibWeb/idl_files.cmake | 1 + 7 files changed, 165 insertions(+) create mode 100644 Userland/Libraries/LibWeb/HTML/NavigationDestination.cpp create mode 100644 Userland/Libraries/LibWeb/HTML/NavigationDestination.h create mode 100644 Userland/Libraries/LibWeb/HTML/NavigationDestination.idl diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/HTML/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/HTML/BUILD.gn index 03e7f08406..62e755667b 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/HTML/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/HTML/BUILD.gn @@ -125,6 +125,7 @@ source_set("HTML") { "NavigableContainer.cpp", "Navigation.cpp", "NavigationCurrentEntryChangeEvent.cpp", + "NavigationDestination.cpp", "NavigationHistoryEntry.cpp", "Navigator.cpp", "NavigatorID.cpp", diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni b/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni index e4d32263aa..1506239f29 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni @@ -184,6 +184,7 @@ standard_idl_files = [ "//Userland/Libraries/LibWeb/HTML/MimeTypeArray.idl", "//Userland/Libraries/LibWeb/HTML/Navigation.idl", "//Userland/Libraries/LibWeb/HTML/NavigationCurrentEntryChangeEvent.idl", + "//Userland/Libraries/LibWeb/HTML/NavigationDestination.idl", "//Userland/Libraries/LibWeb/HTML/NavigationHistoryEntry.idl", "//Userland/Libraries/LibWeb/HTML/Navigator.idl", "//Userland/Libraries/LibWeb/HTML/PageTransitionEvent.idl", diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 3f1291adce..80b4bcfbac 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -351,6 +351,7 @@ set(SOURCES HTML/Navigable.cpp HTML/NavigableContainer.cpp HTML/Navigation.cpp + HTML/NavigationDestination.cpp HTML/NavigationCurrentEntryChangeEvent.cpp HTML/NavigationHistoryEntry.cpp HTML/Navigator.cpp diff --git a/Userland/Libraries/LibWeb/HTML/NavigationDestination.cpp b/Userland/Libraries/LibWeb/HTML/NavigationDestination.cpp new file mode 100644 index 0000000000..8f25d67edb --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/NavigationDestination.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2023, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include + +namespace Web::HTML { + +JS::NonnullGCPtr NavigationDestination::create(JS::Realm& realm) +{ + return realm.heap().allocate(realm, realm); +} + +NavigationDestination::NavigationDestination(JS::Realm& realm) + : Bindings::PlatformObject(realm) +{ +} + +NavigationDestination::~NavigationDestination() = default; + +void NavigationDestination::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + set_prototype(&Bindings::ensure_web_prototype(realm, "NavigationDestination")); +} + +void NavigationDestination::visit_edges(JS::Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_entry); +} + +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-url +WebIDL::ExceptionOr NavigationDestination::url() const +{ + // The url getter steps are to return this's URL, serialized. + return TRY_OR_THROW_OOM(vm(), String::from_deprecated_string(m_url.serialize())); +} + +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-key +String NavigationDestination::key() const +{ + // The key getter steps are: + + // 1. If this's entry is null, then return the empty string. + // 2. Return this's entry's key. + return (m_entry == nullptr) ? String {} : m_entry->key(); +} + +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-id +String NavigationDestination::id() const +{ + // The id getter steps are: + + // 1. If this's entry is null, then return the empty string. + // 2. Return this's entry's ID. + return (m_entry == nullptr) ? String {} : m_entry->id(); +} + +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-index +i64 NavigationDestination::index() const +{ + // The index getter steps are: + + // 1. If this's entry is null, then return -1. + // 2. Return this's entry's index. + return (m_entry == nullptr) ? -1 : m_entry->index(); +} + +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-samedocument +bool NavigationDestination::same_document() const +{ + // The sameDocument getter steps are to return this's is same document. + return m_is_same_document; +} + +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-getstate +WebIDL::ExceptionOr NavigationDestination::get_state() +{ + // The getState() method steps are to return StructuredDeserialize(this's state). + return structured_deserialize(vm(), m_state, realm(), {}); +} + +} diff --git a/Userland/Libraries/LibWeb/HTML/NavigationDestination.h b/Userland/Libraries/LibWeb/HTML/NavigationDestination.h new file mode 100644 index 0000000000..75724fb16d --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/NavigationDestination.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::HTML { + +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationdestination +class NavigationDestination : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(NavigationDestination, Bindings::PlatformObject); + +public: + [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&); + + WebIDL::ExceptionOr url() const; + String key() const; + String id() const; + i64 index() const; + bool same_document() const; + WebIDL::ExceptionOr get_state(); + + // Non-spec'd getter, not exposed to JS + JS::GCPtr navigation_history_entry() const { return m_entry; } + + // Setters are not available to JS, but expected in many spec algorithms + void set_url(AK::URL const& url) { m_url = url; } + void set_entry(JS::GCPtr entry) { m_entry = entry; } + void set_state(SerializationRecord state) { m_state = move(state); } + void set_is_same_document(bool b) { m_is_same_document = b; } + + virtual ~NavigationDestination() override; + +private: + NavigationDestination(JS::Realm&); + + 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-navigationdestination-url + AK::URL m_url; + + // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationdestination-url + JS::GCPtr m_entry; + + // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationdestination-state + SerializationRecord m_state; + + // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationdestination-samedocument + bool m_is_same_document { false }; +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/NavigationDestination.idl b/Userland/Libraries/LibWeb/HTML/NavigationDestination.idl new file mode 100644 index 0000000000..55223472ad --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/NavigationDestination.idl @@ -0,0 +1,11 @@ +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-navigationdestination-interface +[Exposed=Window, UseNewAKString] +interface NavigationDestination { + readonly attribute USVString url; + readonly attribute DOMString key; + readonly attribute DOMString id; + readonly attribute long long index; + readonly attribute boolean sameDocument; + + any getState(); +}; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 1113b8e172..934d346289 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -170,6 +170,7 @@ libweb_js_bindings(HTML/MimeType) libweb_js_bindings(HTML/MimeTypeArray) libweb_js_bindings(HTML/Navigation) libweb_js_bindings(HTML/NavigationCurrentEntryChangeEvent) +libweb_js_bindings(HTML/NavigationDestination) libweb_js_bindings(HTML/NavigationHistoryEntry) libweb_js_bindings(HTML/Navigator) libweb_js_bindings(HTML/PageTransitionEvent)