From c3a5e8e2660cc7867856979795a04d712191a896 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Wed, 14 May 2025 15:36:52 +0200 Subject: [PATCH] LibWeb: Invalidate viewport's text blocks cache on layout update 156c1083e9de23a93075ec9e3c55cd5f93c48896 introduced a text blocks cache for better performance when searching through text on a page, but when we partially recreate the layout tree, this cache does not get invalidated. We now rebuild the entire text blocks cache after a layout update. --- Libraries/LibWeb/DOM/Document.cpp | 4 +++ Libraries/LibWeb/Layout/Viewport.h | 1 + .../expected/HTML/Window-find-mutations.txt | 3 ++ .../input/HTML/Window-find-mutations.html | 30 +++++++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 Tests/LibWeb/Text/expected/HTML/Window-find-mutations.txt create mode 100644 Tests/LibWeb/Text/input/HTML/Window-find-mutations.html diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 7d719a8baa1..e25b32c49e2 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -1304,6 +1304,10 @@ void Document::update_layout(UpdateLayoutReason reason) if (m_created_for_appropriate_template_contents) return; + // Clear text blocks cache so we rebuild them on the next find action. + if (m_layout_root) + m_layout_root->invalidate_text_blocks_cache(); + invalidate_display_list(); auto* document_element = this->document_element(); diff --git a/Libraries/LibWeb/Layout/Viewport.h b/Libraries/LibWeb/Layout/Viewport.h index f947d4fca53..f6b6d32e0eb 100644 --- a/Libraries/LibWeb/Layout/Viewport.h +++ b/Libraries/LibWeb/Layout/Viewport.h @@ -28,6 +28,7 @@ public: Vector positions; }; Vector const& text_blocks(); + void invalidate_text_blocks_cache() { m_text_blocks.clear(); } const DOM::Document& dom_node() const { return static_cast(*Node::dom_node()); } diff --git a/Tests/LibWeb/Text/expected/HTML/Window-find-mutations.txt b/Tests/LibWeb/Text/expected/HTML/Window-find-mutations.txt new file mode 100644 index 00000000000..2fbc7d4daf6 --- /dev/null +++ b/Tests/LibWeb/Text/expected/HTML/Window-find-mutations.txt @@ -0,0 +1,3 @@ +Selection: [object Text] 0 - [object Text] 6 +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 new file mode 100644 index 00000000000..1046e6986c5 --- /dev/null +++ b/Tests/LibWeb/Text/input/HTML/Window-find-mutations.html @@ -0,0 +1,30 @@ + + +
foobar
+