Selection.cpp 17 KB

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