From 1c4328902d3d2ca9b519ded17a7acb9cb105969e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 11 Jan 2023 19:48:17 +0100 Subject: LibWeb: Implement BrowsingContext::select_all() in terms of Selection We do what other browsers do and create a selection anchored at the document's body element. --- Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp | 47 ++++------------------ 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp index d25709cf2c..5ef739105f 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp @@ -508,47 +508,16 @@ DeprecatedString BrowsingContext::selected_text() const void BrowsingContext::select_all() { - if (!active_document()) + auto* document = active_document(); + if (!document) return; - auto* layout_root = active_document()->layout_node(); - if (!layout_root) + auto* body = document->body(); + if (!body) return; - - Layout::Node const* first_layout_node = layout_root; - - for (;;) { - auto* next = first_layout_node->next_in_pre_order(); - if (!next) - break; - first_layout_node = next; - if (is(*first_layout_node)) - break; - } - - Layout::Node const* last_layout_node = first_layout_node; - - for (Layout::Node const* layout_node = first_layout_node; layout_node; layout_node = layout_node->next_in_pre_order()) { - if (is(*layout_node)) - last_layout_node = layout_node; - } - - VERIFY(first_layout_node); - VERIFY(last_layout_node); - - int last_layout_node_index_in_node = 0; - if (is(*last_layout_node)) { - auto const& text_for_rendering = verify_cast(*last_layout_node).text_for_rendering(); - if (!text_for_rendering.is_empty()) - last_layout_node_index_in_node = text_for_rendering.length() - 1; - } - - auto start = Layout::LayoutPosition { - JS::make_handle(const_cast(first_layout_node)), 0 - }; - auto end = Layout::LayoutPosition { - JS::make_handle(const_cast(last_layout_node)), last_layout_node_index_in_node - }; - layout_root->set_selection({ move(start), move(end) }); + auto selection = document->get_selection(); + if (!selection) + return; + (void)selection->select_all_children(*document->body()); } void BrowsingContext::register_viewport_client(ViewportClient& client) -- cgit v1.2.3