Truly independent web browser
LibWeb: Scroll a find-in-page match itself into view
sideshowbarker4563cf98deee
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex f9074120296..a86fece8121 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -4584,8 +4584,9 @@ void scroll_a_target_into_view(Layout::BegunRead const& read, ScrollIntoViewTargreturn element->get_bounding_client_rect();},[](Range* range) {- // AD-HOC: Text-fragment scrolling centers the beginning of a match. Using the union- // of every client rect can put the beginning of a long passage outside the viewport.+ // AD-HOC: A range is scrolled to by its beginning, as text-fragment and find-in-page scrolling show a+ // match. Using the union of every client rect can put the beginning of a long passage outside+ // the viewport.auto rects = range->client_rects();if (rects.is_empty())return CSSPixelRect {};diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cppindex 2be87874276..d6ea0e108e6 100644--- a/Libraries/LibWeb/Page/Page.cpp+++ b/Libraries/LibWeb/Page/Page.cpp@@ -2363,17 +2363,20 @@ void Page::update_find_in_page_active_match(Vector<GC::Root<DOM::Range>> const&auto current_range = matches[m_find_in_page_match_index];set_find_in_page_active_match(current_range.ptr());- auto common_ancestor_container = current_range->common_ancestor_container();- if (!common_ancestor_container->document().window())+ auto& document = current_range->start_container()->document();+ if (!document.window())return;- if (auto element = common_ancestor_container->parent_element()) {- DOM::Element::ScrollIntoViewOptions scroll_options;- scroll_options.block = DOM::Element::ScrollLogicalPosition::Nearest;- scroll_options.inline_ = DOM::Element::ScrollLogicalPosition::Nearest;- scroll_options.behavior = DOM::Element::ScrollBehavior::Instant;- element->scroll_into_view(scroll_options, nullptr);- }+ // NB: Scroll the matched text itself into view. An element around it can be wider than the viewport, as a <pre> of+ // long lines is, and then scrolling it into view leaves the match where it is.+ document.update_layout(DOM::UpdateLayoutReason::RangeGetClientRects);+ Layout::ForcedReadScope read { document };+ DOM::scroll_a_target_into_view(+ read,+ DOM::ScrollIntoViewTarget { current_range.ptr() },+ DOM::Element::ScrollBehavior::Instant,+ DOM::Element::ScrollLogicalPosition::Nearest,+ DOM::Element::ScrollLogicalPosition::Nearest);}void Page::update_find_in_page_highlighted_matches(Vector<GC::Root<DOM::Range>> const& matches, bool highlight_all_matches)diff --git a/Tests/LibWeb/Text/expected/find-in-page-scrolls-match-into-view.txt b/Tests/LibWeb/Text/expected/find-in-page-scrolls-match-into-view.txtnew file mode 100644index 00000000000..2f7330da695--- /dev/null+++ b/Tests/LibWeb/Text/expected/find-in-page-scrolls-match-into-view.txt@@ -0,0 +1,2 @@+second match visible: true+first match visible again after wrapping: truediff --git a/Tests/LibWeb/Text/input/find-in-page-scrolls-match-into-view.html b/Tests/LibWeb/Text/input/find-in-page-scrolls-match-into-view.htmlnew file mode 100644index 00000000000..2f39a2fb85d--- /dev/null+++ b/Tests/LibWeb/Text/input/find-in-page-scrolls-match-into-view.html@@ -0,0 +1,26 @@+<!DOCTYPE html>+<style>+ body { margin: 0; }+ #wide { margin: 0; width: 400vw; }+ #far { position: absolute; left: 200vw; top: 200vh; }+</style>+<pre id="wide">needle</pre>+<span id="far">needle</span>+<script src="include.js"></script>+<script>+ test(() => {+ const isVisible = element => {+ const range = document.createRange();+ range.selectNodeContents(element);+ const rect = range.getBoundingClientRect();+ return rect.left >= 0 && rect.right <= document.documentElement.clientWidth+ && rect.top >= 0 && rect.bottom <= document.documentElement.clientHeight;+ };+ internals.findInPage("needle");+ internals.findInPageNextMatch();+ println(`second match visible: ${isVisible(document.getElementById("far"))}`);+ internals.findInPageNextMatch();+ println(`first match visible again after wrapping: ${isVisible(document.getElementById("wide"))}`);+ internals.findInPageEnd();+ });+</script>