diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp
index c8f81ff651..754f3f3ad0 100644
--- a/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp
+++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp
@@ -35,6 +35,23 @@ void BrowsingContextGroup::visit_edges(Cell::Visitor& visitor)
visitor.visit(context);
}
+// https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-browsing-context-group-and-document
+auto BrowsingContextGroup::create_a_new_browsing_context_group_and_document(Page& page) -> WebIDL::ExceptionOr
+{
+ // 1. Let group be a new browsing context group.
+ // 2. Append group to the user agent's browsing context group set.
+ auto group = Bindings::main_thread_vm().heap().allocate_without_realm(page);
+
+ // 3. Let browsingContext and document be the result of creating a new browsing context and document with null, null, and group.
+ auto [browsing_context, document] = TRY(BrowsingContext::create_a_new_browsing_context_and_document(page, nullptr, nullptr, group));
+
+ // 4. Append browsingContext to group.
+ group->append(browsing_context);
+
+ // 5. Return group and document.
+ return BrowsingContextGroupAndDocument { group, document };
+}
+
// https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-browsing-context-group
JS::NonnullGCPtr BrowsingContextGroup::create_a_new_browsing_context_group(Web::Page& page)
{
diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h
index a42743db80..3556accefb 100644
--- a/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h
+++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h
@@ -19,6 +19,13 @@ class BrowsingContextGroup final : public JS::Cell {
public:
static JS::NonnullGCPtr create_a_new_browsing_context_group(Page&);
+
+ struct BrowsingContextGroupAndDocument {
+ JS::NonnullGCPtr browsing_context;
+ JS::NonnullGCPtr document;
+ };
+ static WebIDL::ExceptionOr create_a_new_browsing_context_group_and_document(Page&);
+
~BrowsingContextGroup();
Page* page() { return m_page; }
diff --git a/Userland/Libraries/LibWeb/HTML/TraversableNavigable.cpp b/Userland/Libraries/LibWeb/HTML/TraversableNavigable.cpp
index 3576728331..70d21aa1a8 100644
--- a/Userland/Libraries/LibWeb/HTML/TraversableNavigable.cpp
+++ b/Userland/Libraries/LibWeb/HTML/TraversableNavigable.cpp
@@ -4,6 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include
+#include
+#include
+#include
#include
#include
@@ -20,6 +24,92 @@ void TraversableNavigable::visit_edges(Cell::Visitor& visitor)
visitor.visit(entry);
}
+static OrderedHashTable& user_agent_top_level_traversable_set()
+{
+ static OrderedHashTable set;
+ return set;
+}
+
+struct BrowsingContextAndDocument {
+ JS::NonnullGCPtr browsing_context;
+ JS::NonnullGCPtr document;
+};
+
+// https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-top-level-browsing-context
+static WebIDL::ExceptionOr create_a_new_top_level_browsing_context_and_document(Page& page)
+{
+ // 1. Let group and document be the result of creating a new browsing context group and document.
+ auto [group, document] = TRY(BrowsingContextGroup::create_a_new_browsing_context_group_and_document(page));
+
+ // 2. Return group's browsing context set[0] and document.
+ return BrowsingContextAndDocument { **group->browsing_context_set().begin(), document };
+}
+
+// https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-top-level-traversable
+WebIDL::ExceptionOr> TraversableNavigable::create_a_new_top_level_traversable(Page& page, JS::GCPtr opener, String target_name)
+{
+ auto& vm = Bindings::main_thread_vm();
+
+ // 1. Let document be null.
+ JS::GCPtr document = nullptr;
+
+ // 2. If opener is null, then set document to the second return value of creating a new top-level browsing context and document.
+ if (!opener) {
+ document = TRY(create_a_new_top_level_browsing_context_and_document(page)).document;
+ }
+
+ // 3. Otherwise, set document to the second return value of creating a new auxiliary browsing context and document given opener.
+ else {
+ document = TRY(BrowsingContext::create_a_new_auxiliary_browsing_context_and_document(page, *opener)).document;
+ }
+
+ // 4. Let documentState be a new document state, with
+ auto document_state = vm.heap().allocate_without_realm();
+
+ // document: document
+ document_state->set_document(document);
+
+ // navigable target name: targetName
+ document_state->set_navigable_target_name(target_name);
+
+ // 5. Let traversable be a new traversable navigable.
+ auto traversable = vm.heap().allocate_without_realm();
+
+ // 6. Initialize the navigable traversable given documentState.
+ TRY_OR_THROW_OOM(vm, traversable->initialize_navigable(document_state, nullptr));
+
+ // 7. Let initialHistoryEntry be traversable's active session history entry.
+ auto initial_history_entry = traversable->active_session_history_entry();
+ VERIFY(initial_history_entry);
+
+ // 8. Set initialHistoryEntry's step to 0.
+ initial_history_entry->step = 0;
+
+ // 9. Append initialHistoryEntry to traversable's session history entries.
+ traversable->m_session_history_entries.append(*initial_history_entry);
+
+ // FIXME: 10. If opener is non-null, then legacy-clone a traversable storage shed given opener's top-level traversable and traversable. [STORAGE]
+
+ // 11. Append traversable to the user agent's top-level traversable set.
+ user_agent_top_level_traversable_set().set(traversable);
+
+ // 12. Return traversable.
+ return traversable;
+}
+
+// https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-fresh-top-level-traversable
+WebIDL::ExceptionOr> TraversableNavigable::create_a_fresh_top_level_traversable(Page& page, AK::URL const& initial_navigation_url, Variant initial_navigation_post_resource)
+{
+ // 1. Let traversable be the result of creating a new top-level traversable given null and the empty string.
+ auto traversable = TRY(create_a_new_top_level_traversable(page, nullptr, {}));
+
+ // 2. Navigate traversable to initialNavigationURL using traversable's active document, with documentResource set to initialNavigationPostResource.
+ TRY(traversable->navigate(initial_navigation_url, *traversable->active_document(), initial_navigation_post_resource));
+
+ // 3. Return traversable.
+ return traversable;
+}
+
// https://html.spec.whatwg.org/multipage/document-sequences.html#top-level-traversable
bool TraversableNavigable::is_top_level_traversable() const
{
diff --git a/Userland/Libraries/LibWeb/HTML/TraversableNavigable.h b/Userland/Libraries/LibWeb/HTML/TraversableNavigable.h
index fba7537d32..fa63517085 100644
--- a/Userland/Libraries/LibWeb/HTML/TraversableNavigable.h
+++ b/Userland/Libraries/LibWeb/HTML/TraversableNavigable.h
@@ -17,6 +17,9 @@ class TraversableNavigable final : public Navigable {
JS_CELL(TraversableNavigable, Navigable);
public:
+ static WebIDL::ExceptionOr> create_a_new_top_level_traversable(Page&, JS::GCPtr opener, String target_name);
+ static WebIDL::ExceptionOr> create_a_fresh_top_level_traversable(Page&, AK::URL const& initial_navigation_url, Variant = Empty {});
+
virtual ~TraversableNavigable() override;
bool is_top_level_traversable() const;