Range.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/DocumentType.h>
  9. #include <LibWeb/DOM/Node.h>
  10. #include <LibWeb/DOM/Range.h>
  11. #include <LibWeb/HTML/Window.h>
  12. namespace Web::DOM {
  13. NonnullRefPtr<Range> Range::create(HTML::Window& window)
  14. {
  15. return Range::create(window.associated_document());
  16. }
  17. NonnullRefPtr<Range> Range::create(Document& document)
  18. {
  19. return adopt_ref(*new Range(document));
  20. }
  21. NonnullRefPtr<Range> Range::create(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
  22. {
  23. return adopt_ref(*new Range(start_container, start_offset, end_container, end_offset));
  24. }
  25. NonnullRefPtr<Range> Range::create_with_global_object(Bindings::WindowObject& window)
  26. {
  27. return Range::create(window.impl());
  28. }
  29. Range::Range(Document& document)
  30. : Range(document, 0, document, 0)
  31. {
  32. }
  33. Range::Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
  34. : AbstractRange(start_container, start_offset, end_container, end_offset)
  35. {
  36. }
  37. // https://dom.spec.whatwg.org/#concept-range-root
  38. Node& Range::root()
  39. {
  40. // The root of a live range is the root of its start node.
  41. return m_start_container->root();
  42. }
  43. Node const& Range::root() const
  44. {
  45. return m_start_container->root();
  46. }
  47. enum class RelativeBoundaryPointPosition {
  48. Equal,
  49. Before,
  50. After,
  51. };
  52. // https://dom.spec.whatwg.org/#concept-range-bp-position
  53. static RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(Node const& node_a, u32 offset_a, Node const& node_b, u32 offset_b)
  54. {
  55. // 1. Assert: nodeA and nodeB have the same root.
  56. VERIFY(&node_a.root() == &node_b.root());
  57. // 2. If nodeA is nodeB, then return equal if offsetA is offsetB, before if offsetA is less than offsetB, and after if offsetA is greater than offsetB.
  58. if (&node_a == &node_b) {
  59. if (offset_a == offset_b)
  60. return RelativeBoundaryPointPosition::Equal;
  61. if (offset_a < offset_b)
  62. return RelativeBoundaryPointPosition::Before;
  63. return RelativeBoundaryPointPosition::After;
  64. }
  65. // 3. If nodeA is following nodeB, then if the position of (nodeB, offsetB) relative to (nodeA, offsetA) is before, return after, and if it is after, return before.
  66. if (node_a.is_following(node_b)) {
  67. auto relative_position = position_of_boundary_point_relative_to_other_boundary_point(node_b, offset_b, node_a, offset_a);
  68. if (relative_position == RelativeBoundaryPointPosition::Before)
  69. return RelativeBoundaryPointPosition::After;
  70. if (relative_position == RelativeBoundaryPointPosition::After)
  71. return RelativeBoundaryPointPosition::Before;
  72. }
  73. // 4. If nodeA is an ancestor of nodeB:
  74. if (node_a.is_ancestor_of(node_b)) {
  75. // 1. Let child be nodeB.
  76. NonnullRefPtr<Node> child = node_b;
  77. // 2. While child is not a child of nodeA, set child to its parent.
  78. while (!node_a.is_parent_of(child)) {
  79. auto* parent = child->parent();
  80. VERIFY(parent);
  81. child = *parent;
  82. }
  83. // 3. If child’s index is less than offsetA, then return after.
  84. if (child->index() < offset_a)
  85. return RelativeBoundaryPointPosition::After;
  86. }
  87. // 5. Return before.
  88. return RelativeBoundaryPointPosition::Before;
  89. }
  90. ExceptionOr<void> Range::set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end)
  91. {
  92. // To set the start or end of a range to a boundary point (node, offset), run these steps:
  93. // 1. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
  94. if (is<DocumentType>(node))
  95. return InvalidNodeTypeError::create("Node cannot be a DocumentType.");
  96. // 2. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
  97. if (offset > node.length())
  98. return IndexSizeError::create(String::formatted("Node does not contain a child at offset {}", offset));
  99. // 3. Let bp be the boundary point (node, offset).
  100. if (start_or_end == StartOrEnd::Start) {
  101. // -> If these steps were invoked as "set the start"
  102. // 1. If range’s root is not equal to node’s root, or if bp is after the range’s end, set range’s end to bp.
  103. if (&root() != &node.root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset) == RelativeBoundaryPointPosition::After) {
  104. m_end_container = node;
  105. m_end_offset = offset;
  106. }
  107. // 2. Set range’s start to bp.
  108. m_start_container = node;
  109. m_start_offset = offset;
  110. } else {
  111. // -> If these steps were invoked as "set the end"
  112. VERIFY(start_or_end == StartOrEnd::End);
  113. // 1. If range’s root is not equal to node’s root, or if bp is before the range’s start, set range’s start to bp.
  114. if (&root() != &node.root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset) == RelativeBoundaryPointPosition::Before) {
  115. m_start_container = node;
  116. m_start_offset = offset;
  117. }
  118. // 2. Set range’s end to bp.
  119. m_end_container = node;
  120. m_end_offset = offset;
  121. }
  122. return {};
  123. }
  124. // https://dom.spec.whatwg.org/#concept-range-bp-set
  125. ExceptionOr<void> Range::set_start(Node& node, u32 offset)
  126. {
  127. // The setStart(node, offset) method steps are to set the start of this to boundary point (node, offset).
  128. return set_start_or_end(node, offset, StartOrEnd::Start);
  129. }
  130. ExceptionOr<void> Range::set_end(Node& node, u32 offset)
  131. {
  132. // The setEnd(node, offset) method steps are to set the end of this to boundary point (node, offset).
  133. return set_start_or_end(node, offset, StartOrEnd::End);
  134. }
  135. // https://dom.spec.whatwg.org/#dom-range-setstartbefore
  136. ExceptionOr<void> Range::set_start_before(Node& node)
  137. {
  138. // 1. Let parent be node’s parent.
  139. auto* parent = node.parent();
  140. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  141. if (!parent)
  142. return InvalidNodeTypeError::create("Given node has no parent.");
  143. // 3. Set the start of this to boundary point (parent, node’s index).
  144. return set_start_or_end(*parent, node.index(), StartOrEnd::Start);
  145. }
  146. // https://dom.spec.whatwg.org/#dom-range-setstartafter
  147. ExceptionOr<void> Range::set_start_after(Node& node)
  148. {
  149. // 1. Let parent be node’s parent.
  150. auto* parent = node.parent();
  151. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  152. if (!parent)
  153. return InvalidNodeTypeError::create("Given node has no parent.");
  154. // 3. Set the start of this to boundary point (parent, node’s index plus 1).
  155. return set_start_or_end(*parent, node.index() + 1, StartOrEnd::Start);
  156. }
  157. // https://dom.spec.whatwg.org/#dom-range-setendbefore
  158. ExceptionOr<void> Range::set_end_before(Node& node)
  159. {
  160. // 1. Let parent be node’s parent.
  161. auto* parent = node.parent();
  162. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  163. if (!parent)
  164. return InvalidNodeTypeError::create("Given node has no parent.");
  165. // 3. Set the end of this to boundary point (parent, node’s index).
  166. return set_start_or_end(*parent, node.index(), StartOrEnd::End);
  167. }
  168. // https://dom.spec.whatwg.org/#dom-range-setendafter
  169. ExceptionOr<void> Range::set_end_after(Node& node)
  170. {
  171. // 1. Let parent be node’s parent.
  172. auto* parent = node.parent();
  173. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  174. if (!parent)
  175. return InvalidNodeTypeError::create("Given node has no parent.");
  176. // 3. Set the end of this to boundary point (parent, node’s index plus 1).
  177. return set_start_or_end(*parent, node.index() + 1, StartOrEnd::End);
  178. }
  179. // https://dom.spec.whatwg.org/#dom-range-compareboundarypoints
  180. ExceptionOr<i16> Range::compare_boundary_points(u16 how, Range const& source_range) const
  181. {
  182. // 1. If how is not one of
  183. // - START_TO_START,
  184. // - START_TO_END,
  185. // - END_TO_END, and
  186. // - END_TO_START,
  187. // then throw a "NotSupportedError" DOMException.
  188. if (how != HowToCompareBoundaryPoints::START_TO_START && how != HowToCompareBoundaryPoints::START_TO_END && how != HowToCompareBoundaryPoints::END_TO_END && how != HowToCompareBoundaryPoints::END_TO_START)
  189. return NotSupportedError::create(String::formatted("Expected 'how' to be one of START_TO_START (0), START_TO_END (1), END_TO_END (2) or END_TO_START (3), got {}", how));
  190. // 2. If this’s root is not the same as sourceRange’s root, then throw a "WrongDocumentError" DOMException.
  191. if (&root() != &source_range.root())
  192. return WrongDocumentError::create("This range is not in the same tree as the source range.");
  193. RefPtr<Node> this_point_node;
  194. u32 this_point_offset = 0;
  195. RefPtr<Node> other_point_node;
  196. u32 other_point_offset = 0;
  197. // 3. If how is:
  198. switch (how) {
  199. case HowToCompareBoundaryPoints::START_TO_START:
  200. // -> START_TO_START:
  201. // Let this point be this’s start. Let other point be sourceRange’s start.
  202. this_point_node = m_start_container;
  203. this_point_offset = m_start_offset;
  204. other_point_node = source_range.m_start_container;
  205. other_point_offset = source_range.m_start_offset;
  206. break;
  207. case HowToCompareBoundaryPoints::START_TO_END:
  208. // -> START_TO_END:
  209. // Let this point be this’s end. Let other point be sourceRange’s start.
  210. this_point_node = m_end_container;
  211. this_point_offset = m_end_offset;
  212. other_point_node = source_range.m_start_container;
  213. other_point_offset = source_range.m_start_offset;
  214. break;
  215. case HowToCompareBoundaryPoints::END_TO_END:
  216. // -> END_TO_END:
  217. // Let this point be this’s end. Let other point be sourceRange’s end.
  218. this_point_node = m_end_container;
  219. this_point_offset = m_end_offset;
  220. other_point_node = source_range.m_end_container;
  221. other_point_offset = source_range.m_end_offset;
  222. break;
  223. case HowToCompareBoundaryPoints::END_TO_START:
  224. // -> END_TO_START:
  225. // Let this point be this’s start. Let other point be sourceRange’s end.
  226. this_point_node = m_start_container;
  227. this_point_offset = m_start_offset;
  228. other_point_node = source_range.m_end_container;
  229. other_point_offset = source_range.m_end_offset;
  230. break;
  231. default:
  232. VERIFY_NOT_REACHED();
  233. }
  234. VERIFY(this_point_node);
  235. VERIFY(other_point_node);
  236. // 4. If the position of this point relative to other point is
  237. auto relative_position = position_of_boundary_point_relative_to_other_boundary_point(*this_point_node, this_point_offset, *other_point_node, other_point_offset);
  238. switch (relative_position) {
  239. case RelativeBoundaryPointPosition::Before:
  240. // -> before
  241. // Return −1.
  242. return -1;
  243. case RelativeBoundaryPointPosition::Equal:
  244. // -> equal
  245. // Return 0.
  246. return 0;
  247. case RelativeBoundaryPointPosition::After:
  248. // -> after
  249. // Return 1.
  250. return 1;
  251. default:
  252. VERIFY_NOT_REACHED();
  253. }
  254. }
  255. // https://dom.spec.whatwg.org/#concept-range-select
  256. ExceptionOr<void> Range::select(Node& node)
  257. {
  258. // 1. Let parent be node’s parent.
  259. auto* parent = node.parent();
  260. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  261. if (!parent)
  262. return InvalidNodeTypeError::create("Given node has no parent.");
  263. // 3. Let index be node’s index.
  264. auto index = node.index();
  265. // 4. Set range’s start to boundary point (parent, index).
  266. m_start_container = *parent;
  267. m_start_offset = index;
  268. // 5. Set range’s end to boundary point (parent, index plus 1).
  269. m_end_container = *parent;
  270. m_end_offset = index + 1;
  271. return {};
  272. }
  273. // https://dom.spec.whatwg.org/#dom-range-selectnode
  274. ExceptionOr<void> Range::select_node(Node& node)
  275. {
  276. // The selectNode(node) method steps are to select node within this.
  277. return select(node);
  278. }
  279. // https://dom.spec.whatwg.org/#dom-range-collapse
  280. void Range::collapse(bool to_start)
  281. {
  282. // The collapse(toStart) method steps are to, if toStart is true, set end to start; otherwise set start to end.
  283. if (to_start) {
  284. m_end_container = m_start_container;
  285. m_end_offset = m_start_offset;
  286. return;
  287. }
  288. m_start_container = m_end_container;
  289. m_start_offset = m_end_offset;
  290. }
  291. // https://dom.spec.whatwg.org/#dom-range-selectnodecontents
  292. ExceptionOr<void> Range::select_node_contents(Node const& node)
  293. {
  294. // 1. If node is a doctype, throw an "InvalidNodeTypeError" DOMException.
  295. if (is<DocumentType>(node))
  296. return InvalidNodeTypeError::create("Node cannot be a DocumentType.");
  297. // 2. Let length be the length of node.
  298. auto length = node.length();
  299. // 3. Set start to the boundary point (node, 0).
  300. m_start_container = node;
  301. m_start_offset = 0;
  302. // 4. Set end to the boundary point (node, length).
  303. m_end_container = node;
  304. m_end_offset = length;
  305. return {};
  306. }
  307. NonnullRefPtr<Range> Range::clone_range() const
  308. {
  309. return adopt_ref(*new Range(const_cast<Node&>(*m_start_container), m_start_offset, const_cast<Node&>(*m_end_container), m_end_offset));
  310. }
  311. NonnullRefPtr<Range> Range::inverted() const
  312. {
  313. return adopt_ref(*new Range(const_cast<Node&>(*m_end_container), m_end_offset, const_cast<Node&>(*m_start_container), m_start_offset));
  314. }
  315. NonnullRefPtr<Range> Range::normalized() const
  316. {
  317. if (m_start_container.ptr() == m_end_container.ptr()) {
  318. if (m_start_offset <= m_end_offset)
  319. return clone_range();
  320. return inverted();
  321. }
  322. if (m_start_container->is_before(m_end_container))
  323. return clone_range();
  324. return inverted();
  325. }
  326. // https://dom.spec.whatwg.org/#dom-range-commonancestorcontainer
  327. NonnullRefPtr<Node> Range::common_ancestor_container() const
  328. {
  329. // 1. Let container be start node.
  330. auto container = m_start_container;
  331. // 2. While container is not an inclusive ancestor of end node, let container be container’s parent.
  332. while (!container->is_inclusive_ancestor_of(m_end_container)) {
  333. VERIFY(container->parent());
  334. container = *container->parent();
  335. }
  336. // 3. Return container.
  337. return container;
  338. }
  339. // https://dom.spec.whatwg.org/#dom-range-intersectsnode
  340. bool Range::intersects_node(Node const& node) const
  341. {
  342. // 1. If node’s root is different from this’s root, return false.
  343. if (&node.root() != &root())
  344. return false;
  345. // 2. Let parent be node’s parent.
  346. auto* parent = node.parent();
  347. // 3. If parent is null, return true.
  348. if (!parent)
  349. return true;
  350. // 4. Let offset be node’s index.
  351. auto offset = node.index();
  352. // 5. If (parent, offset) is before end and (parent, offset plus 1) is after start, return true
  353. auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset, m_end_container, m_end_offset);
  354. auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset + 1, m_start_container, m_start_offset);
  355. if (relative_position_to_end == RelativeBoundaryPointPosition::Before && relative_position_to_start == RelativeBoundaryPointPosition::After)
  356. return true;
  357. // 6. Return false.
  358. return false;
  359. }
  360. // https://dom.spec.whatwg.org/#dom-range-ispointinrange
  361. ExceptionOr<bool> Range::is_point_in_range(Node const& node, u32 offset) const
  362. {
  363. // 1. If node’s root is different from this’s root, return false.
  364. if (&node.root() != &root())
  365. return false;
  366. // 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
  367. if (is<DocumentType>(node))
  368. return InvalidNodeTypeError::create("Node cannot be a DocumentType.");
  369. // 3. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
  370. if (offset > node.length())
  371. return IndexSizeError::create(String::formatted("Node does not contain a child at offset {}", offset));
  372. // 4. If (node, offset) is before start or after end, return false.
  373. auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset);
  374. auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset);
  375. if (relative_position_to_start == RelativeBoundaryPointPosition::Before || relative_position_to_end == RelativeBoundaryPointPosition::After)
  376. return false;
  377. // 5. Return true.
  378. return true;
  379. }
  380. // https://dom.spec.whatwg.org/#dom-range-comparepoint
  381. ExceptionOr<i16> Range::compare_point(Node const& node, u32 offset) const
  382. {
  383. // 1. If node’s root is different from this’s root, then throw a "WrongDocumentError" DOMException.
  384. if (&node.root() != &root())
  385. return WrongDocumentError::create("Given node is not in the same document as the range.");
  386. // 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
  387. if (is<DocumentType>(node))
  388. return InvalidNodeTypeError::create("Node cannot be a DocumentType.");
  389. // 3. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
  390. if (offset > node.length())
  391. return IndexSizeError::create(String::formatted("Node does not contain a child at offset {}", offset));
  392. // 4. If (node, offset) is before start, return −1.
  393. auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset);
  394. if (relative_position_to_start == RelativeBoundaryPointPosition::Before)
  395. return -1;
  396. // 5. If (node, offset) is after end, return 1.
  397. auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset);
  398. if (relative_position_to_end == RelativeBoundaryPointPosition::After)
  399. return 1;
  400. // 6. Return 0.
  401. return 0;
  402. }
  403. }