From 09463f147d8cf4fd767dee84f1174b90b2384632 Mon Sep 17 00:00:00 2001 From: Chris Mulder Date: Thu, 15 May 2025 22:08:03 +0200 Subject: [PATCH] LibWeb: Do not clear the selection, when query is not found Fixes #4743 --- Libraries/LibWeb/Page/Page.cpp | 4 ++-- Tests/LibWeb/Text/expected/HTML/Window-find-mutations.txt | 1 + Tests/LibWeb/Text/input/HTML/Window-find-mutations.html | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp index 3e0d9419c65..f3cf3062d92 100644 --- a/Libraries/LibWeb/Page/Page.cpp +++ b/Libraries/LibWeb/Page/Page.cpp @@ -726,11 +726,11 @@ Page::FindInPageResult Page::find_in_page_previous_match() void Page::update_find_in_page_selection(Vector> matches) { - clear_selection(); - if (matches.is_empty()) return; + clear_selection(); + auto current_range = matches[m_find_in_page_match_index]; auto common_ancestor_container = current_range->common_ancestor_container(); auto& document = common_ancestor_container->document(); diff --git a/Tests/LibWeb/Text/expected/HTML/Window-find-mutations.txt b/Tests/LibWeb/Text/expected/HTML/Window-find-mutations.txt index 2fbc7d4daf6..e8660396395 100644 --- a/Tests/LibWeb/Text/expected/HTML/Window-find-mutations.txt +++ b/Tests/LibWeb/Text/expected/HTML/Window-find-mutations.txt @@ -1,3 +1,4 @@ Selection: [object Text] 0 - [object Text] 6 Selection: [object Text] 0 - [object Text] 3 +Selection: [object Text] 0 - [object Text] 3 Expected exception: IndexSizeError: Selection.getRangeAt() on empty Selection or with invalid argument diff --git a/Tests/LibWeb/Text/input/HTML/Window-find-mutations.html b/Tests/LibWeb/Text/input/HTML/Window-find-mutations.html index 1046e6986c5..b07476e5109 100644 --- a/Tests/LibWeb/Text/input/HTML/Window-find-mutations.html +++ b/Tests/LibWeb/Text/input/HTML/Window-find-mutations.html @@ -16,6 +16,11 @@ test(() => { document.querySelector('div').childNodes[0].deleteData(3, 3); showSelection(); + // Try to find 'baz'. + window.find('baz'); + // Selection should be untouched, since 'baz' was not found + showSelection(); + // Try to find 'bar'. getSelection().empty(); window.find('bar');