Selection.cpp 17 KB

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