Selection.cpp 18 KB

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