Selection.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/Range.h>
  9. #include <LibWeb/Selection/Selection.h>
  10. namespace Web::Selection {
  11. JS::NonnullGCPtr<Selection> Selection::create(JS::NonnullGCPtr<JS::Realm> realm, JS::NonnullGCPtr<DOM::Document> document)
  12. {
  13. return *realm->heap().allocate<Selection>(realm, realm, document);
  14. }
  15. Selection::Selection(JS::NonnullGCPtr<JS::Realm> realm, JS::NonnullGCPtr<DOM::Document> document)
  16. : PlatformObject(realm)
  17. , m_document(document)
  18. {
  19. set_prototype(&Bindings::cached_web_prototype(realm, "Selection"));
  20. }
  21. Selection::~Selection() = default;
  22. // https://w3c.github.io/selection-api/#dfn-empty
  23. bool Selection::is_empty() const
  24. {
  25. // Each selection can be associated with a single range.
  26. // When there is no range associated with the selection, the selection is empty.
  27. // The selection must be initially empty.
  28. // NOTE: This function should not be confused with Selection.empty() which empties the selection.
  29. return !m_range;
  30. }
  31. void Selection::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_range);
  35. visitor.visit(m_document);
  36. }
  37. // https://w3c.github.io/selection-api/#dfn-anchor
  38. JS::GCPtr<DOM::Node> Selection::anchor_node()
  39. {
  40. if (!m_range)
  41. return nullptr;
  42. if (m_direction == Direction::Forwards)
  43. return m_range->start_container();
  44. return m_range->end_container();
  45. }
  46. // https://w3c.github.io/selection-api/#dfn-anchor
  47. unsigned Selection::anchor_offset()
  48. {
  49. if (!m_range)
  50. return 0;
  51. if (m_direction == Direction::Forwards)
  52. return m_range->start_offset();
  53. return m_range->end_offset();
  54. }
  55. // https://w3c.github.io/selection-api/#dfn-focus
  56. JS::GCPtr<DOM::Node> Selection::focus_node()
  57. {
  58. if (!m_range)
  59. return nullptr;
  60. if (m_direction == Direction::Forwards)
  61. return m_range->end_container();
  62. return m_range->start_container();
  63. }
  64. // https://w3c.github.io/selection-api/#dfn-focus
  65. unsigned Selection::focus_offset() const
  66. {
  67. if (!m_range)
  68. return 0;
  69. if (m_direction == Direction::Forwards)
  70. return m_range->end_offset();
  71. return m_range->start_offset();
  72. }
  73. // https://w3c.github.io/selection-api/#dom-selection-iscollapsed
  74. bool Selection::is_collapsed() const
  75. {
  76. // The attribute must return true if and only if the anchor and focus are the same
  77. // (including if both are null). Otherwise it must return false.
  78. return const_cast<Selection*>(this)->anchor_node() == const_cast<Selection*>(this)->focus_node();
  79. }
  80. // https://w3c.github.io/selection-api/#dom-selection-rangecount
  81. unsigned Selection::range_count() const
  82. {
  83. if (m_range)
  84. return 1;
  85. return 0;
  86. }
  87. String Selection::type() const
  88. {
  89. if (!m_range)
  90. return "None";
  91. if (m_range->collapsed())
  92. return "Caret";
  93. return "Range";
  94. }
  95. // https://w3c.github.io/selection-api/#dom-selection-getrangeat
  96. WebIDL::ExceptionOr<JS::GCPtr<DOM::Range>> Selection::get_range_at(unsigned index)
  97. {
  98. // The method must throw an IndexSizeError exception if index is not 0, or if this is empty.
  99. if (index != 0 || is_empty())
  100. return WebIDL::IndexSizeError::create(realm(), "Selection.getRangeAt() on empty Selection or with invalid argument"sv);
  101. // Otherwise, it must return a reference to (not a copy of) this's range.
  102. return m_range;
  103. }
  104. // https://w3c.github.io/selection-api/#dom-selection-addrange
  105. void Selection::add_range(JS::NonnullGCPtr<DOM::Range> range)
  106. {
  107. // 1. If the root of the range's boundary points are not the document associated with this, abort these steps.
  108. if (&range->start_container()->root() != m_document.ptr())
  109. return;
  110. // 2. If rangeCount is not 0, abort these steps.
  111. if (range_count() != 0)
  112. return;
  113. // 3. Set this's range to range by a strong reference (not by making a copy).
  114. m_range = range;
  115. }
  116. // https://w3c.github.io/selection-api/#dom-selection-removerange
  117. WebIDL::ExceptionOr<void> Selection::remove_range(JS::NonnullGCPtr<DOM::Range> range)
  118. {
  119. // The method must make this empty by disassociating its range if this's range is range.
  120. if (m_range == range) {
  121. m_range = nullptr;
  122. return {};
  123. }
  124. // Otherwise, it must throw a NotFoundError.
  125. return WebIDL::NotFoundError::create(realm(), "Selection.removeRange() with invalid argument"sv);
  126. }
  127. // https://w3c.github.io/selection-api/#dom-selection-removeallranges
  128. void Selection::remove_all_ranges()
  129. {
  130. // The method must make this empty by disassociating its range if this has an associated range.
  131. m_range = nullptr;
  132. }
  133. // https://w3c.github.io/selection-api/#dom-selection-empty
  134. void Selection::empty()
  135. {
  136. // The method must be an alias, and behave identically, to removeAllRanges().
  137. remove_all_ranges();
  138. }
  139. // https://w3c.github.io/selection-api/#dom-selection-collapse
  140. void Selection::collapse(JS::GCPtr<DOM::Node>, unsigned offset)
  141. {
  142. (void)offset;
  143. TODO();
  144. }
  145. // https://w3c.github.io/selection-api/#dom-selection-setposition
  146. void Selection::set_position(JS::GCPtr<DOM::Node> node, unsigned offset)
  147. {
  148. // The method must be an alias, and behave identically, to collapse().
  149. collapse(node, offset);
  150. }
  151. // https://w3c.github.io/selection-api/#dom-selection-collapsetostart
  152. void Selection::collapse_to_start()
  153. {
  154. TODO();
  155. }
  156. // https://w3c.github.io/selection-api/#dom-selection-collapsetoend
  157. void Selection::collapse_to_end()
  158. {
  159. TODO();
  160. }
  161. // https://w3c.github.io/selection-api/#dom-selection-extend
  162. WebIDL::ExceptionOr<void> Selection::extend(JS::NonnullGCPtr<DOM::Node> node, unsigned offset)
  163. {
  164. // 1. If node's root is not the document associated with this, abort these steps.
  165. if (&node->root() != m_document.ptr())
  166. return {};
  167. // 2. If this is empty, throw an InvalidStateError exception and abort these steps.
  168. if (!m_range) {
  169. return WebIDL::InvalidStateError::create(realm(), "Selection.extend() on empty range"sv);
  170. }
  171. // 3. Let oldAnchor and oldFocus be the this's anchor and focus, and let newFocus be the boundary point (node, offset).
  172. auto& old_anchor_node = *anchor_node();
  173. auto old_anchor_offset = anchor_offset();
  174. auto& new_focus_node = node;
  175. auto new_focus_offset = offset;
  176. // 4. Let newRange be a new range.
  177. auto new_range = DOM::Range::create(*m_document);
  178. // 5. If node's root is not the same as the this's range's root, set the start newRange's start and end to newFocus.
  179. if (&node->root() != &m_range->start_container()->root()) {
  180. new_range->set_start(new_focus_node, new_focus_offset);
  181. }
  182. // 6. Otherwise, if oldAnchor is before or equal to newFocus, set the start newRange's start to oldAnchor, then set its end to newFocus.
  183. else if (old_anchor_node.is_before(new_focus_node) || &old_anchor_node == new_focus_node.ptr()) {
  184. new_range->set_end(new_focus_node, new_focus_offset);
  185. }
  186. // 7. Otherwise, set the start newRange's start to newFocus, then set its end to oldAnchor.
  187. else {
  188. new_range->set_start(new_focus_node, new_focus_offset);
  189. new_range->set_end(old_anchor_node, old_anchor_offset);
  190. }
  191. // 8. Set this's range to newRange.
  192. m_range = new_range;
  193. // 9. If newFocus is before oldAnchor, set this's direction to backwards. Otherwise, set it to forwards.
  194. if (new_focus_node->is_before(old_anchor_node)) {
  195. m_direction = Direction::Backwards;
  196. } else {
  197. m_direction = Direction::Forwards;
  198. }
  199. return {};
  200. }
  201. // https://w3c.github.io/selection-api/#dom-selection-setbaseandextent
  202. void Selection::set_base_and_extent(JS::NonnullGCPtr<DOM::Node> anchor_node, unsigned anchor_offset, JS::NonnullGCPtr<DOM::Node> focus_node, unsigned focus_offset)
  203. {
  204. (void)anchor_node;
  205. (void)anchor_offset;
  206. (void)focus_node;
  207. (void)focus_offset;
  208. TODO();
  209. }
  210. // https://w3c.github.io/selection-api/#dom-selection-selectallchildren
  211. WebIDL::ExceptionOr<void> Selection::select_all_children(JS::NonnullGCPtr<DOM::Node> node)
  212. {
  213. // 1. If node's root is not the document associated with this, abort these steps.
  214. if (&node->root() != m_document.ptr())
  215. return {};
  216. // 2. Let newRange be a new range and childCount be the number of children of node.
  217. auto new_range = DOM::Range::create(*m_document);
  218. auto child_count = node->child_count();
  219. // 3. Set newRange's start to (node, 0).
  220. TRY(new_range->set_start(node, 0));
  221. // 4. Set newRange's end to (node, childCount).
  222. TRY(new_range->set_end(node, child_count));
  223. // 5. Set this's range to newRange.
  224. m_range = new_range;
  225. // 6. Set this's direction to forwards.
  226. m_direction = Direction::Forwards;
  227. return {};
  228. }
  229. // https://w3c.github.io/selection-api/#dom-selection-deletefromdocument
  230. WebIDL::ExceptionOr<void> Selection::delete_from_document()
  231. {
  232. // The method must invoke deleteContents() on this's range if this is not empty.
  233. // Otherwise the method must do nothing.
  234. if (!is_empty())
  235. return m_range->delete_contents();
  236. return {};
  237. }
  238. // https://w3c.github.io/selection-api/#dom-selection-containsnode
  239. bool Selection::contains_node(JS::NonnullGCPtr<DOM::Node> node, bool allow_partial_containment) const
  240. {
  241. // The method must return false if this is empty or if node's root is not the document associated with this.
  242. if (!m_range)
  243. return false;
  244. if (&node->root() != m_document.ptr())
  245. return false;
  246. // Otherwise, if allowPartialContainment is false, the method must return true if and only if
  247. // start of its range is before or visually equivalent to the first boundary point in the node
  248. // and end of its range is after or visually equivalent to the last boundary point in the node.
  249. if (!allow_partial_containment) {
  250. auto start_relative_position = DOM::position_of_boundary_point_relative_to_other_boundary_point(
  251. *m_range->start_container(),
  252. m_range->start_offset(),
  253. node,
  254. 0);
  255. auto end_relative_position = DOM::position_of_boundary_point_relative_to_other_boundary_point(
  256. *m_range->end_container(),
  257. m_range->end_offset(),
  258. node,
  259. node->length());
  260. return (start_relative_position == DOM::RelativeBoundaryPointPosition::Before || start_relative_position == DOM::RelativeBoundaryPointPosition::Equal)
  261. && (end_relative_position == DOM::RelativeBoundaryPointPosition::Equal || end_relative_position == DOM::RelativeBoundaryPointPosition::After);
  262. }
  263. // If allowPartialContainment is true, the method must return true if and only if
  264. // start of its range is before or visually equivalent to the last boundary point in the node
  265. // and end of its range is after or visually equivalent to the first boundary point in the node.
  266. auto start_relative_position = DOM::position_of_boundary_point_relative_to_other_boundary_point(
  267. *m_range->start_container(),
  268. m_range->start_offset(),
  269. node,
  270. node->length());
  271. auto end_relative_position = DOM::position_of_boundary_point_relative_to_other_boundary_point(
  272. *m_range->end_container(),
  273. m_range->end_offset(),
  274. node,
  275. 0);
  276. return (start_relative_position == DOM::RelativeBoundaryPointPosition::Before || start_relative_position == DOM::RelativeBoundaryPointPosition::Equal)
  277. && (end_relative_position == DOM::RelativeBoundaryPointPosition::Equal || end_relative_position == DOM::RelativeBoundaryPointPosition::After);
  278. }
  279. String Selection::to_string() const
  280. {
  281. // FIXME: This needs more work to be compatible with other engines.
  282. // See https://www.w3.org/Bugs/Public/show_bug.cgi?id=10583
  283. if (!m_range)
  284. return String::empty();
  285. return m_range->to_string();
  286. }
  287. }