LibWeb: Add WrapAround option to find in page

This allows `Page::find_in_page()` to optionally not return a result if
the start or end of the document has been reached.
This commit is contained in:
Tim Ledbetter 2024-06-25 22:18:49 +01:00 committed by Andreas Kling
commit cda31615da
Notes: sideshowbarker 2024-07-16 17:05:37 +09:00
2 changed files with 15 additions and 4 deletions

View file

@ -619,17 +619,23 @@ Page::FindInPageResult Page::perform_find_in_page_query(FindInPageQuery const& q
if (direction.has_value()) { if (direction.has_value()) {
if (direction.value() == SearchDirection::Forward) { if (direction.value() == SearchDirection::Forward) {
if (m_find_in_page_match_index >= all_matches.size() - 1) if (m_find_in_page_match_index >= all_matches.size() - 1) {
if (query.wrap_around == WrapAround::No)
return {};
m_find_in_page_match_index = 0; m_find_in_page_match_index = 0;
else
m_find_in_page_match_index++;
} else { } else {
if (m_find_in_page_match_index == 0) m_find_in_page_match_index++;
}
} else {
if (m_find_in_page_match_index == 0) {
if (query.wrap_around == WrapAround::No)
return {};
m_find_in_page_match_index = all_matches.size() - 1; m_find_in_page_match_index = all_matches.size() - 1;
else } else {
m_find_in_page_match_index--; m_find_in_page_match_index--;
} }
} }
}
update_find_in_page_selection(all_matches); update_find_in_page_selection(all_matches);

View file

@ -190,9 +190,14 @@ public:
void clear_selection(); void clear_selection();
enum class WrapAround {
Yes,
No,
};
struct FindInPageQuery { struct FindInPageQuery {
String string {}; String string {};
CaseSensitivity case_sensitivity { CaseSensitivity::CaseInsensitive }; CaseSensitivity case_sensitivity { CaseSensitivity::CaseInsensitive };
WrapAround wrap_around { WrapAround::Yes };
}; };
struct FindInPageResult { struct FindInPageResult {
size_t current_match_index { 0 }; size_t current_match_index { 0 };