From 7ae7e3eef3d239d27855af02f455c2f61abfbe4e Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 4 Sep 2024 11:29:38 -0400 Subject: [PATCH] LibWeb: Limit select-all actions to editable nodes when they are focused If the user presses ctrl+a inside an element, for example, we now select that element's text only. --- Userland/Libraries/LibWeb/HTML/Navigable.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.cpp b/Userland/Libraries/LibWeb/HTML/Navigable.cpp index b5adf8baa2c..4f0f0e75e01 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Userland/Libraries/LibWeb/HTML/Navigable.cpp @@ -2147,13 +2147,20 @@ void Navigable::select_all() auto document = active_document(); if (!document) return; - auto* body = document->body(); - if (!body) - return; + auto selection = document->get_selection(); if (!selection) return; - (void)selection->select_all_children(*document->body()); + + if (auto position = document->cursor_position(); position && position->node()->is_editable()) { + auto& node = *position->node(); + auto node_length = node.length(); + + (void)selection->set_base_and_extent(node, 0, node, node_length); + document->set_cursor_position(DOM::Position::create(document->realm(), node, node_length)); + } else if (auto* body = document->body()) { + (void)selection->select_all_children(*body); + } } void Navigable::paste(String const& text)