diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp index 52617dfd854..cb33aba5dcb 100644 --- a/Libraries/LibWeb/Editing/Commands.cpp +++ b/Libraries/LibWeb/Editing/Commands.cpp @@ -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&) { @@ -1030,6 +1052,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, @@ -1055,6 +1083,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, diff --git a/Libraries/LibWeb/Editing/Commands.h b/Libraries/LibWeb/Editing/Commands.h index 94282b29bba..43000f60789 100644 --- a/Libraries/LibWeb/Editing/Commands.h +++ b/Libraries/LibWeb/Editing/Commands.h @@ -29,6 +29,7 @@ struct CommandDefinition { Optional 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&); diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-backColor.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-backColor.txt new file mode 100644 index 00000000000..738319e55c5 --- /dev/null +++ b/Tests/LibWeb/Text/expected/Editing/execCommand-backColor.txt @@ -0,0 +1,2 @@ +Div contents: "foobar" +Div contents: "foobar" diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-backColor.html b/Tests/LibWeb/Text/input/Editing/execCommand-backColor.html new file mode 100644 index 00000000000..7ec26de2ec5 --- /dev/null +++ b/Tests/LibWeb/Text/input/Editing/execCommand-backColor.html @@ -0,0 +1,22 @@ + +
foobar
+