LibWeb: Implement document.execCommand("delete")

To facilitate the implementation of "delete" and all associated
algorithms, split off this piece of `Document` into a separate
directory.

This sets up the infrastructure for arbitrary commands to be supported.
This commit is contained in:
Jelle Raaijmakers 2024-11-27 11:57:12 +01:00 committed by Andreas Kling
parent c87960f8f3
commit 7bb865052a
Notes: github-actions[bot] 2024-11-30 16:37:23 +00:00
12 changed files with 2227 additions and 48 deletions

View file

@ -26,6 +26,7 @@
#include <LibWeb/Bindings/WindowExposedInterfaces.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/MutationType.h>
#include <LibWeb/Editing/CommandNames.h>
#include <LibWeb/HTML/AttributeNames.h>
#include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
#include <LibWeb/HTML/CustomElements/CustomElementReactionNames.h>
@ -101,6 +102,7 @@ ErrorOr<void> initialize_main_thread_vm(HTML::EventLoop::Type type)
// These strings could potentially live on the VM similar to CommonPropertyNames.
DOM::MutationType::initialize_strings();
Editing::CommandNames::initialize_strings();
HTML::AttributeNames::initialize_strings();
HTML::CustomElementReactionNames::initialize_strings();
HTML::EventNames::initialize_strings();

View file

