Range.cpp 18 KB

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