Skip to content

Commit

Permalink
LibWeb: Implement the "strikethrough" editing command
Browse files Browse the repository at this point in the history
  • Loading branch information
gmta committed Jan 8, 2025
1 parent 6c40a69 commit 93a0289
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Libraries/LibWeb/Editing/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,23 @@ bool command_remove_format_action(DOM::Document& document, String const&)
return true;
}

// https://w3c.github.io/editing/docs/execCommand/#the-strikethrough-command
bool command_strikethrough_action(DOM::Document& document, String const&)
{
// If queryCommandState("strikethrough") returns true, set the selection's value to null.
if (document.query_command_state(CommandNames::strikethrough)) {
set_the_selections_value(document, CommandNames::strikethrough, {});
}

// Otherwise set the selection's value to "line-through".
else {
set_the_selections_value(document, CommandNames::strikethrough, "line-through"_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)
{
Expand Down Expand Up @@ -1397,6 +1414,12 @@ static Array const commands {
.command = CommandNames::removeFormat,
.action = command_remove_format_action,
},
// https://w3c.github.io/editing/docs/execCommand/#the-strikethrough-command
CommandDefinition {
.command = CommandNames::strikethrough,
.action = command_strikethrough_action,
.inline_activated_values = { "line-through"sv },
},
// https://w3c.github.io/editing/docs/execCommand/#the-stylewithcss-command
CommandDefinition {
.command = CommandNames::styleWithCSS,
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibWeb/Editing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ 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_remove_format_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_state(DOM::Document const&);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Div contents: "foobar" state: false selection: #document 0 #document 0
Div contents: "foo<s>bar</s>" state: false selection: #text 0 #text 3
Div contents: "<s>foobar</s>" state: false selection: #text 0 #text 3
Div contents: "<s>foo</s>bar" state: false selection: #text 0 #text 3
34 changes: 34 additions & 0 deletions Tests/LibWeb/Text/input/Editing/execCommand-strikethrough.html
Original file line number Diff line number Diff line change
@@ -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('strikethrough')} selection: ${printableSelection()}`);
report();

// Strike through 'bar'
range.setStart(divElm.childNodes[0], 3);
range.setEnd(divElm.childNodes[0], 6);
document.execCommand('strikethrough');
report();

// Strike through 'foo'
range.setStart(divElm.childNodes[0], 0);
range.setEnd(divElm.childNodes[0], 3);
document.execCommand('strikethrough');
report();

// Unstrikethroughify 'bar'
range.setStart(divElm.childNodes[0].childNodes[1], 0);
range.setEnd(divElm.childNodes[0].childNodes[1], 3);
document.execCommand('strikethrough');
report();
});
</script>

0 comments on commit 93a0289

Please sign in to comment.