diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp index 7f744f77d77..5592019192f 100644 --- a/Libraries/LibWeb/Editing/Commands.cpp +++ b/Libraries/LibWeb/Editing/Commands.cpp @@ -2228,6 +2228,33 @@ bool command_remove_format_action(DOM::Document& document, String const&) return true; } +// https://w3c.github.io/editing/docs/execCommand/#the-selectall-command +bool command_select_all_action(DOM::Document& document, String const&) +{ + // NOTE: The spec mentions "This is totally broken". So fair warning :^) + + // 1. Let target be the body element of the context object. + GC::Ptr target = document.body(); + + // 2. If target is null, let target be the context object's documentElement. + if (!target) + target = document.document_element(); + + // 3. If target is null, call getSelection() on the context object, and call removeAllRanges() on the result. + auto& selection = *document.get_selection(); + if (!target) { + selection.remove_all_ranges(); + } + + // 4. Otherwise, call getSelection() on the context object, and call selectAllChildren(target) on the result. + else { + MUST(selection.select_all_children(*target)); + } + + // 5. Return true. + return true; +} + // https://w3c.github.io/editing/docs/execCommand/#the-strikethrough-command bool command_strikethrough_action(DOM::Document& document, String const&) { @@ -2602,6 +2629,11 @@ static Array const commands { .command = CommandNames::removeFormat, .action = command_remove_format_action, }, + // https://w3c.github.io/editing/docs/execCommand/#the-selectall-command + CommandDefinition { + .command = CommandNames::selectAll, + .action = command_select_all_action, + }, // https://w3c.github.io/editing/docs/execCommand/#the-strikethrough-command CommandDefinition { .command = CommandNames::strikethrough, diff --git a/Libraries/LibWeb/Editing/Commands.h b/Libraries/LibWeb/Editing/Commands.h index eb9ca1c695a..611b3eb781a 100644 --- a/Libraries/LibWeb/Editing/Commands.h +++ b/Libraries/LibWeb/Editing/Commands.h @@ -75,6 +75,7 @@ bool command_justify_right_state(DOM::Document const&); String command_justify_right_value(DOM::Document const&); bool command_outdent_action(DOM::Document&, String const&); bool command_remove_format_action(DOM::Document&, String const&); +bool command_select_all_action(DOM::Document&, String const&); bool command_strikethrough_action(DOM::Document&, String const&); bool command_style_with_css_action(DOM::Document&, String const&); bool command_style_with_css_state(DOM::Document const&); diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-selectAll.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-selectAll.txt new file mode 100644 index 00000000000..aa14bdf2a2c --- /dev/null +++ b/Tests/LibWeb/Text/expected/Editing/execCommand-selectAll.txt @@ -0,0 +1,3 @@ +No range. +DIV 0 - DIV 0 +BODY 0 - BODY 5 diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html b/Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html new file mode 100644 index 00000000000..15c278e9fa4 --- /dev/null +++ b/Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html @@ -0,0 +1,37 @@ + +
+ foobar + +
+