Selection.cpp 16 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/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. }
  20. Selection::~Selection() = default;
  21. void Selection::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::SelectionPrototype>(realm, "Selection"));
  25. }
  26. // https://w3c.github.io/selection-api/#dfn-empty
  27. bool Selection::is_empty() const
  28. {
  29. // Each selection can be associated with a single range.
  30. // When there is no range associated with the selection, the selection is empty.
  31. // The selection must be initially empty.
  32. // NOTE: This function should not be confused with Selection.empty() which empties the selection.
  33. return !m_range;
  34. }
  35. void Selection::visit_edges(Cell::Visitor& visitor)
  36. {
  37. Base::visit_edges(visitor);
  38. visitor.visit(m_range);
  39. visitor.visit(m_document);
  40. }
  41. // https://w3c.github.io/selection-api/#dfn-anchor
  42. JS::GCPtr<DOM::Node> Selection::anchor_node()
  43. {
  44. if (!m_range)
  45. return nullptr;
  46. if (m_direction == Direction::Forwards)
  47. return m_range->start_container();
  48. return m_range->end_container();
  49. }
  50. // https://w3c.github.io/selection-api/#dfn-anchor
  51. unsigned Selection::anchor_offset()
  52. {
  53. if (!m_range)
  54. return 0;
  55. if (m_direction == Direction::Forwards)
  56. return m_range->start_offset();
  57. return m_range->end_offset();
  58. }
  59. // https://w3c.github.io/selection-api/#dfn-focus
  60. JS::GCPtr<DOM::Node> Selection::focus_node()
  61. {
  62. if (!m_range)
  63. return nullptr;
  64. if (m_direction == Direction::Forwards)
  65. return m_range->end_container();
  66. return m_range->start_container();
  67. }
  68. // https://w3c.github.io/selection-api/#dfn-focus
  69. unsigned Selection::focus_offset() const
  70. {
  71. if (!m_range)
  72. return 0;
  73. if (m_direction == Direction::Forwards)
  74. return m_range->end_offset();
  75. return m_range->start_offset();
  76. }
  77. // https://w3c.github.io/selection-api/#dom-selection-iscollapsed
  78. bool Selection::is_collapsed() const
  79. {
  80. // The attribute must return true if and only if the anchor and focus are the same
  81. // (including if both are null). Otherwise it must return false.
  82. return const_cast<Selection*>(this)->anchor_node() == const_cast<Selection*>(this)->focus_node();
  83. }
  84. // https://w3c.github.io/selection-api/#dom-selection-rangecount
  85. unsigned Selection::range_count() const
  86. {
  87. if (m_range)
  88. return 1;
  89. return 0;
  90. }
  91. String Selection::type() const
  92. {
  93. if (!m_range)
  94. return "None"_string;
  95. if (m_range->collapsed())
  96. return "Caret"_string;
  97. return "Range"_string;
  98. }
  99. // https://w3c.github.io/selection-api/#dom-selection-getrangeat
  100. WebIDL::ExceptionOr<JS::GCPtr<DOM::Range>> Selection::get_range_at(unsigned index)
  101. {
  102. // The method must throw an IndexSizeError exception if index is not 0, or if this is empty.
  103. if (index != 0 || is_empty())
  104. return WebIDL::IndexSizeError::create(realm(), "Selection.getRangeAt() on empty Selection or with invalid argument"sv);
  105. // Otherwise, it must return a reference to (not a copy of) this's range.
  106. return m_range;
  107. }
  108. // https://w3c.github.io/selection-api/#dom-selection-addrange
  109. void Selection::add_range(JS::NonnullGCPtr<DOM::Range> range)
  110. {
  111. // 1. If the root of the range's boundary points are not the document associated with this, abort these steps.
  112. if (&range->start_container()->root() != m_document.ptr())
  113. return;
  114. // 2. If rangeCount is not 0, abort these steps.
  115. if (range_count() != 0)
  116. return;
  117. // 3. Set this's range to range by a strong reference (not by making a copy).
  118. set_range(range);
  119. }
  120. // https://w3c.github.io/selection-api/#dom-selection-removerange
  121. WebIDL::ExceptionOr<void> Selection::remove_range(JS::NonnullGCPtr<DOM::Range> range)
  122. {
  123. // The method must make this empty by disassociating its range if this's range is range.
  124. if (m_range == range) {
  125. set_range(nullptr);
  126. return {};
  127. }
  128. // Otherwise, it must throw a NotFoundError.
  129. return WebIDL::NotFoundError::create(realm(), "Selection.removeRange() with invalid argument"sv);
  130. }
  131. // https://w3c.github.io/selection-api/#dom-selection-removeallranges
  132. void Selection::remove_all_ranges()
  133. {
  134. // The method must make this empty by disassociating its range if this has an associated range.
  135. set_range(nullptr);
  136. }
  137. // https://w3c.github.io/selection-api/#dom-selection-empty
  138. void Selection::empty()
  139. {
  140. // The method must be an alias, and behave identically, to removeAllRanges().
  141. remove_all_ranges();
  142. }
  143. // https://w3c.github.io/selection-api/#dom-selection-collapse
  144. WebIDL::ExceptionOr<void> Selection::collapse(JS::GCPtr<DOM::Node> node, unsigned offset)
  145. {
  146. // 1. If node is null, this method must behave identically as removeAllRanges() and abort these steps.
  147. if (!node) {
  148. remove_all_ranges();
  149. return {};
  150. }
  151. // 2. The method must throw an IndexSizeError exception if offset is longer than node's length and abort these steps.
  152. if (offset > node->length()) {
  153. return WebIDL::IndexSizeError::create(realm(), "Selection.collapse() with offset longer than node's length"sv);
  154. }
  155. // 3. If node's root is not the document associated with this, abort these steps.
  156. if (&node->root() != m_document.ptr())
  157. return {};
  158. // 4. Otherwise, let newRange be a new range.
  159. auto new_range = DOM::Range::create(*m_document);
  160. // 5. Set the start the start and the end of newRange to (node, offset).
  161. TRY(new_range->set_start(*node, offset));
  162. // 6. Set this's range to newRange.
  163. set_range(new_range);
  164. return {};
  165. }
  166. // https://w3c.github.io/selection-api/#dom-selection-setposition
  167. WebIDL::ExceptionOr<void> Selection::set_position(JS::GCPtr<DOM::Node> node, unsigned offset)
  168. {
  169. // The method must be an alias, and behave identically, to collapse().
  170. return collapse(node, offset);
  171. }
  172. // https://w3c.github.io/selection-api/#dom-selection-collapsetostart
  173. WebIDL::ExceptionOr<void> Selection::collapse_to_start()
  174. {
  175. // 1. The method must throw InvalidStateError exception if the this is empty.
  176. if (!m_range) {
  177. return WebIDL::InvalidStateError::create(realm(), "Selection.collapse_to_start() on empty range"sv);
  178. }
  179. // 2. Otherwise, it must create a new range
  180. auto new_range = DOM::Range::create(*m_document);
  181. // 3. Set the start both its start and end to the start of this's range
  182. TRY(new_range->set_start(*anchor_node(), m_range->start_offset()));
  183. TRY(new_range->set_end(*anchor_node(), m_range->start_offset()));
  184. // 4. Then set this's range to the newly-created range.
  185. set_range(new_range);
  186. return {};
  187. }
  188. // https://w3c.github.io/selection-api/#dom-selection-collapsetoend
  189. WebIDL::ExceptionOr<void> Selection::collapse_to_end()
  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_end() on empty range"sv);
  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->end_offset()));
  199. TRY(new_range->set_end(*anchor_node(), m_range->end_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-extend
  205. WebIDL::ExceptionOr<void> Selection::extend(JS::NonnullGCPtr<DOM::Node> node, unsigned offset)
  206. {
  207. // 1. If node's root is not the document associated with this, abort these steps.
  208. if (&node->root() != m_document.ptr())
  209. return {};
  210. // 2. If this is empty, throw an InvalidStateError exception and abort these steps.
  211. if (!m_range) {
  212. return WebIDL::InvalidStateError::create(realm(), "Selection.extend() on empty range"sv);
  213. }
  214. // 3. Let oldAnchor and oldFocus be the this's anchor and focus, and let newFocus be the boundary point (node, offset).
  215. auto& old_anchor_node = *anchor_node();
  216. auto old_anchor_offset = anchor_offset();
  217. auto& new_focus_node = node;
  218. auto new_focus_offset = offset;
  219. // 4. Let newRange be a new range.
  220. auto new_range = DOM::Range::create(*m_document);
  221. // 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.
  222. if (&node->root() != &m_range->start_container()->root()) {
  223. TRY(new_range->set_start(new_focus_node, new_focus_offset));
  224. }
  225. // 6. Otherwise, if oldAnchor is before or equal to newFocus, set the start newRange's start to oldAnchor, then set its end to newFocus.
  226. else if (old_anchor_node.is_before(new_focus_node) || &old_anchor_node == new_focus_node.ptr()) {
  227. TRY(new_range->set_end(new_focus_node, new_focus_offset));
  228. }
  229. // 7. Otherwise, set the start newRange's start to newFocus, then set its end to oldAnchor.
  230. else {
  231. TRY(new_range->set_start(new_focus_node, new_focus_offset));
  232. TRY(new_range->set_end(old_anchor_node, old_anchor_offset));
  233. }
  234. // 8. Set this's range to newRange.
  235. set_range(new_range);
  236. // 9. If newFocus is before oldAnchor, set this's direction to backwards. Otherwise, set it to forwards.
  237. if (new_focus_node->is_before(old_anchor_node)) {
  238. m_direction = Direction::Backwards;
  239. } else {
  240. m_direction = Direction::Forwards;
  241. }
  242. return {};
  243. }
  244. // https://w3c.github.io/selection-api/#dom-selection-setbaseandextent
  245. 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)
  246. {
  247. // 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.
  248. if (anchor_offset > anchor_node->length())
  249. return WebIDL::IndexSizeError::create(realm(), "Anchor offset points outside of the anchor node");
  250. if (focus_offset > focus_node->length())
  251. return WebIDL::IndexSizeError::create(realm(), "Focus offset points outside of the focus node");
  252. // 2. If the roots of anchorNode or focusNode are not the document associated with this, abort these steps.
  253. if (&anchor_node->root() != m_document.ptr())
  254. return {};
  255. if (&focus_node->root() != m_document.ptr())
  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 String::from_deprecated_string(m_range->to_deprecated_string()).release_value();
  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. }