LibWeb: Implement the "italic" editing command

This commit is contained in:
Jelle Raaijmakers 2025-01-08 16:24:38 +01:00 committed by Andreas Kling
parent 228c66f2e1
commit ae12f7036b
Notes: github-actions[bot] 2025-01-10 22:36:38 +00:00
4 changed files with 63 additions and 0 deletions

View file

@ -1189,6 +1189,23 @@ bool command_insert_paragraph_action(DOM::Document& document, String const&)
return true;
}
// https://w3c.github.io/editing/docs/execCommand/#the-italic-command
bool command_italic_action(DOM::Document& document, String const&)
{
// If queryCommandState("italic") returns true, set the selection's value to "normal".
if (document.query_command_state(CommandNames::italic)) {
set_the_selections_value(document, CommandNames::italic, "normal"_string);
}
// Otherwise set the selection's value to "italic".
else {
set_the_selections_value(document, CommandNames::italic, "italic"_string);
}
// Either way, return true.
return true;
}
// https://w3c.github.io/editing/docs/execCommand/#the-stylewithcss-command
bool command_style_with_css_action(DOM::Document& document, String const& value)
{
@ -1281,6 +1298,13 @@ static Array const commands {
.action = command_insert_paragraph_action,
.preserves_overrides = true,
},
// https://w3c.github.io/editing/docs/execCommand/#the-italic-command
CommandDefinition {
.command = CommandNames::italic,
.action = command_italic_action,
.relevant_css_property = CSS::PropertyID::FontStyle,
.inline_activated_values = { "italic"sv, "oblique"sv },
},
// https://w3c.github.io/editing/docs/execCommand/#the-stylewithcss-command
CommandDefinition {
.command = CommandNames::styleWithCSS,

View file

@ -42,6 +42,7 @@ bool command_fore_color_action(DOM::Document&, String const&);
bool command_forward_delete_action(DOM::Document&, String const&);
bool command_insert_linebreak_action(DOM::Document&, String const&);
bool command_insert_paragraph_action(DOM::Document&, String const&);
bool command_italic_action(DOM::Document&, String const&);
bool command_style_with_css_action(DOM::Document&, String const&);
bool command_style_with_css_state(DOM::Document const&);

View file

@ -0,0 +1,4 @@
Div contents: "foobar" state: false selection: #document 0 #document 0
Div contents: "foo<i>bar</i>" state: true selection: #text 0 #text 3
Div contents: "<i>foobar</i>" state: true selection: #text 0 #text 3
Div contents: "<i>foo</i>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('italic')} selection: ${printableSelection()}`);
report();
// Make 'bar' italic
range.setStart(divElm.childNodes[0], 3);
range.setEnd(divElm.childNodes[0], 6);
document.execCommand('italic');
report();
// Make 'foo' italic
range.setStart(divElm.childNodes[0], 0);
range.setEnd(divElm.childNodes[0], 3);
document.execCommand('italic');
report();
// Deitalicize 'bar'
range.setStart(divElm.childNodes[0].childNodes[1], 0);
range.setEnd(divElm.childNodes[0].childNodes[1], 3);
document.execCommand('italic');
report();
});
</script>