mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-05 23:59:49 +00:00
LibWeb: Implement the "selectAll" editing command
This commit is contained in:
parent
03bcfb9b8c
commit
70af48c18b
Notes:
github-actions[bot]
2025-01-10 22:34:39 +00:00
Author: https://github.com/gmta
Commit: 70af48c18b
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3216
4 changed files with 73 additions and 0 deletions
|
@ -2228,6 +2228,33 @@ bool command_remove_format_action(DOM::Document& document, String const&)
|
||||||
return true;
|
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<DOM::Node> 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
|
// https://w3c.github.io/editing/docs/execCommand/#the-strikethrough-command
|
||||||
bool command_strikethrough_action(DOM::Document& document, String const&)
|
bool command_strikethrough_action(DOM::Document& document, String const&)
|
||||||
{
|
{
|
||||||
|
@ -2602,6 +2629,11 @@ static Array const commands {
|
||||||
.command = CommandNames::removeFormat,
|
.command = CommandNames::removeFormat,
|
||||||
.action = command_remove_format_action,
|
.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
|
// https://w3c.github.io/editing/docs/execCommand/#the-strikethrough-command
|
||||||
CommandDefinition {
|
CommandDefinition {
|
||||||
.command = CommandNames::strikethrough,
|
.command = CommandNames::strikethrough,
|
||||||
|
|
|
@ -75,6 +75,7 @@ bool command_justify_right_state(DOM::Document const&);
|
||||||
String command_justify_right_value(DOM::Document const&);
|
String command_justify_right_value(DOM::Document const&);
|
||||||
bool command_outdent_action(DOM::Document&, String const&);
|
bool command_outdent_action(DOM::Document&, String const&);
|
||||||
bool command_remove_format_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_strikethrough_action(DOM::Document&, String const&);
|
||||||
bool command_style_with_css_action(DOM::Document&, String const&);
|
bool command_style_with_css_action(DOM::Document&, String const&);
|
||||||
bool command_style_with_css_state(DOM::Document const&);
|
bool command_style_with_css_state(DOM::Document const&);
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
No range.
|
||||||
|
DIV 0 - DIV 0
|
||||||
|
BODY 0 - BODY 5
|
37
Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html
Normal file
37
Tests/LibWeb/Text/input/Editing/execCommand-selectAll.html
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
<script src="../include.js"></script>
|
||||||
|
<div contenteditable="true">
|
||||||
|
<span>foobar</span>
|
||||||
|
<ul>
|
||||||
|
<li>foo</li>
|
||||||
|
<li>bar</li>
|
||||||
|
<li>baz</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
test(() => {
|
||||||
|
const selection = getSelection();
|
||||||
|
|
||||||
|
const reportSelection = () => {
|
||||||
|
if (selection.rangeCount === 0) {
|
||||||
|
println('No range.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const range = selection.getRangeAt(0);
|
||||||
|
println(`${range.startContainer.nodeName} ${range.startOffset} - ${range.endContainer.nodeName} ${range.endOffset}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
reportSelection();
|
||||||
|
|
||||||
|
let divElm = document.querySelector('div');
|
||||||
|
let range = document.createRange();
|
||||||
|
range.setStart(divElm, 0);
|
||||||
|
range.setEnd(divElm, 0);
|
||||||
|
selection.addRange(range);
|
||||||
|
|
||||||
|
reportSelection();
|
||||||
|
|
||||||
|
// Perform selectAll
|
||||||
|
document.execCommand('selectAll');
|
||||||
|
reportSelection();
|
||||||
|
});
|
||||||
|
</script>
|
Loading…
Add table
Add a link
Reference in a new issue