Skip to content

Commit

Permalink
LibWeb: Add WrapAround option to find in page
Browse files Browse the repository at this point in the history
This allows `Page::find_in_page()` to optionally not return a result if
the start or end of the document has been reached.
  • Loading branch information
tcl3 committed Jun 26, 2024
1 parent 7ce350b commit f052097
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Userland/Libraries/LibWeb/Page/Page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,15 +619,21 @@ Page::FindInPageResult Page::perform_find_in_page_query(FindInPageQuery const& q

if (direction.has_value()) {
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;
else
} else {
m_find_in_page_match_index++;
}
} else {
if (m_find_in_page_match_index == 0)
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;
else
} else {
m_find_in_page_match_index--;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions Userland/Libraries/LibWeb/Page/Page.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,14 @@ class Page final : public JS::Cell {

void clear_selection();

enum class WrapAround {
Yes,
No,
};
struct FindInPageQuery {
String string {};
CaseSensitivity case_sensitivity { CaseSensitivity::CaseInsensitive };
WrapAround wrap_around { WrapAround::Yes };
};
struct FindInPageResult {
size_t current_match_index { 0 };
Expand Down

0 comments on commit f052097

Please sign in to comment.