diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp index a059babdc31..695c3bd5999 100644 --- a/Libraries/LibWeb/Editing/Commands.cpp +++ b/Libraries/LibWeb/Editing/Commands.cpp @@ -593,6 +593,28 @@ String command_font_size_value(DOM::Document const& document) return legacy_font_size(pixel_size.to_int()); } +// https://w3c.github.io/editing/docs/execCommand/#the-forecolor-command +bool command_fore_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::foreColor, resulting_value); + + // 4. Return true. + return true; +} + // https://w3c.github.io/editing/docs/execCommand/#the-forwarddelete-command bool command_forward_delete_action(DOM::Document& document, String const&) { @@ -1229,6 +1251,12 @@ static Array const commands { .value = command_font_size_value, .relevant_css_property = CSS::PropertyID::FontSize, }, + // https://w3c.github.io/editing/docs/execCommand/#the-forecolor-command + CommandDefinition { + .command = CommandNames::foreColor, + .action = command_fore_color_action, + .relevant_css_property = CSS::PropertyID::Color, + }, // https://w3c.github.io/editing/docs/execCommand/#the-forwarddelete-command CommandDefinition { .command = CommandNames::forwardDelete, diff --git a/Libraries/LibWeb/Editing/Commands.h b/Libraries/LibWeb/Editing/Commands.h index af971161fda..205caa0bdee 100644 --- a/Libraries/LibWeb/Editing/Commands.h +++ b/Libraries/LibWeb/Editing/Commands.h @@ -38,6 +38,7 @@ bool command_delete_action(DOM::Document&, String const&); bool command_font_name_action(DOM::Document&, String const&); bool command_font_size_action(DOM::Document&, String const&); String command_font_size_value(DOM::Document const&); +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&); diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-foreColor.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-foreColor.txt new file mode 100644 index 00000000000..12af97fceb2 --- /dev/null +++ b/Tests/LibWeb/Text/expected/Editing/execCommand-foreColor.txt @@ -0,0 +1,2 @@ +Div contents: "foobar" +Div contents: "foobar" diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-foreColor.html b/Tests/LibWeb/Text/input/Editing/execCommand-foreColor.html new file mode 100644 index 00000000000..c1e534dae5e --- /dev/null +++ b/Tests/LibWeb/Text/input/Editing/execCommand-foreColor.html @@ -0,0 +1,22 @@ + +
foobar
+