Range.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. NonnullRefPtr<Range> Range::clone_range() const
  259. {
  260. return adopt_ref(*new Range(const_cast<Node&>(*m_start_container), m_start_offset, const_cast<Node&>(*m_end_container), m_end_offset));
  261. }
  262. NonnullRefPtr<Range> Range::inverted() const
  263. {
  264. return adopt_ref(*new Range(const_cast<Node&>(*m_end_container), m_end_offset, const_cast<Node&>(*m_start_container), m_start_offset));
  265. }
  266. NonnullRefPtr<Range> Range::normalized() const
  267. {
  268. if (m_start_container.ptr() == m_end_container.ptr()) {
  269. if (m_start_offset <= m_end_offset)
  270. return clone_range();
  271. return inverted();
  272. }
  273. if (m_start_container->is_before(m_end_container))
  274. return clone_range();
  275. return inverted();
  276. }
  277. // https://dom.spec.whatwg.org/#dom-range-commonancestorcontainer
  278. NonnullRefPtr<Node> Range::common_ancestor_container() const
  279. {
  280. // 1. Let container be start node.
  281. auto container = m_start_container;
  282. // 2. While container is not an inclusive ancestor of end node, let container be container’s parent.
  283. while (!container->is_inclusive_ancestor_of(m_end_container)) {
  284. VERIFY(container->parent());
  285. container = *container->parent();
  286. }
  287. // 3. Return container.
  288. return container;
  289. }
  290. }