Commands.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/DOM/ElementFactory.h>
  8. #include <LibWeb/DOM/Range.h>
  9. #include <LibWeb/DOM/Text.h>
  10. #include <LibWeb/Editing/CommandNames.h>
  11. #include <LibWeb/Editing/Commands.h>
  12. #include <LibWeb/Editing/Internal/Algorithms.h>
  13. #include <LibWeb/HTML/HTMLAnchorElement.h>
  14. #include <LibWeb/HTML/HTMLBRElement.h>
  15. #include <LibWeb/HTML/HTMLImageElement.h>
  16. #include <LibWeb/HTML/HTMLTableElement.h>
  17. #include <LibWeb/Namespace.h>
  18. namespace Web::Editing {
  19. // https://w3c.github.io/editing/docs/execCommand/#the-stylewithcss-command
  20. bool command_style_with_css_action(DOM::Document& document, String const& value)
  21. {
  22. // If value is an ASCII case-insensitive match for the string "false", set the CSS styling flag to false.
  23. // Otherwise, set the CSS styling flag to true.
  24. document.set_css_styling_flag(!value.equals_ignoring_ascii_case("false"sv));
  25. // Either way, return true.
  26. return true;
  27. }
  28. // https://w3c.github.io/editing/docs/execCommand/#the-stylewithcss-command
  29. bool command_style_with_css_state(DOM::Document const& document)
  30. {
  31. // True if the CSS styling flag is true, otherwise false.
  32. return document.css_styling_flag();
  33. }
  34. // https://w3c.github.io/editing/docs/execCommand/#the-defaultparagraphseparator-command
  35. bool command_default_paragraph_separator_action(DOM::Document& document, String const& input_value)
  36. {
  37. // Let value be converted to ASCII lowercase.
  38. auto value = input_value.to_ascii_lowercase();
  39. // If value is then equal to "p" or "div", set the context object's default single-line
  40. // container name to value, then return true.
  41. if (value == HTML::TagNames::p) {
  42. document.set_default_single_line_container_name(HTML::TagNames::p);
  43. return true;
  44. }
  45. if (value == HTML::TagNames::div) {
  46. document.set_default_single_line_container_name(HTML::TagNames::div);
  47. return true;
  48. }
  49. // Otherwise, return false.
  50. return false;
  51. }
  52. // https://w3c.github.io/editing/docs/execCommand/#the-defaultparagraphseparator-command
  53. String command_default_paragraph_separator_value(DOM::Document const& document)
  54. {
  55. // Return the context object's default single-line container name.
  56. return document.default_single_line_container_name().to_string();
  57. }
  58. // https://w3c.github.io/editing/docs/execCommand/#the-delete-command
  59. bool command_delete_action(DOM::Document& document, String const&)
  60. {
  61. // 1. If the active range is not collapsed, delete the selection and return true.
  62. auto& selection = *document.get_selection();
  63. auto& active_range = *selection.range();
  64. if (!active_range.collapsed()) {
  65. delete_the_selection(selection);
  66. return true;
  67. }
  68. // 2. Canonicalize whitespace at the active range's start.
  69. canonicalize_whitespace(active_range.start_container(), active_range.start_offset());
  70. // 3. Let node and offset be the active range's start node and offset.
  71. GC::Ptr<DOM::Node> node = active_range.start_container();
  72. int offset = active_range.start_offset();
  73. // 4. Repeat the following steps:
  74. GC::Ptr<DOM::Node> offset_minus_one_child;
  75. while (true) {
  76. offset_minus_one_child = node->child_at_index(offset - 1);
  77. // 1. If offset is zero and node's previousSibling is an editable invisible node, remove
  78. // node's previousSibling from its parent.
  79. if (auto* previous_sibling = node->previous_sibling()) {
  80. if (offset == 0 && previous_sibling->is_editable() && is_invisible_node(*previous_sibling)) {
  81. previous_sibling->remove();
  82. continue;
  83. }
  84. }
  85. // 2. Otherwise, if node has a child with index offset − 1 and that child is an editable
  86. // invisible node, remove that child from node, then subtract one from offset.
  87. if (offset_minus_one_child && offset_minus_one_child->is_editable() && is_invisible_node(*offset_minus_one_child)) {
  88. offset_minus_one_child->remove();
  89. --offset;
  90. continue;
  91. }
  92. // 3. Otherwise, if offset is zero and node is an inline node, or if node is an invisible
  93. // node, set offset to the index of node, then set node to its parent.
  94. if ((offset == 0 && is_inline_node(*node)) || is_invisible_node(*node)) {
  95. offset = node->index();
  96. node = *node->parent();
  97. continue;
  98. }
  99. // 4. Otherwise, if node has a child with index offset − 1 and that child is an editable a,
  100. // remove that child from node, preserving its descendants. Then return true.
  101. if (is<HTML::HTMLAnchorElement>(offset_minus_one_child.ptr()) && offset_minus_one_child->is_editable()) {
  102. remove_node_preserving_its_descendants(*offset_minus_one_child);
  103. return true;
  104. }
  105. // 5. Otherwise, if node has a child with index offset − 1 and that child is not a block
  106. // node or a br or an img, set node to that child, then set offset to the length of node.
  107. if (offset_minus_one_child && !is_block_node(*offset_minus_one_child)
  108. && !is<HTML::HTMLBRElement>(*offset_minus_one_child) && !is<HTML::HTMLImageElement>(*offset_minus_one_child)) {
  109. node = *offset_minus_one_child;
  110. offset = node->length();
  111. continue;
  112. }
  113. // 6. Otherwise, break from this loop.
  114. break;
  115. }
  116. // 5. If node is a Text node and offset is not zero, or if node is a block node that has a child
  117. // with index offset − 1 and that child is a br or hr or img:
  118. bool block_node_child_is_relevant_type = false;
  119. if (is_block_node(*node)) {
  120. if (auto* child_node = node->child_at_index(offset - 1)) {
  121. auto& child_element = static_cast<DOM::Element&>(*child_node);
  122. block_node_child_is_relevant_type = child_element.local_name().is_one_of(HTML::TagNames::br, HTML::TagNames::hr, HTML::TagNames::img);
  123. }
  124. }
  125. if ((is<DOM::Text>(*node) && offset != 0) || block_node_child_is_relevant_type) {
  126. // 1. Call collapse(node, offset) on the context object's selection.
  127. MUST(selection.collapse(node, offset));
  128. // 2. Call extend(node, offset − 1) on the context object's selection.
  129. MUST(selection.extend(*node, offset - 1));
  130. // 3. Delete the selection.
  131. delete_the_selection(selection);
  132. // 4. Return true.
  133. return true;
  134. }
  135. // 6. If node is an inline node, return true.
  136. if (is_inline_node(*node))
  137. return true;
  138. // 7. If node is an li or dt or dd and is the first child of its parent, and offset is zero:
  139. auto& node_element = static_cast<DOM::Element&>(*node);
  140. if (offset == 0 && node->index() == 0
  141. && node_element.local_name().is_one_of(HTML::TagNames::li, HTML::TagNames::dt, HTML::TagNames::dd)) {
  142. // 1. Let items be a list of all lis that are ancestors of node.
  143. auto items = Vector<GC::Ref<DOM::Element>>();
  144. GC::Ptr<DOM::Node> ancestor = node->parent();
  145. do {
  146. auto& ancestor_element = static_cast<DOM::Element&>(*ancestor);
  147. if (ancestor_element.local_name() == HTML::TagNames::li)
  148. items.append(ancestor_element);
  149. ancestor = ancestor->parent();
  150. } while (ancestor);
  151. // 2. Normalize sublists of each item in items.
  152. for (auto item : items)
  153. normalize_sublists_in_node(*item);
  154. // 3. Record the values of the one-node list consisting of node, and let values be the
  155. // result.
  156. auto values = record_the_values_of_nodes({ *node });
  157. // 4. Split the parent of the one-node list consisting of node.
  158. split_the_parent_of_nodes({ *node });
  159. // 5. Restore the values from values.
  160. restore_the_values_of_nodes(values);
  161. // 6. If node is a dd or dt, and it is not an allowed child of any of its ancestors in the
  162. // same editing host, set the tag name of node to the default single-line container name
  163. // and let node be the result.
  164. if (node_element.local_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt)) {
  165. ancestor = node->parent();
  166. bool allowed_child_of_any_ancestor = false;
  167. do {
  168. if (is_in_same_editing_host(*node, *ancestor) && is_allowed_child_of_node(GC::Ref { *node }, GC::Ref { *ancestor })) {
  169. allowed_child_of_any_ancestor = true;
  170. break;
  171. }
  172. ancestor = ancestor->parent();
  173. } while (ancestor);
  174. if (!allowed_child_of_any_ancestor)
  175. node = set_the_tag_name(node_element, document.default_single_line_container_name());
  176. }
  177. // 7. Fix disallowed ancestors of node.
  178. fix_disallowed_ancestors_of_node(node);
  179. // 8. Return true.
  180. return true;
  181. }
  182. // 8. Let start node equal node and let start offset equal offset.
  183. auto start_node = node;
  184. auto start_offset = offset;
  185. // 9. Repeat the following steps:
  186. while (true) {
  187. // 1. If start offset is zero, set start offset to the index of start node and then set
  188. // start node to its parent.
  189. if (start_offset == 0) {
  190. start_offset = start_node->index();
  191. start_node = *start_node->parent();
  192. continue;
  193. }
  194. // 2. Otherwise, if start node has an editable invisible child with index start offset minus
  195. // one, remove it from start node and subtract one from start offset.
  196. offset_minus_one_child = start_node->child_at_index(start_offset - 1);
  197. if (offset_minus_one_child && offset_minus_one_child->is_editable() && is_invisible_node(*offset_minus_one_child)) {
  198. offset_minus_one_child->remove();
  199. --start_offset;
  200. continue;
  201. }
  202. // 3. Otherwise, break from this loop.
  203. break;
  204. }
  205. // FIXME: 10. If offset is zero, and node has an editable inclusive ancestor in the same editing host
  206. // that's an indentation element:
  207. if (false) {
  208. // FIXME: 1. Block-extend the range whose start and end are both (node, 0), and let new range be
  209. // the result.
  210. // FIXME: 2. Let node list be a list of nodes, initially empty.
  211. // FIXME: 3. For each node current node contained in new range, append current node to node list if
  212. // the last member of node list (if any) is not an ancestor of current node, and current
  213. // node is editable but has no editable descendants.
  214. // FIXME: 4. Outdent each node in node list.
  215. // 5. Return true.
  216. return true;
  217. }
  218. // 11. If the child of start node with index start offset is a table, return true.
  219. if (is<HTML::HTMLTableElement>(start_node->child_at_index(start_offset)))
  220. return true;
  221. // 12. If start node has a child with index start offset − 1, and that child is a table:
  222. offset_minus_one_child = start_node->child_at_index(start_offset - 1);
  223. if (is<HTML::HTMLTableElement>(offset_minus_one_child.ptr())) {
  224. // 1. Call collapse(start node, start offset − 1) on the context object's selection.
  225. MUST(selection.collapse(start_node, start_offset - 1));
  226. // 2. Call extend(start node, start offset) on the context object's selection.
  227. MUST(selection.extend(*start_node, start_offset));
  228. // 3. Return true.
  229. return true;
  230. }
  231. // 13. If offset is zero; and either the child of start node with index start offset minus one
  232. // is an hr, or the child is a br whose previousSibling is either a br or not an inline
  233. // node:
  234. if (offset == 0 && is<DOM::Element>(offset_minus_one_child.ptr())) {
  235. auto& child_element = static_cast<DOM::Element&>(*offset_minus_one_child);
  236. auto* previous_sibling = child_element.previous_sibling();
  237. if (child_element.local_name() == HTML::TagNames::hr
  238. || (is<HTML::HTMLBRElement>(child_element) && previous_sibling && (is<HTML::HTMLBRElement>(*previous_sibling) || !is_inline_node(*previous_sibling)))) {
  239. // 1. Call collapse(start node, start offset − 1) on the context object's selection.
  240. MUST(selection.collapse(start_node, start_offset - 1));
  241. // 2. Call extend(start node, start offset) on the context object's selection.
  242. MUST(selection.extend(*start_node, start_offset));
  243. // 3. Delete the selection.
  244. delete_the_selection(selection);
  245. // 4. Call collapse(node, offset) on the selection.
  246. MUST(selection.collapse(node, offset));
  247. // 5. Return true.
  248. return true;
  249. }
  250. }
  251. // 14. If the child of start node with index start offset is an li or dt or dd, and that child's
  252. // firstChild is an inline node, and start offset is not zero:
  253. auto is_li_dt_or_dd = [](DOM::Element const& node) {
  254. return node.local_name().is_one_of(HTML::TagNames::li, HTML::TagNames::dt, HTML::TagNames::dd);
  255. };
  256. auto* start_offset_child = start_node->child_at_index(start_offset);
  257. if (start_offset != 0 && is<DOM::Element>(start_offset_child)
  258. && is_li_dt_or_dd(static_cast<DOM::Element&>(*start_offset_child))
  259. && start_offset_child->has_children() && is_inline_node(*start_offset_child->first_child())) {
  260. // 1. Let previous item be the child of start node with index start offset minus one.
  261. GC::Ref<DOM::Node> previous_item = *start_node->child_at_index(start_offset - 1);
  262. // 2. If previous item's lastChild is an inline node other than a br, call
  263. // createElement("br") on the context object and append the result as the last child of
  264. // previous item.
  265. GC::Ptr<DOM::Node> previous_item_last_child = previous_item->last_child();
  266. if (previous_item_last_child && is_inline_node(*previous_item_last_child) && !is<HTML::HTMLBRElement>(*previous_item_last_child)) {
  267. auto br_element = MUST(DOM::create_element(previous_item->document(), HTML::TagNames::br, Namespace::HTML));
  268. MUST(previous_item->append_child(br_element));
  269. }
  270. // 3. If previous item's lastChild is an inline node, call createElement("br") on the
  271. // context object and append the result as the last child of previous item.
  272. if (previous_item_last_child && is_inline_node(*previous_item_last_child)) {
  273. auto br_element = MUST(DOM::create_element(previous_item->document(), HTML::TagNames::br, Namespace::HTML));
  274. MUST(previous_item->append_child(br_element));
  275. }
  276. }
  277. // FIXME: 15. If start node's child with index start offset is an li or dt or dd, and that child's
  278. // previousSibling is also an li or dt or dd:
  279. if (false) {
  280. // FIXME: 1. Call cloneRange() on the active range, and let original range be the result.
  281. // FIXME: 2. Set start node to its child with index start offset − 1.
  282. // FIXME: 3. Set start offset to start node's length.
  283. // FIXME: 4. Set node to start node's nextSibling.
  284. // FIXME: 5. Call collapse(start node, start offset) on the context object's selection.
  285. // FIXME: 6. Call extend(node, 0) on the context object's selection.
  286. // FIXME: 7. Delete the selection.
  287. // FIXME: 8. Call removeAllRanges() on the context object's selection.
  288. // FIXME: 9. Call addRange(original range) on the context object's selection.
  289. // 10. Return true.
  290. return true;
  291. }
  292. // 16. While start node has a child with index start offset minus one:
  293. while (start_node->child_at_index(start_offset - 1) != nullptr) {
  294. // 1. If start node's child with index start offset minus one is editable and invisible,
  295. // remove it from start node, then subtract one from start offset.
  296. offset_minus_one_child = start_node->child_at_index(start_offset - 1);
  297. if (offset_minus_one_child->is_editable() && is_invisible_node(*offset_minus_one_child)) {
  298. offset_minus_one_child->remove();
  299. --start_offset;
  300. }
  301. // 2. Otherwise, set start node to its child with index start offset minus one, then set
  302. // start offset to the length of start node.
  303. else {
  304. start_node = *offset_minus_one_child;
  305. start_offset = start_node->length();
  306. }
  307. }
  308. // 17. Call collapse(start node, start offset) on the context object's selection.
  309. MUST(selection.collapse(start_node, start_offset));
  310. // 18. Call extend(node, offset) on the context object's selection.
  311. MUST(selection.extend(*node, offset));
  312. // FIXME: 19. Delete the selection, with direction "backward".
  313. delete_the_selection(selection);
  314. // 20. Return true.
  315. return true;
  316. }
  317. static Array const commands {
  318. CommandDefinition { CommandNames::delete_, command_delete_action, {}, {}, {} },
  319. CommandDefinition { CommandNames::defaultParagraphSeparator, command_default_paragraph_separator_action, {}, {}, command_default_paragraph_separator_value },
  320. CommandDefinition { CommandNames::styleWithCSS, command_style_with_css_action, {}, command_style_with_css_state, {} },
  321. };
  322. Optional<CommandDefinition const&> find_command_definition(FlyString const& command)
  323. {
  324. for (auto& definition : commands) {
  325. if (command.equals_ignoring_ascii_case(definition.command))
  326. return definition;
  327. }
  328. return {};
  329. }
  330. }