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