diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp
index ea276329c8..f4fb6f1d01 100644
--- a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp
+++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp
@@ -27,7 +27,8 @@ void BrowsingContextContainer::inserted()
if (!is_connected())
return;
if (auto* frame = document().browsing_context()) {
- m_nested_browsing_context = BrowsingContext::create_nested(*this, frame->top_level_browsing_context());
+ VERIFY(frame->page());
+ m_nested_browsing_context = BrowsingContext::create_nested(*frame->page(), *this);
m_nested_browsing_context->set_frame_nesting_levels(frame->frame_nesting_levels());
m_nested_browsing_context->register_frame_nesting(document().url());
}
diff --git a/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp b/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp
index a46f13cc07..404cfbe24e 100644
--- a/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp
+++ b/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp
@@ -18,9 +18,8 @@
namespace Web {
-BrowsingContext::BrowsingContext(Page& page, HTML::BrowsingContextContainer* container, BrowsingContext& top_level_browsing_context)
+BrowsingContext::BrowsingContext(Page& page, HTML::BrowsingContextContainer* container)
: m_page(page)
- , m_top_level_browsing_context(top_level_browsing_context)
, m_loader(*this)
, m_event_handler({}, *this)
, m_container(container)
@@ -35,16 +34,6 @@ BrowsingContext::BrowsingContext(Page& page, HTML::BrowsingContextContainer* con
});
}
-BrowsingContext::BrowsingContext(HTML::BrowsingContextContainer& container, BrowsingContext& top_level_browsing_context)
- : BrowsingContext(*top_level_browsing_context.page(), &container, top_level_browsing_context)
-{
-}
-
-BrowsingContext::BrowsingContext(Page& page)
- : BrowsingContext(page, nullptr, *this)
-{
-}
-
BrowsingContext::~BrowsingContext()
{
}
diff --git a/Userland/Libraries/LibWeb/Page/BrowsingContext.h b/Userland/Libraries/LibWeb/Page/BrowsingContext.h
index 507932634d..a252a6b5a2 100644
--- a/Userland/Libraries/LibWeb/Page/BrowsingContext.h
+++ b/Userland/Libraries/LibWeb/Page/BrowsingContext.h
@@ -24,8 +24,8 @@ namespace Web {
class BrowsingContext : public TreeNode {
public:
- static NonnullRefPtr create_nested(HTML::BrowsingContextContainer& container, BrowsingContext& top_level_browsing_context) { return adopt_ref(*new BrowsingContext(container, top_level_browsing_context)); }
- static NonnullRefPtr create(Page& page) { return adopt_ref(*new BrowsingContext(page)); }
+ static NonnullRefPtr create_nested(Page& page, HTML::BrowsingContextContainer& container) { return adopt_ref(*new BrowsingContext(page, &container)); }
+ static NonnullRefPtr create(Page& page) { return adopt_ref(*new BrowsingContext(page, nullptr)); }
~BrowsingContext();
class ViewportClient {
@@ -36,7 +36,7 @@ public:
void register_viewport_client(ViewportClient&);
void unregister_viewport_client(ViewportClient&);
- bool is_top_level() const { return this == &top_level_browsing_context(); }
+ bool is_top_level() const { return !container(); }
bool is_focused_context() const;
DOM::Document const* active_document() const { return m_active_document; }
@@ -64,8 +64,15 @@ public:
void scroll_to_anchor(String const&);
- BrowsingContext& top_level_browsing_context() { return *m_top_level_browsing_context; }
- BrowsingContext const& top_level_browsing_context() const { return *m_top_level_browsing_context; }
+ BrowsingContext& top_level_browsing_context()
+ {
+ BrowsingContext* context = this;
+ while (context->parent())
+ context = context->parent();
+ return *context;
+ }
+
+ BrowsingContext const& top_level_browsing_context() const { return const_cast(this)->top_level_browsing_context(); }
HTML::BrowsingContextContainer* container() { return m_container; }
HTML::BrowsingContextContainer const* container() const { return m_container; }
@@ -95,18 +102,12 @@ public:
DOM::Document const* container_document() const;
private:
- explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*, BrowsingContext& top_level_browsing_context);
- explicit BrowsingContext(HTML::BrowsingContextContainer&, BrowsingContext& top_level_browsing_context);
- explicit BrowsingContext(Page&);
+ explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);
void reset_cursor_blink_cycle();
WeakPtr m_page;
- // NOTE: We expect there to always be a top-level browsing context as long as we exist.
- // The use of WeakPtr is for safety in case we get something wrong.
- WeakPtr m_top_level_browsing_context;
-
FrameLoader m_loader;
EventHandler m_event_handler;