Skip to content

Commit

Permalink
LibWeb: Implement the "backColor" and "hiliteColor" editing commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gmta committed Jan 8, 2025
1 parent 2727fb7 commit a21f081
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Libraries/LibWeb/Editing/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@

namespace Web::Editing {

// https://w3c.github.io/editing/docs/execCommand/#the-backcolor-command
bool command_back_color_action(DOM::Document& document, String const& value)
{
// 1. If value is not a valid CSS color, prepend "#" to it.
auto resulting_value = value;
if (!Color::from_string(resulting_value).has_value()) {
resulting_value = MUST(String::formatted("#{}", resulting_value));

// 2. If value is still not a valid CSS color, or if it is currentColor, return false.
if (!Color::from_string(resulting_value).has_value()) {
// FIXME: Also return false in case of currentColor.
return false;
}
}

// 3. Set the selection's value to value.
set_the_selections_value(document, CommandNames::backColor, resulting_value);

// 4. Return true.
return true;
}

// https://w3c.github.io/editing/docs/execCommand/#the-bold-command
bool command_bold_action(DOM::Document& document, String const&)
{
Expand Down Expand Up @@ -1042,6 +1064,12 @@ bool command_style_with_css_state(DOM::Document const& document)
}

static Array const commands {
// https://w3c.github.io/editing/docs/execCommand/#the-backcolor-command
CommandDefinition {
.command = CommandNames::backColor,
.action = command_back_color_action,
.relevant_css_property = CSS::PropertyID::BackgroundColor,
},
// https://w3c.github.io/editing/docs/execCommand/#the-bold-command
CommandDefinition {
.command = CommandNames::bold,
Expand All @@ -1067,6 +1095,12 @@ static Array const commands {
.action = command_forward_delete_action,
.preserves_overrides = true,
},
// https://w3c.github.io/editing/docs/execCommand/#the-hilitecolor-command
CommandDefinition {
.command = CommandNames::hiliteColor,
.action = command_back_color_action, // For historical reasons, backColor and hiliteColor behave identically.
.relevant_css_property = CSS::PropertyID::BackgroundColor,
},
// https://w3c.github.io/editing/docs/execCommand/#the-insertlinebreak-command
CommandDefinition {
.command = CommandNames::insertLineBreak,
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 @@ -29,6 +29,7 @@ struct CommandDefinition {
Optional<CommandDefinition const&> find_command_definition(FlyString const&);

// Command implementations
bool command_back_color_action(DOM::Document&, String const&);
bool command_bold_action(DOM::Document&, String const&);
bool command_default_paragraph_separator_action(DOM::Document&, String const&);
String command_default_paragraph_separator_value(DOM::Document const&);
Expand Down
2 changes: 2 additions & 0 deletions Tests/LibWeb/Text/expected/Editing/execCommand-backColor.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Div contents: "<span style="background-color: rgb(0, 0, 255);">foo</span>bar"
Div contents: "<span style="background-color: rgb(0, 0, 255);">foo</span><span style="background-color: red;">bar</span>"
22 changes: 22 additions & 0 deletions Tests/LibWeb/Text/input/Editing/execCommand-backColor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script src="../include.js"></script>
<div contenteditable="true">foobar</div>
<script>
test(() => {
const range = document.createRange();
getSelection().addRange(range);

const divElm = document.querySelector('div');

// Make 'foo' blue
range.setStart(divElm.childNodes[0], 0);
range.setEnd(divElm.childNodes[0], 3);
document.execCommand('backColor', false, '#0000ff');
println(`Div contents: "${divElm.innerHTML}"`);

// Make 'bar' red
range.setStart(divElm.childNodes[1], 0);
range.setEnd(divElm.childNodes[1], 3);
document.execCommand('hiliteColor', false, 'red');
println(`Div contents: "${divElm.innerHTML}"`);
});
</script>

0 comments on commit a21f081

Please sign in to comment.