@ -222,6 +222,10 @@ set(SOURCES
DOMURL/URLSearchParams.cpp
DOMURL/URLSearchParamsIterator.cpp
Dump.cpp
Editing/CommandNames.cpp
Editing/Commands.cpp
Editing/ExecCommand.cpp
Editing/Internal/Algorithms.cpp
Encoding/TextDecoder.cpp
Encoding/TextEncoder.cpp
EntriesAPI/FileSystemEntry.cpp

View file

@ -5077,48 +5077,6 @@ JS::Value Document::named_item_value(FlyString const& name) const
});
}
// https://w3c.github.io/editing/docs/execCommand/#execcommand()
bool Document::exec_command(String const& command, bool show_ui, String const& value)
{
dbgln("FIXME: document.execCommand(\"{}\", {}, \"{}\")", command, show_ui, value);
return false;
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandenabled()
bool Document::query_command_enabled(String const& command)
{
dbgln("FIXME: document.queryCommandEnabled(\"{}\")", command);
return false;
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandindeterm()
bool Document::query_command_indeterm(String const& command)
{
dbgln("FIXME: document.queryCommandIndeterm(\"{}\")", command);
return false;
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandstate()
bool Document::query_command_state(String const& command)
{
dbgln("FIXME: document.queryCommandState(\"{}\")", command);
return false;
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandsupported()
bool Document::query_command_supported(String const& command)
{
dbgln("FIXME: document.queryCommandSupported(\"{}\")", command);
return false;
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandvalue()
String Document::query_command_value(String const& command)
{
dbgln("FIXME: document.queryCommandValue(\"{}\")", command);
return String {};
}
// https://drafts.csswg.org/resize-observer-1/#calculate-depth-for-node
static size_t calculate_depth_for_node(Node const& node)
{

View file

@ -569,12 +569,12 @@ public:
void set_previous_document_unload_timing(DocumentUnloadTimingInfo const& previous_document_unload_timing) { m_previous_document_unload_timing = previous_document_unload_timing; }
// https://w3c.github.io/editing/docs/execCommand/
bool exec_command(String const& command, bool show_ui, String const& value);
bool query_command_enabled(String const& command);
bool query_command_indeterm(String const& command);
bool query_command_state(String const& command);
bool query_command_supported(String const& command);
String query_command_value(String const& command);
bool exec_command(FlyString const& command, bool show_ui, String const& value);
bool query_command_enabled(FlyString const& command);
bool query_command_indeterm(FlyString const& command);
bool query_command_state(FlyString const& command);
bool query_command_supported(FlyString const& command);
String query_command_value(FlyString const& command);
// https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event
bool has_scheduled_selectionchange_event() const { return m_has_scheduled_selectionchange_event; }

View file

@ -143,6 +143,7 @@ interface Document : Node {
readonly attribute Element? scrollingElement;
// https://w3c.github.io/editing/docs/execCommand/
// FIXME: [CEReactions] boolean execCommand(DOMString commandId, optional boolean showUI = false, optional (TrustedHTML or DOMString) value = "");
[CEReactions] boolean execCommand(DOMString commandId, optional boolean showUI = false, optional DOMString value = "");
boolean queryCommandEnabled(DOMString commandId);
boolean queryCommandIndeterm(DOMString commandId);

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Editing/CommandNames.h>
namespace Web::Editing::CommandNames {
#define __ENUMERATE_COMMAND_NAME(name) FlyString name;
ENUMERATE_COMMAND_NAMES
#undef __ENUMERATE_COMMAND_NAME
FlyString delete_;
void initialize_strings()
{
static bool s_initialized = false;
VERIFY(!s_initialized);
#define __ENUMERATE_COMMAND_NAME(name) name = #name##_fly_string;
ENUMERATE_COMMAND_NAMES
#undef __ENUMERATE_MATHML_TAG
delete_ = "delete"_fly_string;
s_initialized = true;
}
}

View file

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
namespace Web::Editing::CommandNames {
#define ENUMERATE_COMMAND_NAMES \
__ENUMERATE_COMMAND_NAME(backColor) \
__ENUMERATE_COMMAND_NAME(bold) \
__ENUMERATE_COMMAND_NAME(copy) \
__ENUMERATE_COMMAND_NAME(createLink) \
__ENUMERATE_COMMAND_NAME(cut) \
__ENUMERATE_COMMAND_NAME(defaultParagraphSeparator) \
__ENUMERATE_COMMAND_NAME(fontName) \
__ENUMERATE_COMMAND_NAME(fontSize) \
__ENUMERATE_COMMAND_NAME(foreColor) \
__ENUMERATE_COMMAND_NAME(formatBlock) \
__ENUMERATE_COMMAND_NAME(forwardDelete) \
__ENUMERATE_COMMAND_NAME(hiliteColor) \
__ENUMERATE_COMMAND_NAME(indent) \
__ENUMERATE_COMMAND_NAME(insertHTML) \
__ENUMERATE_COMMAND_NAME(insertHorizontalRule) \
__ENUMERATE_COMMAND_NAME(insertImage) \
__ENUMERATE_COMMAND_NAME(insertLineBreak) \
__ENUMERATE_COMMAND_NAME(insertOrderedList) \
__ENUMERATE_COMMAND_NAME(insertParagraph) \
__ENUMERATE_COMMAND_NAME(insertText) \
__ENUMERATE_COMMAND_NAME(insertUnorderedList) \
__ENUMERATE_COMMAND_NAME(italic) \
__ENUMERATE_COMMAND_NAME(justifyCenter) \
__ENUMERATE_COMMAND_NAME(justifyFull) \
__ENUMERATE_COMMAND_NAME(justifyLeft) \
__ENUMERATE_COMMAND_NAME(justifyRight) \
__ENUMERATE_COMMAND_NAME(outdent) \
__ENUMERATE_COMMAND_NAME(paste) \
__ENUMERATE_COMMAND_NAME(redo) \
__ENUMERATE_COMMAND_NAME(removeFormat) \
__ENUMERATE_COMMAND_NAME(selectAll) \
__ENUMERATE_COMMAND_NAME(strikethrough) \
__ENUMERATE_COMMAND_NAME(styleWithCSS) \
__ENUMERATE_COMMAND_NAME(subscript) \
__ENUMERATE_COMMAND_NAME(superscript) \
__ENUMERATE_COMMAND_NAME(underline) \
__ENUMERATE_COMMAND_NAME(undo) \
__ENUMERATE_COMMAND_NAME(unlink) \
__ENUMERATE_COMMAND_NAME(useCSS)
#define __ENUMERATE_COMMAND_NAME(name) extern FlyString name;
ENUMERATE_COMMAND_NAMES
#undef __ENUMERATE_COMMAND_NAME
extern FlyString delete_;
void initialize_strings();
}

View file

@ -0,0 +1,340 @@
/*
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/Range.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/Editing/CommandNames.h>
#include <LibWeb/Editing/Commands.h>
#include <LibWeb/Editing/Internal/Algorithms.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLBRElement.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLTableElement.h>
#include <LibWeb/Namespace.h>
namespace Web::Editing {
// https://w3c.github.io/editing/docs/execCommand/#the-delete-command
bool command_delete_action(DOM::Document& document, String const&)
{
// 1. If the active range is not collapsed, delete the selection and return true.
auto& selection = *document.get_selection();
auto& active_range = *selection.range();
if (!active_range.collapsed()) {
delete_the_selection(selection);
return true;
}
// 2. Canonicalize whitespace at the active range's start.
canonicalize_whitespace(active_range.start_container(), active_range.start_offset());
// 3. Let node and offset be the active range's start node and offset.
auto node = active_range.start_container();
int offset = active_range.start_offset();
// 4. Repeat the following steps:
GC::Ptr<DOM::Node> offset_minus_one_child;
while (true) {
offset_minus_one_child = node->child_at_index(offset - 1);
// 1. If offset is zero and node's previousSibling is an editable invisible node, remove
// node's previousSibling from its parent.
if (auto* previous_sibling = node->previous_sibling()) {
if (offset == 0 && previous_sibling->is_editable() && is_invisible_node(*previous_sibling)) {
previous_sibling->remove();
continue;
}
}
// 2. Otherwise, if node has a child with index offset 1 and that child is an editable
// invisible node, remove that child from node, then subtract one from offset.
if (offset_minus_one_child && offset_minus_one_child->is_editable() && is_invisible_node(*offset_minus_one_child)) {
offset_minus_one_child->remove();
--offset;
continue;
}
// 3. Otherwise, if offset is zero and node is an inline node, or if node is an invisible
// node, set offset to the index of node, then set node to its parent.
if ((offset == 0 && is_inline_node(node)) || is_invisible_node(node)) {
offset = node->index();
node = *node->parent();
continue;
}
// 4. Otherwise, if node has a child with index offset 1 and that child is an editable a,
// remove that child from node, preserving its descendants. Then return true.
if (is<HTML::HTMLAnchorElement>(offset_minus_one_child.ptr()) && offset_minus_one_child->is_editable()) {
remove_node_preserving_its_descendants(*offset_minus_one_child);
return true;
}
// 5. Otherwise, if node has a child with index offset 1 and that child is not a block
// node or a br or an img, set node to that child, then set offset to the length of node.
if (offset_minus_one_child && !is_block_node(*offset_minus_one_child)
&& !is<HTML::HTMLBRElement>(*offset_minus_one_child) && !is<HTML::HTMLImageElement>(*offset_minus_one_child)) {
node = *offset_minus_one_child;
offset = node->length();
continue;
}
// 6. Otherwise, break from this loop.
break;
}
// 5. If node is a Text node and offset is not zero, or if node is a block node that has a child
// with index offset 1 and that child is a br or hr or img:
bool block_node_child_is_relevant_type = false;
if (is_block_node(node)) {
if (auto* child_node = node->child_at_index(offset - 1)) {
auto& child_element = static_cast<DOM::Element&>(*child_node);
block_node_child_is_relevant_type = child_element.local_name().is_one_of(HTML::TagNames::br, HTML::TagNames::hr, HTML::TagNames::img);
}
}
if ((is<DOM::Text>(*node) && offset != 0) || block_node_child_is_relevant_type) {
// 1. Call collapse(node, offset) on the context object's selection.
MUST(selection.collapse(node, offset));
// 2. Call extend(node, offset 1) on the context object's selection.
MUST(selection.extend(*node, offset - 1));
// 3. Delete the selection.
delete_the_selection(selection);
// 4. Return true.
return true;
}
// 6. If node is an inline node, return true.
if (is_inline_node(node))
return true;
// 7. If node is an li or dt or dd and is the first child of its parent, and offset is zero:
auto& node_element = static_cast<DOM::Element&>(*node);
if (offset == 0 && node->index() == 0
&& node_element.local_name().is_one_of(HTML::TagNames::li, HTML::TagNames::dt, HTML::TagNames::dd)) {
// 1. Let items be a list of all lis that are ancestors of node.
auto items = Vector<GC::Ref<DOM::Element>>();
GC::Ptr<DOM::Node> ancestor = node->parent();
do {
auto& ancestor_element = static_cast<DOM::Element&>(*ancestor);
if (ancestor_element.local_name() == HTML::TagNames::li)
items.append(ancestor_element);
ancestor = ancestor->parent();
} while (ancestor);
// 2. Normalize sublists of each item in items.
for (auto item : items)
normalize_sublists_in_node(*item);
// 3. Record the values of the one-node list consisting of node, and let values be the
// result.
auto values = record_the_values_of_nodes({ node });
// 4. Split the parent of the one-node list consisting of node.
split_the_parent_of_nodes({ node });
// 5. Restore the values from values.
restore_the_values_of_nodes(values);
// FIXME: 6. If node is a dd or dt, and it is not an allowed child of any of its ancestors in the
// same editing host, set the tag name of node to the default single-line container name
// and let node be the result.
// FIXME: 7. Fix disallowed ancestors of node.
// 8. Return true.
return true;
}
// 8. Let start node equal node and let start offset equal offset.
auto start_node = node;
auto start_offset = offset;
// 9. Repeat the following steps:
while (true) {
// 1. If start offset is zero, set start offset to the index of start node and then set
// start node to its parent.
if (start_offset == 0) {
start_offset = start_node->index();
start_node = *start_node->parent();
continue;
}
// 2. Otherwise, if start node has an editable invisible child with index start offset minus
// one, remove it from start node and subtract one from start offset.
offset_minus_one_child = start_node->child_at_index(start_offset - 1);
if (offset_minus_one_child && offset_minus_one_child->is_editable() && is_invisible_node(*offset_minus_one_child)) {
offset_minus_one_child->remove();
--start_offset;
continue;
}
// 3. Otherwise, break from this loop.
break;
}
// FIXME: 10. If offset is zero, and node has an editable inclusive ancestor in the same editing host
// that's an indentation element:
if (false) {
// FIXME: 1. Block-extend the range whose start and end are both (node, 0), and let new range be
// the result.
// FIXME: 2. Let node list be a list of nodes, initially empty.
// FIXME: 3. For each node current node contained in new range, append current node to node list if
// the last member of node list (if any) is not an ancestor of current node, and current
// node is editable but has no editable descendants.
// FIXME: 4. Outdent each node in node list.
// 5. Return true.
return true;
}
// 11. If the child of start node with index start offset is a table, return true.
if (is<HTML::HTMLTableElement>(start_node->child_at_index(start_offset)))
return true;
// 12. If start node has a child with index start offset 1, and that child is a table:
offset_minus_one_child = start_node->child_at_index(start_offset - 1);
if (is<HTML::HTMLTableElement>(offset_minus_one_child.ptr())) {
// 1. Call collapse(start node, start offset 1) on the context object's selection.
MUST(selection.collapse(start_node, start_offset - 1));
// 2. Call extend(start node, start offset) on the context object's selection.
MUST(selection.extend(*start_node, start_offset));
// 3. Return true.
return true;
}
// 13. If offset is zero; and either the child of start node with index start offset minus one
// is an hr, or the child is a br whose previousSibling is either a br or not an inline
// node:
if (offset == 0 && is<DOM::Element>(offset_minus_one_child.ptr())) {
auto& child_element = static_cast<DOM::Element&>(*offset_minus_one_child);
auto* previous_sibling = child_element.previous_sibling();
if (child_element.local_name() == HTML::TagNames::hr
|| (is<HTML::HTMLBRElement>(child_element) && previous_sibling && (is<HTML::HTMLBRElement>(*previous_sibling) || !is_inline_node(*previous_sibling)))) {
// 1. Call collapse(start node, start offset 1) on the context object's selection.
MUST(selection.collapse(start_node, start_offset - 1));
// 2. Call extend(start node, start offset) on the context object's selection.
MUST(selection.extend(*start_node, start_offset));
// 3. Delete the selection.
delete_the_selection(selection);
// 4. Call collapse(node, offset) on the selection.
MUST(selection.collapse(node, offset));
// 5. Return true.
return true;
}
}
// 14. If the child of start node with index start offset is an li or dt or dd, and that child's
// firstChild is an inline node, and start offset is not zero:
auto is_li_dt_or_dd = [](DOM::Element const& node) {
return node.local_name().is_one_of(HTML::TagNames::li, HTML::TagNames::dt, HTML::TagNames::dd);
};
auto* start_offset_child = start_node->child_at_index(start_offset);
if (start_offset != 0 && is<DOM::Element>(start_offset_child)
&& is_li_dt_or_dd(static_cast<DOM::Element&>(*start_offset_child))
&& start_offset_child->has_children() && is_inline_node(*start_offset_child->first_child())) {
// 1. Let previous item be the child of start node with index start offset minus one.
GC::Ref<DOM::Node> previous_item = *start_node->child_at_index(start_offset - 1);
// 2. If previous item's lastChild is an inline node other than a br, call
// createElement("br") on the context object and append the result as the last child of
// previous item.
GC::Ptr<DOM::Node> previous_item_last_child = previous_item->last_child();
if (previous_item_last_child && is_inline_node(*previous_item_last_child) && !is<HTML::HTMLBRElement>(*previous_item_last_child)) {
auto br_element = MUST(DOM::create_element(previous_item->document(), HTML::TagNames::br, Namespace::HTML));
MUST(previous_item->append_child(br_element));
}
// 3. If previous item's lastChild is an inline node, call createElement("br") on the
// context object and append the result as the last child of previous item.
if (previous_item_last_child && is_inline_node(*previous_item_last_child)) {
auto br_element = MUST(DOM::create_element(previous_item->document(), HTML::TagNames::br, Namespace::HTML));
MUST(previous_item->append_child(br_element));
}
}
// FIXME: 15. If start node's child with index start offset is an li or dt or dd, and that child's
// previousSibling is also an li or dt or dd:
if (false) {
// FIXME: 1. Call cloneRange() on the active range, and let original range be the result.
// FIXME: 2. Set start node to its child with index start offset 1.
// FIXME: 3. Set start offset to start node's length.
// FIXME: 4. Set node to start node's nextSibling.
// FIXME: 5. Call collapse(start node, start offset) on the context object's selection.
// FIXME: 6. Call extend(node, 0) on the context object's selection.
// FIXME: 7. Delete the selection.
// FIXME: 8. Call removeAllRanges() on the context object's selection.
// FIXME: 9. Call addRange(original range) on the context object's selection.
// 10. Return true.
return true;
}
// 16. While start node has a child with index start offset minus one:
while (start_node->child_at_index(start_offset - 1) != nullptr) {
// 1. If start node's child with index start offset minus one is editable and invisible,
// remove it from start node, then subtract one from start offset.
offset_minus_one_child = start_node->child_at_index(start_offset - 1);
if (offset_minus_one_child->is_editable() && is_invisible_node(*offset_minus_one_child)) {
offset_minus_one_child->remove();
--start_offset;
}
// 2. Otherwise, set start node to its child with index start offset minus one, then set
// start offset to the length of start node.
else {
start_node = *offset_minus_one_child;
start_offset = start_node->length();
}
}
// 17. Call collapse(start node, start offset) on the context object's selection.
MUST(selection.collapse(start_node, start_offset));
// 18. Call extend(node, offset) on the context object's selection.
MUST(selection.extend(*node, offset));
// FIXME: 19. Delete the selection, with direction "backward".
delete_the_selection(selection);
// 20. Return true.
return true;
}
static Array const commands {
CommandDefinition { CommandNames::delete_, command_delete_action, {}, {}, {} },
};
Optional<CommandDefinition const&> find_command_definition(FlyString const& command)
{
for (auto& definition : commands) {
if (command.equals_ignoring_ascii_case(definition.command))
return definition;
}
return {};
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Forward.h>
namespace Web::Editing {
struct CommandDefinition {
FlyString const& command;
Function<bool(DOM::Document&, String const&)> action;
Function<bool(DOM::Document const&)> indeterminate;
Function<bool(DOM::Document const&)> state;
Function<String(DOM::Document const&)> value;
};
Optional<CommandDefinition const&> find_command_definition(FlyString const&);
// Command implementations
bool command_delete_action(DOM::Document&, String const&);
}

View file

@ -0,0 +1,201 @@
/*
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Range.h>
#include <LibWeb/Editing/CommandNames.h>
#include <LibWeb/Editing/Commands.h>
#include <LibWeb/Editing/Internal/Algorithms.h>
#include <LibWeb/Selection/Selection.h>
namespace Web::DOM {
// https://w3c.github.io/editing/docs/execCommand/#execcommand()
bool Document::exec_command(FlyString const& command, [[maybe_unused]] bool show_ui, String const& value)
{
// 1. If only one argument was provided, let show UI be false.
// 2. If only one or two arguments were provided, let value be the empty string.
// NOTE: these steps are dealt by the default values for both show_ui and value
// 3. If command is not supported or not enabled, return false.
// NOTE: query_command_enabled() also checks if command is supported
if (!query_command_enabled(command))
return false;
// 4. If command is not in the Miscellaneous commands section:
//
// We don't fire events for copy/cut/paste/undo/redo/selectAll because they should all have
// their own events. We don't fire events for styleWithCSS/useCSS because it's not obvious
// where to fire them, or why anyone would want them. We don't fire events for unsupported
// commands, because then if they became supported and were classified with the miscellaneous
// events, we'd have to stop firing events for consistency's sake.
//
// AD-HOC: The defaultParagraphSeparator command is also in the Miscellaneous commands section
if (command != Editing::CommandNames::defaultParagraphSeparator
&& command != Editing::CommandNames::redo
&& command != Editing::CommandNames::selectAll
&& command != Editing::CommandNames::styleWithCSS
&& command != Editing::CommandNames::undo
&& command != Editing::CommandNames::useCSS) {
// FIXME: 1. Let affected editing host be the editing host that is an inclusive ancestor of the
// active range's start node and end node, and is not the ancestor of any editing host
// that is an inclusive ancestor of the active range's start node and end node.
// FIXME: 2. Fire an event named "beforeinput" at affected editing host using InputEvent, with its
// bubbles and cancelable attributes initialized to true, and its data attribute
// initialized to null.
// FIXME: 3. If the value returned by the previous step is false, return false.
// 4. If command is not enabled, return false.
//
// We have to check again whether the command is enabled, because the beforeinput handler
// might have done something annoying like getSelection().removeAllRanges().
if (!query_command_enabled(command))
return false;
// FIXME: 5. Let affected editing host be the editing host that is an inclusive ancestor of the
// active range's start node and end node, and is not the ancestor of any editing host
// that is an inclusive ancestor of the active range's start node and end node.
//
// This new affected editing host is what we'll fire the input event at in a couple of
// lines. We want to compute it beforehand just to be safe: bugs in the command action
// might remove the selection or something bad like that, and we don't want to have to
// handle it later. We recompute it after the beforeinput event is handled so that if the
// handler moves the selection to some other editing host, the input event will be fired
// at the editing host that was actually affected.
}
// 5. Take the action for command, passing value to the instructions as an argument.
auto optional_command = Editing::find_command_definition(command);
VERIFY(optional_command.has_value());
auto const& command_definition = optional_command.release_value();
auto command_result = command_definition.action(*this, value);
// 6. If the previous step returned false, return false.
if (!command_result)
return false;
// FIXME: 7. If the action modified DOM tree, then fire an event named "input" at affected editing host
// using InputEvent, with its isTrusted and bubbles attributes initialized to true, inputType
// attribute initialized to the mapped value of command, and its data attribute initialized
// to null.
// 8. Return true.
return true;
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandenabled()
bool Document::query_command_enabled(FlyString const& command)
{
// 2. Return true if command is both supported and enabled, false otherwise.
if (!query_command_supported(command))
return false;
// https://w3c.github.io/editing/docs/execCommand/#enabled
// Among commands defined in this specification, those listed in Miscellaneous commands are
// always enabled, except for the cut command and the paste command.
// AD-HOC: Cut and Paste are not in the Miscellaneous commands section; so Copy is assumed
// AD-HOC: DefaultParagraphSeparator is also in the Miscellaneous commands section
if (command == Editing::CommandNames::copy
|| command == Editing::CommandNames::defaultParagraphSeparator
|| command == Editing::CommandNames::redo
|| command == Editing::CommandNames::selectAll
|| command == Editing::CommandNames::styleWithCSS
|| command == Editing::CommandNames::undo
|| command == Editing::CommandNames::useCSS)
return true;
// The other commands defined here are enabled if the active range is not null,
auto selection = get_selection();
if (!selection)
return false;
auto active_range = selection->range();
if (!active_range)
return false;
// its start node is either editable or an editing host,
auto& start_node = *active_range->start_container();
if (!start_node.is_editable() && !Editing::is_editing_host(start_node))
return false;
// FIXME: the editing host of its start node is not an EditContext editing host,
// its end node is either editable or an editing host,
auto& end_node = *active_range->end_container();
if (!end_node.is_editable() && !Editing::is_editing_host(end_node))
return false;
// FIXME: the editing host of its end node is not an EditContext editing host,
// FIXME: and there is some editing host that is an inclusive ancestor of both its start node and its
// end node.
return true;
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandindeterm()
bool Document::query_command_indeterm(FlyString const& command)
{
// 1. If command is not supported or has no indeterminacy, return false.
auto optional_command = Editing::find_command_definition(command);
if (!optional_command.has_value())
return false;
auto const& command_definition = optional_command.value();
if (!command_definition.indeterminate)
return false;
// 2. Return true if command is indeterminate, otherwise false.
return command_definition.indeterminate(*this);
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandstate()
bool Document::query_command_state(FlyString const& command)
{
// 1. If command is not supported or has no state, return false.
auto optional_command = Editing::find_command_definition(command);
if (!optional_command.has_value())
return false;
auto const& command_definition = optional_command.release_value();
if (!command_definition.state)
return false;
// FIXME: 2. If the state override for command is set, return it.
// 3. Return true if command's state is true, otherwise false.
return command_definition.state(*this);
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandsupported()
bool Document::query_command_supported(FlyString const& command)
{
// When the queryCommandSupported(command) method on the Document interface is invoked, the
// user agent must return true if command is supported and available within the current script
// on the current site, and false otherwise.
return Editing::find_command_definition(command).has_value();
}
// https://w3c.github.io/editing/docs/execCommand/#querycommandvalue()
String Document::query_command_value(FlyString const& command)
{
// 1. If command is not supported or has no value, return the empty string.
auto optional_command = Editing::find_command_definition(command);
if (!optional_command.has_value())
return {};
auto const& command_definition = optional_command.release_value();
if (!command_definition.value)
return {};
// FIXME: 2. If command is "fontSize" and its value override is set, convert the value override to an
// integer number of pixels and return the legacy font size for the result.
// FIXME: 3. If the value override for command is set, return it.
// 4. Return command's value.
return command_definition.value(*this);
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <LibWeb/DOM/Node.h>
namespace Web::Editing {
// https://w3c.github.io/editing/docs/execCommand/#record-the-values
struct RecordedNodeValue {
GC::Ref<DOM::Node> node;
FlyString const& command;
Optional<String> specified_command_value;
};
// Below algorithms are specified here:
// https://w3c.github.io/editing/docs/execCommand/#assorted-common-algorithms
String canonical_space_sequence(u32 length, bool non_breaking_start, bool non_breaking_end);
void canonicalize_whitespace(GC::Ref<DOM::Node>, u32 offset, bool fix_collapsed_space = true);
void delete_the_selection(Selection::Selection const&);
GC::Ptr<DOM::Node> editing_host_of_node(GC::Ref<DOM::Node>);
bool follows_a_line_break(GC::Ref<DOM::Node>);
bool is_allowed_child_of_node(Variant<GC::Ref<DOM::Node>, FlyString> child, Variant<GC::Ref<DOM::Node>, FlyString> parent);
bool is_block_boundary_point(GC::Ref<DOM::Node>, u32 offset);
bool is_block_end_point(GC::Ref<DOM::Node>, u32 offset);
bool is_block_node(GC::Ref<DOM::Node>);
bool is_block_start_point(GC::Ref<DOM::Node>, u32 offset);
bool is_collapsed_whitespace_node(GC::Ref<DOM::Node>);
bool is_editing_host(GC::Ref<DOM::Node>);
bool is_element_with_inline_contents(GC::Ref<DOM::Node>);
bool is_extraneous_line_break(GC::Ref<DOM::Node>);
bool is_in_same_editing_host(GC::Ref<DOM::Node>, GC::Ref<DOM::Node>);
bool is_inline_node(GC::Ref<DOM::Node>);
bool is_invisible_node(GC::Ref<DOM::Node>);
bool is_name_of_an_element_with_inline_contents(FlyString const&);
bool is_prohibited_paragraph_child_name(FlyString const&);
bool is_visible_node(GC::Ref<DOM::Node>);
bool is_whitespace_node(GC::Ref<DOM::Node>);
void move_node_preserving_ranges(GC::Ref<DOM::Node>, GC::Ref<DOM::Node> new_parent, u32 new_index);
void normalize_sublists_in_node(GC::Ref<DOM::Element>);
bool precedes_a_line_break(GC::Ref<DOM::Node>);
Vector<RecordedNodeValue> record_the_values_of_nodes(Vector<GC::Ref<DOM::Node>> const&);
void remove_extraneous_line_breaks_at_the_end_of_node(GC::Ref<DOM::Node>);
void remove_extraneous_line_breaks_before_node(GC::Ref<DOM::Node>);
void remove_extraneous_line_breaks_from_a_node(GC::Ref<DOM::Node>);
void remove_node_preserving_its_descendants(GC::Ref<DOM::Node>);
void restore_the_values_of_nodes(Vector<RecordedNodeValue> const&);
GC::Ref<DOM::Element> set_the_tag_name(GC::Ref<DOM::Element>, FlyString const&);
Optional<String> specified_command_value(GC::Ref<DOM::Element>, FlyString const& command);
void split_the_parent_of_nodes(Vector<GC::Ref<DOM::Node>> const&);
}