mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-28 19:59:17 +00:00
LibWeb: Implement the "underline" editing command
This commit is contained in:
parent
679fbb5eda
commit
043a28baeb
Notes:
github-actions[bot]
2025-01-10 22:36:05 +00:00
Author: https://github.com/gmta
Commit: 043a28baeb
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3216
4 changed files with 62 additions and 0 deletions
|
@ -1422,6 +1422,23 @@ bool command_superscript_indeterminate(DOM::Document const& document)
|
||||||
return has_mixed_value;
|
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 {
|
static Array const commands {
|
||||||
// https://w3c.github.io/editing/docs/execCommand/#the-backcolor-command
|
// https://w3c.github.io/editing/docs/execCommand/#the-backcolor-command
|
||||||
CommandDefinition {
|
CommandDefinition {
|
||||||
|
@ -1534,6 +1551,12 @@ static Array const commands {
|
||||||
.indeterminate = command_superscript_indeterminate,
|
.indeterminate = command_superscript_indeterminate,
|
||||||
.inline_activated_values = { "superscript"sv },
|
.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)
|
Optional<CommandDefinition const&> find_command_definition(FlyString const& command)
|
||||||
|
|
|
@ -51,5 +51,6 @@ bool command_subscript_action(DOM::Document&, String const&);
|
||||||
bool command_subscript_indeterminate(DOM::Document const&);
|
bool command_subscript_indeterminate(DOM::Document const&);
|
||||||
bool command_superscript_action(DOM::Document&, String const&);
|
bool command_superscript_action(DOM::Document&, String const&);
|
||||||
bool command_superscript_indeterminate(DOM::Document const&);
|
bool command_superscript_indeterminate(DOM::Document const&);
|
||||||
|
bool command_underline_action(DOM::Document&, String const&);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
34
Tests/LibWeb/Text/input/Editing/execCommand-underline.html
Normal file
34
Tests/LibWeb/Text/input/Editing/execCommand-underline.html
Normal 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>
|
Loading…
Add table
Add a link
Reference in a new issue