From d9d88963809300893cbc2a532265a2c31fb90686 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Thu, 6 Apr 2023 18:08:44 +0300 Subject: [PATCH] LibWeb: Implement "get session history entries" for navigables https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-session-history-entries --- Userland/Libraries/LibWeb/HTML/Navigable.cpp | 38 ++++++++++++++++++++ Userland/Libraries/LibWeb/HTML/Navigable.h | 2 ++ 2 files changed, 40 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.cpp b/Userland/Libraries/LibWeb/HTML/Navigable.cpp index f26fb63ef9..60702bd518 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Userland/Libraries/LibWeb/HTML/Navigable.cpp @@ -160,6 +160,44 @@ JS::GCPtr Navigable::top_level_traversable() return verify_cast(navigable); } +// https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-session-history-entries +Vector>& Navigable::get_session_history_entries() const +{ + // 1. Let traversable be navigable's traversable navigable. + auto traversable = traversable_navigable(); + + // FIXME 2. Assert: this is running within traversable's session history traversal queue. + + // 3. If navigable is traversable, return traversable's session history entries. + if (this == traversable) + return traversable->session_history_entries(); + + // 4. Let docStates be an empty ordered set of document states. + Vector> doc_states; + + // 5. For each entry of traversable's session history entries, append entry's document state to docStates. + for (auto& entry : traversable->session_history_entries()) + doc_states.append(entry->document_state); + + // 6. For each docState of docStates: + while (!doc_states.is_empty()) { + auto doc_state = doc_states.take_first(); + + // 1. For each nestedHistory of docState's nested histories: + for (auto& nested_history : doc_state->nested_histories()) { + // 1. If nestedHistory's id equals navigable's id, return nestedHistory's entries. + if (nested_history.id == id()) + return nested_history.entries; + + // 2. For each entry of nestedHistory's entries, append entry's document state to docStates. + for (auto& entry : nested_history.entries) + doc_states.append(entry->document_state); + } + } + + VERIFY_NOT_REACHED(); +} + // To navigate a navigable navigable to a URL url using a Document sourceDocument, // with an optional POST resource, string, or null documentResource (default null), // an optional response-or-null response (default null), an optional boolean exceptionsEnabled (default false), diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.h b/Userland/Libraries/LibWeb/HTML/Navigable.h index efabfed9d5..61c1d9d7f8 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.h +++ b/Userland/Libraries/LibWeb/HTML/Navigable.h @@ -35,6 +35,8 @@ public: JS::GCPtr active_session_history_entry() const { return m_active_session_history_entry; }; JS::GCPtr current_session_history_entry() const { return m_current_session_history_entry; }; + Vector>& get_session_history_entries() const; + JS::GCPtr active_document(); JS::GCPtr active_browsing_context(); JS::GCPtr active_window_proxy();