LibWeb: Implement the "underline" editing command

This commit is contained in:
Jelle Raaijmakers 2025-01-08 17:26:06 +01:00 committed by Andreas Kling
parent 679fbb5eda
commit 043a28baeb
Notes: github-actions[bot] 2025-01-10 22:36:05 +00:00
4 changed files with 62 additions and 0 deletions

View file

@ -1422,6 +1422,23 @@ bool command_superscript_indeterminate(DOM::Document const& document)
return has_mixed_value;
}
// https://w3c.github.io/editing/docs/execCommand/#the-underline-command
bool command_underline_action(DOM::Document& document, String const&)
{
// If queryCommandState("underline") returns true, set the selection's value to null.
if (document.query_command_state(CommandNames::underline)) {
set_the_selections_value(document, CommandNames::underline, {});
}
// Otherwise set the selection's value to "underline".
else {
set_the_selections_value(document, CommandNames::underline, "underline"_string);
}
// Either way, return true.
return true;
}
static Array const commands {
// https://w3c.github.io/editing/docs/execCommand/#the-backcolor-command
CommandDefinition {
@ -1534,6 +1551,12 @@ static Array const commands {
.indeterminate = command_superscript_indeterminate,
.inline_activated_values = { "superscript"sv },
},
// https://w3c.github.io/editing/docs/execCommand/#the-underline-command
CommandDefinition {
.command = CommandNames::underline,
.action = command_underline_action,
.inline_activated_values = { "underline"sv },
},
};
Optional<CommandDefinition const&> find_command_definition(FlyString const& command)

View file

@ -51,5 +51,6 @@ bool command_subscript_action(DOM::Document&, String const&);
bool command_subscript_indeterminate(DOM::Document const&);
bool command_superscript_action(DOM::Document&, String const&);
bool command_superscript_indeterminate(DOM::Document const&);
bool command_underline_action(DOM::Document&, String const&);
}

View file

@ -0,0 +1,4 @@
Div contents: "foobar" state: false selection: #document 0 #document 0
Div contents: "foo<u>bar</u>" state: true selection: #text 0 #text 3
Div contents: "<u>foobar</u>" state: true selection: #text 0 #text 3
Div contents: "<u>foo</u>bar" state: false selection: #text 0 #text 3

View file

@ -0,0 +1,34 @@
<script src="../include.js"></script>
<div contenteditable="true">foobar</div>
<script>
test(() => {
const range = document.createRange();
getSelection().addRange(range);
const divElm = document.querySelector('div');
const printableSelection = () => {
let activeRange = getSelection().getRangeAt(0);
return `${activeRange.startContainer.nodeName} ${activeRange.startOffset} ${activeRange.endContainer.nodeName} ${activeRange.endOffset}`;
};
const report = () => println(`Div contents: "${divElm.innerHTML}" state: ${document.queryCommandState('underline')} selection: ${printableSelection()}`);
report();
// Underline 'bar'
range.setStart(divElm.childNodes[0], 3);
range.setEnd(divElm.childNodes[0], 6);
document.execCommand('underline');
report();
// Underline 'foo'
range.setStart(divElm.childNodes[0], 0);
range.setEnd(divElm.childNodes[0], 3);
document.execCommand('underline');
report();
// Ununderlinify 'bar'
range.setStart(divElm.childNodes[0].childNodes[1], 0);
range.setEnd(divElm.childNodes[0].childNodes[1], 3);
document.execCommand('underline');
report();
});
</script>