Selection.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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/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. if (!m_range)
  85. return true;
  86. return const_cast<Selection*>(this)->anchor_node() == const_cast<Selection*>(this)->focus_node()
  87. && m_range->start_offset() == m_range->end_offset();
  88. }
  89. // https://w3c.github.io/selection-api/#dom-selection-rangecount
  90. unsigned Selection::range_count() const
  91. {
  92. if (m_range)
  93. return 1;
  94. return 0;
  95. }
  96. String Selection::type() const
  97. {
  98. if (!m_range)
  99. return "None"_string;
  100. if (m_range->collapsed())
  101. return "Caret"_string;
  102. return "Range"_string;
  103. }
  104. String Selection::direction() const
  105. {
  106. if (!m_range || m_direction == Direction::Directionless)
  107. return "none"_string;
  108. if (m_direction == Direction::Forwards)
  109. return "forward"_string;
  110. return "backward"_string;
  111. }
  112. // https://w3c.github.io/selection-api/#dom-selection-getrangeat
  113. WebIDL::ExceptionOr<JS::GCPtr<DOM::Range>> Selection::get_range_at(unsigned index)
  114. {
  115. // The method must throw an IndexSizeError exception if index is not 0, or if this is empty.
  116. if (index != 0 || is_empty())
  117. return WebIDL::IndexSizeError::create(realm(), "Selection.getRangeAt() on empty Selection or with invalid argument"_string);
  118. // Otherwise, it must return a reference to (not a copy of) this's range.
  119. return m_range;
  120. }
  121. // https://w3c.github.io/selection-api/#dom-selection-addrange
  122. void Selection::add_range(JS::NonnullGCPtr<DOM::Range> range)
  123. {
  124. // 1. If the root of the range's boundary points are not the document associated with this, abort these steps.
  125. if (&range->start_container()->root() != m_document.ptr())
  126. return;
  127. // 2. If rangeCount is not 0, abort these steps.
  128. if (range_count() != 0)
  129. return;
  130. // 3. Set this's range to range by a strong reference (not by making a copy).
  131. set_range(range);
  132. // AD-HOC: WPT selection/removeAllRanges.html and selection/addRange.htm expect this
  133. m_direction = Direction::Forwards;
  134. }
  135. // https://w3c.github.io/selection-api/#dom-selection-removerange
  136. WebIDL::ExceptionOr<void> Selection::remove_range(JS::NonnullGCPtr<DOM::Range> range)
  137. {
  138. // The method must make this empty by disassociating its range if this's range is range.
  139. if (m_range == range) {
  140. set_range(nullptr);
  141. return {};
  142. }
  143. // Otherwise, it must throw a NotFoundError.
  144. return WebIDL::NotFoundError::create(realm(), "Selection.removeRange() with invalid argument"_string);
  145. }
  146. // https://w3c.github.io/selection-api/#dom-selection-removeallranges
  147. void Selection::remove_all_ranges()
  148. {
  149. // The method must make this empty by disassociating its range if this has an associated range.
  150. set_range(nullptr);
  151. }
  152. // https://w3c.github.io/selection-api/#dom-selection-empty
  153. void Selection::empty()
  154. {
  155. // The method must be an alias, and behave identically, to removeAllRanges().
  156. remove_all_ranges();
  157. }
  158. // https://w3c.github.io/selection-api/#dom-selection-collapse
  159. WebIDL::ExceptionOr<void> Selection::collapse(JS::GCPtr<DOM::Node> node, unsigned offset)
  160. {
  161. // 1. If node is null, this method must behave identically as removeAllRanges() and abort these steps.
  162. if (!node) {
  163. remove_all_ranges();
  164. return {};
  165. }
  166. // 2. The method must throw an IndexSizeError exception if offset is longer than node's length and abort these steps.
  167. if (offset > node->length()) {
  168. return WebIDL::IndexSizeError::create(realm(), "Selection.collapse() with offset longer than node's length"_string);
  169. }
  170. // 3. If document associated with this is not a shadow-including inclusive ancestor of node, abort these steps.
  171. if (!m_document->is_shadow_including_inclusive_ancestor_of(*node))
  172. return {};
  173. // 4. Otherwise, let newRange be a new range.
  174. auto new_range = DOM::Range::create(*m_document);
  175. // 5. Set the start the start and the end of newRange to (node, offset).
  176. TRY(new_range->set_start(*node, offset));
  177. // 6. Set this's range to newRange.
  178. set_range(new_range);
  179. return {};
  180. }
  181. // https://w3c.github.io/selection-api/#dom-selection-setposition
  182. WebIDL::ExceptionOr<void> Selection::set_position(JS::GCPtr<DOM::Node> node, unsigned offset)
  183. {
  184. // The method must be an alias, and behave identically, to collapse().
  185. return collapse(node, offset);
  186. }
  187. // https://w3c.github.io/selection-api/#dom-selection-collapsetostart
  188. WebIDL::ExceptionOr<void> Selection::collapse_to_start()
  189. {
  190. // 1. The method must throw InvalidStateError exception if the this is empty.
  191. if (!m_range) {
  192. return WebIDL::InvalidStateError::create(realm(), "Selection.collapse_to_start() on empty range"_string);
  193. }
  194. // 2. Otherwise, it must create a new range
  195. auto new_range = DOM::Range::create(*m_document);
  196. // 3. Set the start both its start and end to the start of this's range
  197. TRY(new_range->set_start(*anchor_node(), m_range->start_offset()));
  198. TRY(new_range->set_end(*anchor_node(), m_range->start_offset()));
  199. // 4. Then set this's range to the newly-created range.
  200. set_range(new_range);
  201. return {};
  202. }
  203. // https://w3c.github.io/selection-api/#dom-selection-collapsetoend
  204. WebIDL::ExceptionOr<void> Selection::collapse_to_end()
  205. {
  206. // 1. The method must throw InvalidStateError exception if the this is empty.
  207. if (!m_range) {
  208. return WebIDL::InvalidStateError::create(realm(), "Selection.collapse_to_end() on empty range"_string);
  209. }
  210. // 2. Otherwise, it must create a new range
  211. auto new_range = DOM::Range::create(*m_document);
  212. // 3. Set the start both its start and end to the start of this's range
  213. TRY(new_range->set_start(*anchor_node(), m_range->end_offset()));
  214. TRY(new_range->set_end(*anchor_node(), m_range->end_offset()));
  215. // 4. Then set this's range to the newly-created range.
  216. set_range(new_range);
  217. return {};
  218. }
  219. // https://w3c.github.io/selection-api/#dom-selection-extend
  220. WebIDL::ExceptionOr<void> Selection::extend(JS::NonnullGCPtr<DOM::Node> node, unsigned offset)
  221. {
  222. // 1. If the document associated with this is not a shadow-including inclusive ancestor of node, abort these steps.
  223. if (!m_document->is_shadow_including_inclusive_ancestor_of(node))
  224. return {};
  225. // 2. If this is empty, throw an InvalidStateError exception and abort these steps.
  226. if (!m_range) {
  227. return WebIDL::InvalidStateError::create(realm(), "Selection.extend() on empty range"_string);
  228. }
  229. // 3. Let oldAnchor and oldFocus be the this's anchor and focus, and let newFocus be the boundary point (node, offset).
  230. auto& old_anchor_node = *anchor_node();
  231. auto old_anchor_offset = anchor_offset();
  232. auto& new_focus_node = node;
  233. auto new_focus_offset = offset;
  234. // 4. Let newRange be a new range.
  235. auto new_range = DOM::Range::create(*m_document);
  236. // 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.
  237. if (&node->root() != &m_range->start_container()->root()) {
  238. TRY(new_range->set_start(new_focus_node, new_focus_offset));
  239. TRY(new_range->set_end(new_focus_node, new_focus_offset));
  240. }
  241. // 6. Otherwise, if oldAnchor is before or equal to newFocus, set the start newRange's start to oldAnchor, then set its end to newFocus.
  242. else if (old_anchor_node.is_before(new_focus_node) || &old_anchor_node == new_focus_node.ptr()) {
  243. TRY(new_range->set_start(old_anchor_node, old_anchor_offset));
  244. TRY(new_range->set_end(new_focus_node, new_focus_offset));
  245. }
  246. // 7. Otherwise, set the start newRange's start to newFocus, then set its end to oldAnchor.
  247. else {
  248. TRY(new_range->set_start(new_focus_node, new_focus_offset));
  249. TRY(new_range->set_end(old_anchor_node, old_anchor_offset));
  250. }
  251. // 8. Set this's range to newRange.
  252. set_range(new_range);
  253. // 9. If newFocus is before oldAnchor, set this's direction to backwards. Otherwise, set it to forwards.
  254. if (new_focus_node->is_before(old_anchor_node)) {
  255. m_direction = Direction::Backwards;
  256. } else {
  257. m_direction = Direction::Forwards;
  258. }
  259. return {};
  260. }
  261. // https://w3c.github.io/selection-api/#dom-selection-setbaseandextent
  262. 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)
  263. {
  264. // 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.
  265. if (anchor_offset > anchor_node->length())
  266. return WebIDL::IndexSizeError::create(realm(), "Anchor offset points outside of the anchor node"_string);
  267. if (focus_offset > focus_node->length())
  268. return WebIDL::IndexSizeError::create(realm(), "Focus offset points outside of the focus node"_string);
  269. // 2. If document associated with this is not a shadow-including inclusive ancestor of anchorNode or focusNode, abort these steps.
  270. if (!m_document->is_shadow_including_inclusive_ancestor_of(anchor_node) || !m_document->is_shadow_including_inclusive_ancestor_of(focus_node))
  271. return {};
  272. // 3. Let anchor be the boundary point (anchorNode, anchorOffset) and let focus be the boundary point (focusNode, focusOffset).
  273. // 4. Let newRange be a new range.
  274. auto new_range = DOM::Range::create(*m_document);
  275. // 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.
  276. 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);
  277. if (position_of_anchor_relative_to_focus == DOM::RelativeBoundaryPointPosition::Before) {
  278. TRY(new_range->set_start(anchor_node, anchor_offset));
  279. TRY(new_range->set_end(focus_node, focus_offset));
  280. } else {
  281. TRY(new_range->set_start(focus_node, focus_offset));
  282. TRY(new_range->set_end(anchor_node, anchor_offset));
  283. }
  284. // 6. Set this's range to newRange.
  285. set_range(new_range);
  286. // 7. If focus is before anchor, set this's direction to backwards. Otherwise, set it to forwards
  287. // NOTE: "Otherwise" can be seen as "focus is equal to or after anchor".
  288. if (position_of_anchor_relative_to_focus == DOM::RelativeBoundaryPointPosition::After)
  289. m_direction = Direction::Backwards;
  290. else
  291. m_direction = Direction::Forwards;
  292. return {};
  293. }
  294. // https://w3c.github.io/selection-api/#dom-selection-selectallchildren
  295. WebIDL::ExceptionOr<void> Selection::select_all_children(JS::NonnullGCPtr<DOM::Node> node)
  296. {
  297. // 1. If node's root is not the document associated with this, abort these steps.
  298. if (&node->root() != m_document.ptr())
  299. return {};
  300. // 2. Let newRange be a new range and childCount be the number of children of node.
  301. auto new_range = DOM::Range::create(*m_document);
  302. auto child_count = node->child_count();
  303. // 3. Set newRange's start to (node, 0).
  304. TRY(new_range->set_start(node, 0));
  305. // 4. Set newRange's end to (node, childCount).
  306. TRY(new_range->set_end(node, child_count));
  307. // 5. Set this's range to newRange.
  308. set_range(new_range);
  309. // 6. Set this's direction to forwards.
  310. m_direction = Direction::Forwards;
  311. return {};
  312. }
  313. // https://w3c.github.io/selection-api/#dom-selection-deletefromdocument
  314. WebIDL::ExceptionOr<void> Selection::delete_from_document()
  315. {
  316. // The method must invoke deleteContents() on this's range if this is not empty.
  317. // Otherwise the method must do nothing.
  318. if (!is_empty())
  319. return m_range->delete_contents();
  320. return {};
  321. }
  322. // https://w3c.github.io/selection-api/#dom-selection-containsnode
  323. bool Selection::contains_node(JS::NonnullGCPtr<DOM::Node> node, bool allow_partial_containment) const
  324. {
  325. // The method must return false if this is empty or if node's root is not the document associated with this.
  326. if (!m_range)
  327. return false;
  328. if (&node->root() != m_document.ptr())
  329. return false;
  330. // Otherwise, if allowPartialContainment is false, the method must return true if and only if
  331. // start of its range is before or visually equivalent to the first boundary point in the node
  332. // and end of its range is after or visually equivalent to the last boundary point in the node.
  333. if (!allow_partial_containment) {
  334. auto start_relative_position = DOM::position_of_boundary_point_relative_to_other_boundary_point(
  335. *m_range->start_container(),
  336. m_range->start_offset(),
  337. node,
  338. 0);
  339. auto end_relative_position = DOM::position_of_boundary_point_relative_to_other_boundary_point(
  340. *m_range->end_container(),
  341. m_range->end_offset(),
  342. node,
  343. node->length());
  344. return (start_relative_position == DOM::RelativeBoundaryPointPosition::Before || start_relative_position == DOM::RelativeBoundaryPointPosition::Equal)
  345. && (end_relative_position == DOM::RelativeBoundaryPointPosition::Equal || end_relative_position == DOM::RelativeBoundaryPointPosition::After);
  346. }
  347. // If allowPartialContainment is true, the method must return true if and only if
  348. // start of its range is before or visually equivalent to the last boundary point in the node
  349. // and end of its range is after or visually equivalent to the first boundary point in the node.
  350. auto start_relative_position = DOM::position_of_boundary_point_relative_to_other_boundary_point(
  351. *m_range->start_container(),
  352. m_range->start_offset(),
  353. node,
  354. node->length());
  355. auto end_relative_position = DOM::position_of_boundary_point_relative_to_other_boundary_point(
  356. *m_range->end_container(),
  357. m_range->end_offset(),
  358. node,
  359. 0);
  360. return (start_relative_position == DOM::RelativeBoundaryPointPosition::Before || start_relative_position == DOM::RelativeBoundaryPointPosition::Equal)
  361. && (end_relative_position == DOM::RelativeBoundaryPointPosition::Equal || end_relative_position == DOM::RelativeBoundaryPointPosition::After);
  362. }
  363. String Selection::to_string() const
  364. {
  365. // FIXME: This needs more work to be compatible with other engines.
  366. // See https://www.w3.org/Bugs/Public/show_bug.cgi?id=10583
  367. if (!m_range)
  368. return String {};
  369. return m_range->to_string();
  370. }
  371. JS::NonnullGCPtr<DOM::Document> Selection::document() const
  372. {
  373. return m_document;
  374. }
  375. JS::GCPtr<DOM::Range> Selection::range() const
  376. {
  377. return m_range;
  378. }
  379. void Selection::set_range(JS::GCPtr<DOM::Range> range)
  380. {
  381. if (m_range == range)
  382. return;
  383. if (m_range)
  384. m_range->set_associated_selection({}, nullptr);
  385. m_range = range;
  386. if (m_range)
  387. m_range->set_associated_selection({}, this);
  388. }
  389. }