Skip to content

Commit

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

// https://w3c.github.io/editing/docs/execCommand/#the-fontname-command
bool command_font_name_action(DOM::Document& document, String const& value)
{
// Set the selection's value to value, then return true.
set_the_selections_value(document, CommandNames::fontName, value);
return true;
}

// https://w3c.github.io/editing/docs/execCommand/#the-forwarddelete-command
bool command_forward_delete_action(DOM::Document& document, String const&)
{
Expand Down Expand Up @@ -1127,6 +1135,12 @@ static Array const commands {
.action = command_default_paragraph_separator_action,
.value = command_default_paragraph_separator_value,
},
// https://w3c.github.io/editing/docs/execCommand/#the-fontname-command
CommandDefinition {
.command = CommandNames::fontName,
.action = command_font_name_action,
.relevant_css_property = CSS::PropertyID::FontFamily,
},
// https://w3c.github.io/editing/docs/execCommand/#the-forwarddelete-command
CommandDefinition {
.command = CommandNames::forwardDelete,
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 @@ -35,6 +35,7 @@ bool command_create_link_action(DOM::Document&, String const&);
bool command_default_paragraph_separator_action(DOM::Document&, String const&);
String command_default_paragraph_separator_value(DOM::Document const&);
bool command_delete_action(DOM::Document&, String const&);
bool command_font_name_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&);
Expand Down
2 changes: 2 additions & 0 deletions Tests/LibWeb/Text/expected/Editing/execCommand-fontName.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo<font face="monospace" style="font-family: monospace;">bar</font>
<font face="sans-serif" style="font-family: sans-serif;">foo</font><font face="sans-serif" style="font-family: sans-serif;"><font style="">bar</font></font>
22 changes: 22 additions & 0 deletions Tests/LibWeb/Text/input/Editing/execCommand-fontName.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');

// Set fontName for 'bar'
range.setStart(divElm.childNodes[0], 3);
range.setEnd(divElm.childNodes[0], 6);
document.execCommand('fontName', false, 'monospace');
println(divElm.innerHTML);

// Set fontName for the entire editable div
range.setStart(divElm, 0);
range.setEnd(divElm, 2);
document.execCommand('fontName', false, 'sans-serif');
println(divElm.innerHTML);
});
</script>

0 comments on commit 19ad5bf

Please sign in to comment.