From 077cb7687dbc7f3312689d37a6ffdf340ab13daa Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Fri, 14 Jun 2024 14:56:56 +0100 Subject: [PATCH] LibWeb: Limit find in page query to documents in the active window Previously, refreshing the current page and doing a new find in page query would result in duplicate matches being returned. --- Userland/Libraries/LibWeb/Page/Page.cpp | 26 +++++++++++++++---------- Userland/Libraries/LibWeb/Page/Page.h | 2 ++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibWeb/Page/Page.cpp b/Userland/Libraries/LibWeb/Page/Page.cpp index d88fbeb7362..7f2728ac577 100644 --- a/Userland/Libraries/LibWeb/Page/Page.cpp +++ b/Userland/Libraries/LibWeb/Page/Page.cpp @@ -538,13 +538,23 @@ void Page::set_user_style(String source) } } +Vector> Page::documents_in_active_window() const +{ + if (!top_level_traversable_is_initialized()) + return {}; + + auto documents = HTML::main_thread_event_loop().documents_in_this_event_loop(); + for (ssize_t i = documents.size() - 1; i >= 0; --i) { + if (documents[i]->window() != top_level_traversable()->active_window()) + documents.remove(i); + } + + return documents; +} + void Page::clear_selection() { - auto documents = HTML::main_thread_event_loop().documents_in_this_event_loop(); - for (auto const& document : documents) { - if (&document->page() != this) - continue; - + for (auto const& document : documents_in_active_window()) { auto selection = document->get_selection(); if (!selection) continue; @@ -563,12 +573,8 @@ Page::FindInPageResult Page::find_in_page(String const& query, CaseSensitivity c return {}; } - auto documents = HTML::main_thread_event_loop().documents_in_this_event_loop(); Vector> all_matches; - for (auto const& document : documents) { - if (&document->page() != this) - continue; - + for (auto const& document : documents_in_active_window()) { auto matches = document->find_matching_text(query, case_sensitivity); all_matches.extend(move(matches)); } diff --git a/Userland/Libraries/LibWeb/Page/Page.h b/Userland/Libraries/LibWeb/Page/Page.h index 1ef03b7ccf6..25dbbdf4c92 100644 --- a/Userland/Libraries/LibWeb/Page/Page.h +++ b/Userland/Libraries/LibWeb/Page/Page.h @@ -201,6 +201,8 @@ private: JS::GCPtr media_context_menu_element(); + Vector> documents_in_active_window() const; + void update_find_in_page_selection(); JS::NonnullGCPtr m_client;