Skip to content

Commit

Permalink
LibWeb: Require existing Selection for .execCommand("selectAll")
Browse files Browse the repository at this point in the history
Disable the command if no selection is available. This is a spec bug:

w3c/editing#475

Fixes LadybirdBrowser#3325
  • Loading branch information
gmta committed Jan 21, 2025
1 parent 8b097b3 commit 4ae1651
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Libraries/LibWeb/Editing/ExecCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions Tests/LibWeb/Text/expected/Editing/execCommand-selectAll.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
No range.
DIV 0 - DIV 0
BODY 0 - BODY 5
true
false
false
Did not crash!
12 changes: 12 additions & 0 deletions Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});
</script>

0 comments on commit 4ae1651

Please sign in to comment.