Skip to content

Commit

Permalink
LibWeb: Limit find in page query to documents in the active window
Browse files Browse the repository at this point in the history
Previously, refreshing the current page and doing a new find in page
query would result in duplicate matches being returned.
  • Loading branch information
tcl3 committed Jun 14, 2024
1 parent ec4d298 commit 484bcd3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 15 additions & 9 deletions Userland/Libraries/LibWeb/Page/Page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,23 @@ void Page::set_user_style(String source)
}
}

void Page::clear_selection()
Vector<JS::Handle<DOM::Document>> 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 (auto const& document : documents) {
if (&document->page() != this)
continue;
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()
{
for (auto const& document : documents_in_active_window()) {
auto selection = document->get_selection();
if (!selection)
continue;
Expand All @@ -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<JS::Handle<DOM::Range>> 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));
}
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/Page/Page.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ class Page final : public JS::Cell {

JS::GCPtr<HTML::HTMLMediaElement> media_context_menu_element();

Vector<JS::Handle<DOM::Document>> documents_in_active_window() const;

void update_find_in_page_selection();

JS::NonnullGCPtr<PageClient> m_client;
Expand Down

0 comments on commit 484bcd3

Please sign in to comment.