mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 12:19:54 +00:00
LibWeb: Cache text blocks used by find in page
The first step of the find in page algorithm is to walk the layout tree of each document on the page and construct a list of strings against which to search for matches. Previously, this was being done for each new query, even when the page content hadn't been updated. The output of this process is now cached in the viewport node of the associated document. This ensures that this process is no longer repeated unnceessarily.
This commit is contained in:
parent
31698281b6
commit
156c1083e9
Notes:
sideshowbarker
2024-07-17 10:16:43 +09:00
Author: https://github.com/tcl3
Commit: 156c1083e9
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/311
Reviewed-by: https://github.com/kalenikaliaksandr
3 changed files with 81 additions and 54 deletions
|
@ -5111,65 +5111,13 @@ void Document::set_needs_to_refresh_scroll_state(bool b)
|
|||
|
||||
Vector<JS::Handle<DOM::Range>> Document::find_matching_text(String const& query, CaseSensitivity case_sensitivity)
|
||||
{
|
||||
if (!document_element() || !document_element()->layout_node())
|
||||
if (!layout_node())
|
||||
return {};
|
||||
|
||||
struct TextPosition {
|
||||
DOM::Text& dom_node;
|
||||
size_t start_offset { 0 };
|
||||
};
|
||||
|
||||
struct TextBlock {
|
||||
String text;
|
||||
Vector<TextPosition> positions;
|
||||
};
|
||||
|
||||
auto gather_text_blocks = [&]() -> Vector<TextBlock> {
|
||||
StringBuilder builder;
|
||||
size_t current_start_position = 0;
|
||||
Vector<TextPosition> text_positions;
|
||||
Vector<TextBlock> text_blocks;
|
||||
document_element()->layout_node()->for_each_in_inclusive_subtree([&](auto const& layout_node) {
|
||||
if (layout_node.display().is_none() || !layout_node.paintable() || !layout_node.paintable()->is_visible())
|
||||
return TraversalDecision::Continue;
|
||||
|
||||
if (layout_node.is_block_container()) {
|
||||
if (!builder.is_empty()) {
|
||||
text_blocks.append({ builder.to_string_without_validation(), text_positions });
|
||||
current_start_position = 0;
|
||||
text_positions.clear_with_capacity();
|
||||
builder.clear();
|
||||
}
|
||||
return TraversalDecision::Continue;
|
||||
}
|
||||
|
||||
if (layout_node.is_text_node()) {
|
||||
auto const& text_node = verify_cast<Layout::TextNode>(layout_node);
|
||||
auto& dom_node = const_cast<DOM::Text&>(text_node.dom_node());
|
||||
if (text_positions.is_empty()) {
|
||||
text_positions.empend(dom_node);
|
||||
} else {
|
||||
text_positions.empend(dom_node, current_start_position);
|
||||
}
|
||||
|
||||
auto const& current_node_text = text_node.text_for_rendering();
|
||||
current_start_position += current_node_text.bytes_as_string_view().length();
|
||||
builder.append(move(current_node_text));
|
||||
}
|
||||
|
||||
return TraversalDecision::Continue;
|
||||
});
|
||||
|
||||
if (!builder.is_empty())
|
||||
text_blocks.append({ builder.to_string_without_validation(), text_positions });
|
||||
|
||||
return text_blocks;
|
||||
};
|
||||
|
||||
// Ensure the layout tree exists before searching for text matches.
|
||||
update_layout();
|
||||
|
||||
auto text_blocks = gather_text_blocks();
|
||||
auto const& text_blocks = layout_node()->text_blocks();
|
||||
if (text_blocks.is_empty())
|
||||
return {};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue