From d967f56936e0b2fc1e89310dab96a40c69dbd84b Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Mon, 20 Jan 2025 23:56:55 +0100 Subject: [PATCH] LibWeb: Require existing Selection for `.execCommand("selectAll")` Disable the command if no selection is available. This is a spec bug: https://github.com/w3c/editing/issues/475 Fixes #3325 --- Libraries/LibWeb/Editing/ExecCommand.cpp | 10 +++++----- .../Text/expected/Editing/execCommand-selectAll.txt | 4 ++++ .../Text/input/Editing/execCommand-selectAll.html | 12 ++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Libraries/LibWeb/Editing/ExecCommand.cpp b/Libraries/LibWeb/Editing/ExecCommand.cpp index 3a679a9fde8..0338d15a46a 100644 --- a/Libraries/LibWeb/Editing/ExecCommand.cpp +++ b/Libraries/LibWeb/Editing/ExecCommand.cpp @@ -112,17 +112,17 @@ bool Document::query_command_enabled(FlyString const& command) if (command.is_one_of( Editing::CommandNames::defaultParagraphSeparator, Editing::CommandNames::redo, - Editing::CommandNames::selectAll, Editing::CommandNames::styleWithCSS, Editing::CommandNames::undo, Editing::CommandNames::useCSS)) return true; + // AD-HOC: selectAll requires a selection object to exist. + if (command == Editing::CommandNames::selectAll) + return get_selection(); + // The other commands defined here are enabled if the active range is not null, - auto selection = get_selection(); - if (!selection) - return false; - auto active_range = selection->range(); + auto active_range = Editing::active_range(*this); if (!active_range) return false; diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-selectAll.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-selectAll.txt index aa14bdf2a2c..43f9cabbfac 100644 --- a/Tests/LibWeb/Text/expected/Editing/execCommand-selectAll.txt +++ b/Tests/LibWeb/Text/expected/Editing/execCommand-selectAll.txt @@ -1,3 +1,7 @@ No range. DIV 0 - DIV 0 BODY 0 - BODY 5 +true +false +false +Did not crash! diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html b/Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html index 15c278e9fa4..3f883a64931 100644 --- a/Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html +++ b/Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html @@ -33,5 +33,17 @@ // Perform selectAll document.execCommand('selectAll'); reportSelection(); + + // Report whether command is enabled and make sure it does not crash + const documents = [ + document, + new Document(), + document.implementation.createHTMLDocument(), + ]; + for (const doc of documents) { + println(doc.queryCommandEnabled('selectAll')); + doc.execCommand('selectAll'); + } + println('Did not crash!'); });