From 649f70db65f13565b84efee25ac0b0c4a329e3f7 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Thu, 11 Apr 2024 17:21:09 +0200 Subject: [PATCH] LibWeb+WebContent: Initialise JS console from Document::initialize() Before this change JS console was initialise from activate_history_entry() which is too late for about:blank documents that are ready to run scripts immediately after creation. --- Userland/Libraries/LibWeb/DOM/Document.cpp | 6 ++++++ Userland/Libraries/LibWeb/HTML/Navigable.cpp | 4 ---- Userland/Libraries/LibWeb/Page/Page.h | 1 + Userland/Services/WebContent/PageClient.cpp | 11 ++++++----- Userland/Services/WebContent/PageClient.h | 1 + 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index b2f7b1ca90a..9753f95436f 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -378,6 +378,8 @@ void Document::initialize(JS::Realm& realm) m_selection = heap().allocate(realm, realm, *this); m_list_of_available_images = heap().allocate(realm); + + page().client().page_did_create_new_document(*this); } // https://html.spec.whatwg.org/multipage/document-lifecycle.html#populate-with-html/head/body @@ -3408,6 +3410,10 @@ void Document::make_active() // 2. Set document's browsing context's WindowProxy's [[Window]] internal slot value to window. m_browsing_context->window_proxy()->set_window(window); + if (m_browsing_context->is_top_level()) { + page().client().page_did_change_active_document_in_top_level_browsing_context(*this); + } + // 3. Set document's visibility state to document's node navigable's traversable navigable's system visibility state. if (navigable()) { m_visibility_state = navigable()->traversable_navigable()->system_visibility_state(); diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.cpp b/Userland/Libraries/LibWeb/HTML/Navigable.cpp index 9615b44bc1f..c5b269298d2 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Userland/Libraries/LibWeb/HTML/Navigable.cpp @@ -197,10 +197,6 @@ void Navigable::activate_history_entry(JS::GCPtr entry) // 5. Make active newDocument. new_document->make_active(); - - // Not in the spec: - VERIFY(active_browsing_context()); - active_browsing_context()->page().client().page_did_create_new_document(*new_document); } // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-document diff --git a/Userland/Libraries/LibWeb/Page/Page.h b/Userland/Libraries/LibWeb/Page/Page.h index 0e337e661bb..05d77c2bc2d 100644 --- a/Userland/Libraries/LibWeb/Page/Page.h +++ b/Userland/Libraries/LibWeb/Page/Page.h @@ -258,6 +258,7 @@ public: virtual Gfx::IntRect page_did_request_fullscreen_window() { return {}; } virtual void page_did_start_loading(URL::URL const&, bool is_redirect) { (void)is_redirect; } virtual void page_did_create_new_document(Web::DOM::Document&) { } + virtual void page_did_change_active_document_in_top_level_browsing_context(Web::DOM::Document&) { } virtual void page_did_destroy_document(Web::DOM::Document&) { } virtual void page_did_finish_loading(URL::URL const&) { } virtual void page_did_update_url(URL::URL const&, Web::HTML::HistoryHandlingBehavior) { } diff --git a/Userland/Services/WebContent/PageClient.cpp b/Userland/Services/WebContent/PageClient.cpp index 75b9ce87e2c..af8b5678307 100644 --- a/Userland/Services/WebContent/PageClient.cpp +++ b/Userland/Services/WebContent/PageClient.cpp @@ -355,6 +355,12 @@ void PageClient::page_did_create_new_document(Web::DOM::Document& document) initialize_js_console(document); } +void PageClient::page_did_change_active_document_in_top_level_browsing_context(Web::DOM::Document& document) +{ + VERIFY(m_console_clients.contains(document)); + m_top_level_document_console_client = *m_console_clients.get(document).value(); +} + void PageClient::page_did_destroy_document(Web::DOM::Document& document) { destroy_js_console(document); @@ -675,11 +681,6 @@ void PageClient::initialize_js_console(Web::DOM::Document& document) auto console_client = make(console_object->console(), document.realm(), *this); console_object->console().set_client(*console_client); - VERIFY(document.browsing_context()); - if (document.browsing_context()->is_top_level()) { - m_top_level_document_console_client = console_client->make_weak_ptr(); - } - m_console_clients.set(document, move(console_client)); } diff --git a/Userland/Services/WebContent/PageClient.h b/Userland/Services/WebContent/PageClient.h index 45fe94e5bbb..88a8b2fdc32 100644 --- a/Userland/Services/WebContent/PageClient.h +++ b/Userland/Services/WebContent/PageClient.h @@ -117,6 +117,7 @@ private: virtual void page_did_request_media_context_menu(Web::CSSPixelPoint, ByteString const& target, unsigned modifiers, Web::Page::MediaContextMenu) override; virtual void page_did_start_loading(URL::URL const&, bool) override; virtual void page_did_create_new_document(Web::DOM::Document&) override; + virtual void page_did_change_active_document_in_top_level_browsing_context(Web::DOM::Document&) override; virtual void page_did_destroy_document(Web::DOM::Document&) override; virtual void page_did_finish_loading(URL::URL const&) override; virtual void page_did_update_url(URL::URL const&, Web::HTML::HistoryHandlingBehavior) override;