Range.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  4. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/DOM/Comment.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/DocumentFragment.h>
  12. #include <LibWeb/DOM/DocumentType.h>
  13. #include <LibWeb/DOM/Node.h>
  14. #include <LibWeb/DOM/ProcessingInstruction.h>
  15. #include <LibWeb/DOM/Range.h>
  16. #include <LibWeb/DOM/Text.h>
  17. #include <LibWeb/Geometry/DOMRect.h>
  18. #include <LibWeb/HTML/Window.h>
  19. namespace Web::DOM {
  20. HashTable<Range*>& Range::live_ranges()
  21. {
  22. static HashTable<Range*> ranges;
  23. return ranges;
  24. }
  25. JS::NonnullGCPtr<Range> Range::create(HTML::Window& window)
  26. {
  27. return Range::create(window.associated_document());
  28. }
  29. JS::NonnullGCPtr<Range> Range::create(Document& document)
  30. {
  31. auto& realm = document.realm();
  32. return *realm.heap().allocate<Range>(realm, document);
  33. }
  34. JS::NonnullGCPtr<Range> Range::create(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
  35. {
  36. auto& realm = start_container.realm();
  37. return *realm.heap().allocate<Range>(realm, start_container, start_offset, end_container, end_offset);
  38. }
  39. JS::NonnullGCPtr<Range> Range::construct_impl(JS::Realm& realm)
  40. {
  41. auto& window = verify_cast<HTML::Window>(realm.global_object());
  42. return Range::create(window);
  43. }
  44. Range::Range(Document& document)
  45. : Range(document, 0, document, 0)
  46. {
  47. set_prototype(&Bindings::cached_web_prototype(document.realm(), "Range"));
  48. }
  49. Range::Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
  50. : AbstractRange(start_container, start_offset, end_container, end_offset)
  51. {
  52. set_prototype(&Bindings::cached_web_prototype(start_container.realm(), "Range"));
  53. live_ranges().set(this);
  54. }
  55. Range::~Range()
  56. {
  57. live_ranges().remove(this);
  58. }
  59. // https://dom.spec.whatwg.org/#concept-range-root
  60. Node& Range::root()
  61. {
  62. // The root of a live range is the root of its start node.
  63. return m_start_container->root();
  64. }
  65. Node const& Range::root() const
  66. {
  67. return m_start_container->root();
  68. }
  69. // https://dom.spec.whatwg.org/#concept-range-bp-position
  70. RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(Node const& node_a, u32 offset_a, Node const& node_b, u32 offset_b)
  71. {
  72. // 1. Assert: nodeA and nodeB have the same root.
  73. VERIFY(&node_a.root() == &node_b.root());
  74. // 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.
  75. if (&node_a == &node_b) {
  76. if (offset_a == offset_b)
  77. return RelativeBoundaryPointPosition::Equal;
  78. if (offset_a < offset_b)
  79. return RelativeBoundaryPointPosition::Before;
  80. return RelativeBoundaryPointPosition::After;
  81. }
  82. // 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.
  83. if (node_a.is_following(node_b)) {
  84. auto relative_position = position_of_boundary_point_relative_to_other_boundary_point(node_b, offset_b, node_a, offset_a);
  85. if (relative_position == RelativeBoundaryPointPosition::Before)
  86. return RelativeBoundaryPointPosition::After;
  87. if (relative_position == RelativeBoundaryPointPosition::After)
  88. return RelativeBoundaryPointPosition::Before;
  89. }
  90. // 4. If nodeA is an ancestor of nodeB:
  91. if (node_a.is_ancestor_of(node_b)) {
  92. // 1. Let child be nodeB.
  93. JS::NonnullGCPtr<Node> child = node_b;
  94. // 2. While child is not a child of nodeA, set child to its parent.
  95. while (!node_a.is_parent_of(child)) {
  96. auto* parent = child->parent();
  97. VERIFY(parent);
  98. child = parent;
  99. }
  100. // 3. If child’s index is less than offsetA, then return after.
  101. if (child->index() < offset_a)
  102. return RelativeBoundaryPointPosition::After;
  103. }
  104. // 5. Return before.
  105. return RelativeBoundaryPointPosition::Before;
  106. }
  107. WebIDL::ExceptionOr<void> Range::set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end)
  108. {
  109. // To set the start or end of a range to a boundary point (node, offset), run these steps:
  110. // 1. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
  111. if (is<DocumentType>(node))
  112. return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType.");
  113. // 2. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
  114. if (offset > node.length())
  115. return WebIDL::IndexSizeError::create(realm(), DeprecatedString::formatted("Node does not contain a child at offset {}", offset));
  116. // 3. Let bp be the boundary point (node, offset).
  117. if (start_or_end == StartOrEnd::Start) {
  118. // -> If these steps were invoked as "set the start"
  119. // 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.
  120. if (&root() != &node.root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset) == RelativeBoundaryPointPosition::After) {
  121. m_end_container = &node;
  122. m_end_offset = offset;
  123. }
  124. // 2. Set range’s start to bp.
  125. m_start_container = &node;
  126. m_start_offset = offset;
  127. } else {
  128. // -> If these steps were invoked as "set the end"
  129. VERIFY(start_or_end == StartOrEnd::End);
  130. // 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.
  131. if (&root() != &node.root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset) == RelativeBoundaryPointPosition::Before) {
  132. m_start_container = &node;
  133. m_start_offset = offset;
  134. }
  135. // 2. Set range’s end to bp.
  136. m_end_container = &node;
  137. m_end_offset = offset;
  138. }
  139. return {};
  140. }
  141. // https://dom.spec.whatwg.org/#concept-range-bp-set
  142. WebIDL::ExceptionOr<void> Range::set_start(Node& node, u32 offset)
  143. {
  144. // The setStart(node, offset) method steps are to set the start of this to boundary point (node, offset).
  145. return set_start_or_end(node, offset, StartOrEnd::Start);
  146. }
  147. WebIDL::ExceptionOr<void> Range::set_end(Node& node, u32 offset)
  148. {
  149. // The setEnd(node, offset) method steps are to set the end of this to boundary point (node, offset).
  150. return set_start_or_end(node, offset, StartOrEnd::End);
  151. }
  152. // https://dom.spec.whatwg.org/#dom-range-setstartbefore
  153. WebIDL::ExceptionOr<void> Range::set_start_before(Node& node)
  154. {
  155. // 1. Let parent be node’s parent.
  156. auto* parent = node.parent();
  157. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  158. if (!parent)
  159. return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
  160. // 3. Set the start of this to boundary point (parent, node’s index).
  161. return set_start_or_end(*parent, node.index(), StartOrEnd::Start);
  162. }
  163. // https://dom.spec.whatwg.org/#dom-range-setstartafter
  164. WebIDL::ExceptionOr<void> Range::set_start_after(Node& node)
  165. {
  166. // 1. Let parent be node’s parent.
  167. auto* parent = node.parent();
  168. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  169. if (!parent)
  170. return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
  171. // 3. Set the start of this to boundary point (parent, node’s index plus 1).
  172. return set_start_or_end(*parent, node.index() + 1, StartOrEnd::Start);
  173. }
  174. // https://dom.spec.whatwg.org/#dom-range-setendbefore
  175. WebIDL::ExceptionOr<void> Range::set_end_before(Node& node)
  176. {
  177. // 1. Let parent be node’s parent.
  178. auto* parent = node.parent();
  179. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  180. if (!parent)
  181. return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
  182. // 3. Set the end of this to boundary point (parent, node’s index).
  183. return set_start_or_end(*parent, node.index(), StartOrEnd::End);
  184. }
  185. // https://dom.spec.whatwg.org/#dom-range-setendafter
  186. WebIDL::ExceptionOr<void> Range::set_end_after(Node& node)
  187. {
  188. // 1. Let parent be node’s parent.
  189. auto* parent = node.parent();
  190. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  191. if (!parent)
  192. return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
  193. // 3. Set the end of this to boundary point (parent, node’s index plus 1).
  194. return set_start_or_end(*parent, node.index() + 1, StartOrEnd::End);
  195. }
  196. // https://dom.spec.whatwg.org/#dom-range-compareboundarypoints
  197. WebIDL::ExceptionOr<i16> Range::compare_boundary_points(u16 how, Range const& source_range) const
  198. {
  199. // 1. If how is not one of
  200. // - START_TO_START,
  201. // - START_TO_END,
  202. // - END_TO_END, and
  203. // - END_TO_START,
  204. // then throw a "NotSupportedError" DOMException.
  205. if (how != HowToCompareBoundaryPoints::START_TO_START && how != HowToCompareBoundaryPoints::START_TO_END && how != HowToCompareBoundaryPoints::END_TO_END && how != HowToCompareBoundaryPoints::END_TO_START)
  206. return WebIDL::NotSupportedError::create(realm(), DeprecatedString::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));
  207. // 2. If this’s root is not the same as sourceRange’s root, then throw a "WrongDocumentError" DOMException.
  208. if (&root() != &source_range.root())
  209. return WebIDL::WrongDocumentError::create(realm(), "This range is not in the same tree as the source range.");
  210. JS::GCPtr<Node> this_point_node;
  211. u32 this_point_offset = 0;
  212. JS::GCPtr<Node> other_point_node;
  213. u32 other_point_offset = 0;
  214. // 3. If how is:
  215. switch (how) {
  216. case HowToCompareBoundaryPoints::START_TO_START:
  217. // -> START_TO_START:
  218. // Let this point be this’s start. Let other point be sourceRange’s start.
  219. this_point_node = m_start_container;
  220. this_point_offset = m_start_offset;
  221. other_point_node = source_range.m_start_container;
  222. other_point_offset = source_range.m_start_offset;
  223. break;
  224. case HowToCompareBoundaryPoints::START_TO_END:
  225. // -> START_TO_END:
  226. // Let this point be this’s end. Let other point be sourceRange’s start.
  227. this_point_node = m_end_container;
  228. this_point_offset = m_end_offset;
  229. other_point_node = source_range.m_start_container;
  230. other_point_offset = source_range.m_start_offset;
  231. break;
  232. case HowToCompareBoundaryPoints::END_TO_END:
  233. // -> END_TO_END:
  234. // Let this point be this’s end. Let other point be sourceRange’s end.
  235. this_point_node = m_end_container;
  236. this_point_offset = m_end_offset;
  237. other_point_node = source_range.m_end_container;
  238. other_point_offset = source_range.m_end_offset;
  239. break;
  240. case HowToCompareBoundaryPoints::END_TO_START:
  241. // -> END_TO_START:
  242. // Let this point be this’s start. Let other point be sourceRange’s end.
  243. this_point_node = m_start_container;
  244. this_point_offset = m_start_offset;
  245. other_point_node = source_range.m_end_container;
  246. other_point_offset = source_range.m_end_offset;
  247. break;
  248. default:
  249. VERIFY_NOT_REACHED();
  250. }
  251. VERIFY(this_point_node);
  252. VERIFY(other_point_node);
  253. // 4. If the position of this point relative to other point is
  254. auto relative_position = position_of_boundary_point_relative_to_other_boundary_point(*this_point_node, this_point_offset, *other_point_node, other_point_offset);
  255. switch (relative_position) {
  256. case RelativeBoundaryPointPosition::Before:
  257. // -> before
  258. // Return −1.
  259. return -1;
  260. case RelativeBoundaryPointPosition::Equal:
  261. // -> equal
  262. // Return 0.
  263. return 0;
  264. case RelativeBoundaryPointPosition::After:
  265. // -> after
  266. // Return 1.
  267. return 1;
  268. default:
  269. VERIFY_NOT_REACHED();
  270. }
  271. }
  272. // https://dom.spec.whatwg.org/#concept-range-select
  273. WebIDL::ExceptionOr<void> Range::select(Node& node)
  274. {
  275. // 1. Let parent be node’s parent.
  276. auto* parent = node.parent();
  277. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
  278. if (!parent)
  279. return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
  280. // 3. Let index be node’s index.
  281. auto index = node.index();
  282. // 4. Set range’s start to boundary point (parent, index).
  283. m_start_container = parent;
  284. m_start_offset = index;
  285. // 5. Set range’s end to boundary point (parent, index plus 1).
  286. m_end_container = parent;
  287. m_end_offset = index + 1;
  288. return {};
  289. }
  290. // https://dom.spec.whatwg.org/#dom-range-selectnode
  291. WebIDL::ExceptionOr<void> Range::select_node(Node& node)
  292. {
  293. // The selectNode(node) method steps are to select node within this.
  294. return select(node);
  295. }
  296. // https://dom.spec.whatwg.org/#dom-range-collapse
  297. void Range::collapse(bool to_start)
  298. {
  299. // The collapse(toStart) method steps are to, if toStart is true, set end to start; otherwise set start to end.
  300. if (to_start) {
  301. m_end_container = m_start_container;
  302. m_end_offset = m_start_offset;
  303. return;
  304. }
  305. m_start_container = m_end_container;
  306. m_start_offset = m_end_offset;
  307. }
  308. // https://dom.spec.whatwg.org/#dom-range-selectnodecontents
  309. WebIDL::ExceptionOr<void> Range::select_node_contents(Node const& node)
  310. {
  311. // 1. If node is a doctype, throw an "InvalidNodeTypeError" DOMException.
  312. if (is<DocumentType>(node))
  313. return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType.");
  314. // 2. Let length be the length of node.
  315. auto length = node.length();
  316. // 3. Set start to the boundary point (node, 0).
  317. m_start_container = &node;
  318. m_start_offset = 0;
  319. // 4. Set end to the boundary point (node, length).
  320. m_end_container = &node;
  321. m_end_offset = length;
  322. return {};
  323. }
  324. JS::NonnullGCPtr<Range> Range::clone_range() const
  325. {
  326. return *heap().allocate<Range>(shape().realm(), const_cast<Node&>(*m_start_container), m_start_offset, const_cast<Node&>(*m_end_container), m_end_offset);
  327. }
  328. JS::NonnullGCPtr<Range> Range::inverted() const
  329. {
  330. return *heap().allocate<Range>(shape().realm(), const_cast<Node&>(*m_end_container), m_end_offset, const_cast<Node&>(*m_start_container), m_start_offset);
  331. }
  332. JS::NonnullGCPtr<Range> Range::normalized() const
  333. {
  334. if (m_start_container.ptr() == m_end_container.ptr()) {
  335. if (m_start_offset <= m_end_offset)
  336. return clone_range();
  337. return inverted();
  338. }
  339. if (m_start_container->is_before(m_end_container))
  340. return clone_range();
  341. return inverted();
  342. }
  343. // https://dom.spec.whatwg.org/#dom-range-commonancestorcontainer
  344. JS::NonnullGCPtr<Node> Range::common_ancestor_container() const
  345. {
  346. // 1. Let container be start node.
  347. auto container = m_start_container;
  348. // 2. While container is not an inclusive ancestor of end node, let container be container’s parent.
  349. while (!container->is_inclusive_ancestor_of(m_end_container)) {
  350. VERIFY(container->parent());
  351. container = container->parent();
  352. }
  353. // 3. Return container.
  354. return container;
  355. }
  356. // https://dom.spec.whatwg.org/#dom-range-intersectsnode
  357. bool Range::intersects_node(Node const& node) const
  358. {
  359. // 1. If node’s root is different from this’s root, return false.
  360. if (&node.root() != &root())
  361. return false;
  362. // 2. Let parent be node’s parent.
  363. auto* parent = node.parent();
  364. // 3. If parent is null, return true.
  365. if (!parent)
  366. return true;
  367. // 4. Let offset be node’s index.
  368. auto offset = node.index();
  369. // 5. If (parent, offset) is before end and (parent, offset plus 1) is after start, return true
  370. auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset, m_end_container, m_end_offset);
  371. auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset + 1, m_start_container, m_start_offset);
  372. if (relative_position_to_end == RelativeBoundaryPointPosition::Before && relative_position_to_start == RelativeBoundaryPointPosition::After)
  373. return true;
  374. // 6. Return false.
  375. return false;
  376. }
  377. // https://dom.spec.whatwg.org/#dom-range-ispointinrange
  378. WebIDL::ExceptionOr<bool> Range::is_point_in_range(Node const& node, u32 offset) const
  379. {
  380. // 1. If node’s root is different from this’s root, return false.
  381. if (&node.root() != &root())
  382. return false;
  383. // 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
  384. if (is<DocumentType>(node))
  385. return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType.");
  386. // 3. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
  387. if (offset > node.length())
  388. return WebIDL::IndexSizeError::create(realm(), DeprecatedString::formatted("Node does not contain a child at offset {}", offset));
  389. // 4. If (node, offset) is before start or after end, return false.
  390. auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset);
  391. auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset);
  392. if (relative_position_to_start == RelativeBoundaryPointPosition::Before || relative_position_to_end == RelativeBoundaryPointPosition::After)
  393. return false;
  394. // 5. Return true.
  395. return true;
  396. }
  397. // https://dom.spec.whatwg.org/#dom-range-comparepoint
  398. WebIDL::ExceptionOr<i16> Range::compare_point(Node const& node, u32 offset) const
  399. {
  400. // 1. If node’s root is different from this’s root, then throw a "WrongDocumentError" DOMException.
  401. if (&node.root() != &root())
  402. return WebIDL::WrongDocumentError::create(realm(), "Given node is not in the same document as the range.");
  403. // 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
  404. if (is<DocumentType>(node))
  405. return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType.");
  406. // 3. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
  407. if (offset > node.length())
  408. return WebIDL::IndexSizeError::create(realm(), DeprecatedString::formatted("Node does not contain a child at offset {}", offset));
  409. // 4. If (node, offset) is before start, return −1.
  410. auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset);
  411. if (relative_position_to_start == RelativeBoundaryPointPosition::Before)
  412. return -1;
  413. // 5. If (node, offset) is after end, return 1.
  414. auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset);
  415. if (relative_position_to_end == RelativeBoundaryPointPosition::After)
  416. return 1;
  417. // 6. Return 0.
  418. return 0;
  419. }
  420. // https://dom.spec.whatwg.org/#dom-range-stringifier
  421. DeprecatedString Range::to_deprecated_string() const
  422. {
  423. // 1. Let s be the empty string.
  424. StringBuilder builder;
  425. // 2. If this’s start node is this’s end node and it is a Text node,
  426. // then return the substring of that Text node’s data beginning at this’s start offset and ending at this’s end offset.
  427. if (start_container() == end_container() && is<Text>(*start_container()))
  428. return static_cast<Text const&>(*start_container()).data().substring(start_offset(), end_offset() - start_offset());
  429. // 3. If this’s start node is a Text node, then append the substring of that node’s data from this’s start offset until the end to s.
  430. if (is<Text>(*start_container()))
  431. builder.append(static_cast<Text const&>(*start_container()).data().substring_view(start_offset()));
  432. // 4. Append the concatenation of the data of all Text nodes that are contained in this, in tree order, to s.
  433. for (Node const* node = start_container(); node != end_container()->next_sibling(); node = node->next_in_pre_order()) {
  434. if (is<Text>(*node) && contains_node(*node))
  435. builder.append(static_cast<Text const&>(*node).data());
  436. }
  437. // 5. If this’s end node is a Text node, then append the substring of that node’s data from its start until this’s end offset to s.
  438. if (is<Text>(*end_container()))
  439. builder.append(static_cast<Text const&>(*end_container()).data().substring_view(0, end_offset()));
  440. // 6. Return s.
  441. return builder.to_deprecated_string();
  442. }
  443. // https://dom.spec.whatwg.org/#dom-range-extractcontents
  444. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::extract_contents()
  445. {
  446. return extract();
  447. }
  448. // https://dom.spec.whatwg.org/#concept-range-extract
  449. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::extract()
  450. {
  451. // 1. Let fragment be a new DocumentFragment node whose node document is range’s start node’s node document.
  452. auto* fragment = heap().allocate<DOM::DocumentFragment>(realm(), const_cast<Document&>(start_container()->document()));
  453. // 2. If range is collapsed, then return fragment.
  454. if (collapsed())
  455. return JS::NonnullGCPtr(*fragment);
  456. // 3. Let original start node, original start offset, original end node, and original end offset
  457. // be range’s start node, start offset, end node, and end offset, respectively.
  458. JS::NonnullGCPtr<Node> original_start_node = m_start_container;
  459. auto original_start_offset = m_start_offset;
  460. JS::NonnullGCPtr<Node> original_end_node = m_end_container;
  461. auto original_end_offset = m_end_offset;
  462. // 4. If original start node is original end node and it is a CharacterData node, then:
  463. if (original_start_node.ptr() == original_end_node.ptr() && is<CharacterData>(*original_start_node)) {
  464. // 1. Let clone be a clone of original start node.
  465. auto clone = original_start_node->clone_node();
  466. // 2. Set the data of clone to the result of substringing data with node original start node,
  467. // offset original start offset, and count original end offset minus original start offset.
  468. auto result = TRY(static_cast<CharacterData const&>(*original_start_node).substring_data(original_start_offset, original_end_offset - original_start_offset));
  469. verify_cast<CharacterData>(*clone).set_data(move(result));
  470. // 3. Append clone to fragment.
  471. TRY(fragment->append_child(clone));
  472. // 4. Replace data with node original start node, offset original start offset, count original end offset minus original start offset, and data the empty string.
  473. TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_end_offset - original_start_offset, ""));
  474. // 5. Return fragment.
  475. return JS::NonnullGCPtr(*fragment);
  476. }
  477. // 5. Let common ancestor be original start node.
  478. JS::NonnullGCPtr<Node> common_ancestor = original_start_node;
  479. // 6. While common ancestor is not an inclusive ancestor of original end node, set common ancestor to its own parent.
  480. while (!common_ancestor->is_inclusive_ancestor_of(original_end_node))
  481. common_ancestor = common_ancestor->parent_node();
  482. // 7. Let first partially contained child be null.
  483. JS::GCPtr<Node> first_partially_contained_child;
  484. // 8. If original start node is not an inclusive ancestor of original end node,
  485. // set first partially contained child to the first child of common ancestor that is partially contained in range.
  486. if (!original_start_node->is_inclusive_ancestor_of(original_end_node)) {
  487. for (auto* child = common_ancestor->first_child(); child; child = child->next_sibling()) {
  488. if (partially_contains_node(*child)) {
  489. first_partially_contained_child = child;
  490. break;
  491. }
  492. }
  493. }
  494. // 9. Let last partially contained child be null.
  495. JS::GCPtr<Node> last_partially_contained_child;
  496. // 10. If original end node is not an inclusive ancestor of original start node,
  497. // set last partially contained child to the last child of common ancestor that is partially contained in range.
  498. if (!original_end_node->is_inclusive_ancestor_of(original_start_node)) {
  499. for (auto* child = common_ancestor->last_child(); child; child = child->previous_sibling()) {
  500. if (partially_contains_node(*child)) {
  501. last_partially_contained_child = child;
  502. break;
  503. }
  504. }
  505. }
  506. // 11. Let contained children be a list of all children of common ancestor that are contained in range, in tree order.
  507. Vector<JS::NonnullGCPtr<Node>> contained_children;
  508. for (Node const* node = common_ancestor->first_child(); node; node = node->next_sibling()) {
  509. if (contains_node(*node))
  510. contained_children.append(*node);
  511. }
  512. // 12. If any member of contained children is a doctype, then throw a "HierarchyRequestError" DOMException.
  513. for (auto const& child : contained_children) {
  514. if (is<DocumentType>(*child))
  515. return WebIDL::HierarchyRequestError::create(realm(), "Contained child is a DocumentType");
  516. }
  517. JS::GCPtr<Node> new_node;
  518. size_t new_offset = 0;
  519. // 13. If original start node is an inclusive ancestor of original end node, set new node to original start node and new offset to original start offset.
  520. if (original_start_node->is_inclusive_ancestor_of(original_end_node)) {
  521. new_node = original_start_node;
  522. new_offset = original_start_offset;
  523. }
  524. // 14. Otherwise:
  525. else {
  526. // 1. Let reference node equal original start node.
  527. JS::GCPtr<Node> reference_node = original_start_node;
  528. // 2. While reference node’s parent is not null and is not an inclusive ancestor of original end node, set reference node to its parent.
  529. while (reference_node->parent_node() && !reference_node->parent_node()->is_inclusive_ancestor_of(original_end_node))
  530. reference_node = reference_node->parent_node();
  531. // 3. Set new node to the parent of reference node, and new offset to one plus reference node’s index.
  532. new_node = reference_node->parent_node();
  533. new_offset = 1 + reference_node->index();
  534. }
  535. // 15. If first partially contained child is a CharacterData node, then:
  536. if (first_partially_contained_child && is<CharacterData>(*first_partially_contained_child)) {
  537. // 1. Let clone be a clone of original start node.
  538. auto clone = original_start_node->clone_node();
  539. // 2. Set the data of clone to the result of substringing data with node original start node, offset original start offset,
  540. // and count original start node’s length minus original start offset.
  541. auto result = TRY(static_cast<CharacterData const&>(*original_start_node).substring_data(original_start_offset, original_start_node->length() - original_start_offset));
  542. verify_cast<CharacterData>(*clone).set_data(move(result));
  543. // 3. Append clone to fragment.
  544. TRY(fragment->append_child(clone));
  545. // 4. Replace data with node original start node, offset original start offset, count original start node’s length minus original start offset, and data the empty string.
  546. TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_start_node->length() - original_start_offset, ""));
  547. }
  548. // 16. Otherwise, if first partially contained child is not null:
  549. else if (first_partially_contained_child) {
  550. // 1. Let clone be a clone of first partially contained child.
  551. auto clone = first_partially_contained_child->clone_node();
  552. // 2. Append clone to fragment.
  553. TRY(fragment->append_child(clone));
  554. // 3. Let subrange be a new live range whose start is (original start node, original start offset) and whose end is (first partially contained child, first partially contained child’s length).
  555. auto subrange = Range::create(original_start_node, original_start_offset, *first_partially_contained_child, first_partially_contained_child->length());
  556. // 4. Let subfragment be the result of extracting subrange.
  557. auto subfragment = TRY(subrange->extract());
  558. // 5. Append subfragment to clone.
  559. TRY(clone->append_child(subfragment));
  560. }
  561. // 17. For each contained child in contained children, append contained child to fragment.
  562. for (auto& contained_child : contained_children) {
  563. TRY(fragment->append_child(contained_child));
  564. }
  565. // 18. If last partially contained child is a CharacterData node, then:
  566. if (last_partially_contained_child && is<CharacterData>(*last_partially_contained_child)) {
  567. // 1. Let clone be a clone of original end node.
  568. auto clone = original_end_node->clone_node();
  569. // 2. Set the data of clone to the result of substringing data with node original end node, offset 0, and count original end offset.
  570. auto result = TRY(static_cast<CharacterData const&>(*original_end_node).substring_data(0, original_end_offset));
  571. verify_cast<CharacterData>(*clone).set_data(move(result));
  572. // 3. Append clone to fragment.
  573. TRY(fragment->append_child(clone));
  574. // 4. Replace data with node original end node, offset 0, count original end offset, and data the empty string.
  575. TRY(verify_cast<CharacterData>(*original_end_node).replace_data(0, original_end_offset, ""));
  576. }
  577. // 19. Otherwise, if last partially contained child is not null:
  578. else if (last_partially_contained_child) {
  579. // 1. Let clone be a clone of last partially contained child.
  580. auto clone = last_partially_contained_child->clone_node();
  581. // 2. Append clone to fragment.
  582. TRY(fragment->append_child(clone));
  583. // 3. Let subrange be a new live range whose start is (last partially contained child, 0) and whose end is (original end node, original end offset).
  584. auto subrange = Range::create(*last_partially_contained_child, 0, original_end_node, original_end_offset);
  585. // 4. Let subfragment be the result of extracting subrange.
  586. auto subfragment = TRY(subrange->extract());
  587. // 5. Append subfragment to clone.
  588. TRY(clone->append_child(subfragment));
  589. }
  590. // 20. Set range’s start and end to (new node, new offset).
  591. TRY(set_start(*new_node, new_offset));
  592. TRY(set_end(*new_node, new_offset));
  593. // 21. Return fragment.
  594. return JS::NonnullGCPtr(*fragment);
  595. }
  596. // https://dom.spec.whatwg.org/#contained
  597. bool Range::contains_node(Node const& node) const
  598. {
  599. // A node node is contained in a live range range if node’s root is range’s root,
  600. if (&node.root() != &root())
  601. return false;
  602. // and (node, 0) is after range’s start,
  603. if (position_of_boundary_point_relative_to_other_boundary_point(node, 0, m_start_container, m_start_offset) != RelativeBoundaryPointPosition::After)
  604. return false;
  605. // and (node, node’s length) is before range’s end.
  606. if (position_of_boundary_point_relative_to_other_boundary_point(node, node.length(), m_end_container, m_end_offset) != RelativeBoundaryPointPosition::Before)
  607. return false;
  608. return true;
  609. }
  610. // https://dom.spec.whatwg.org/#partially-contained
  611. bool Range::partially_contains_node(Node const& node) const
  612. {
  613. // A node is partially contained in a live range if it’s an inclusive ancestor of the live range’s start node but not its end node, or vice versa.
  614. if (node.is_inclusive_ancestor_of(m_start_container) && &node != m_end_container.ptr())
  615. return true;
  616. if (node.is_inclusive_ancestor_of(m_end_container) && &node != m_start_container.ptr())
  617. return true;
  618. return false;
  619. }
  620. // https://dom.spec.whatwg.org/#dom-range-insertnode
  621. WebIDL::ExceptionOr<void> Range::insert_node(JS::NonnullGCPtr<Node> node)
  622. {
  623. return insert(node);
  624. }
  625. // https://dom.spec.whatwg.org/#concept-range-insert
  626. WebIDL::ExceptionOr<void> Range::insert(JS::NonnullGCPtr<Node> node)
  627. {
  628. // 1. If range’s start node is a ProcessingInstruction or Comment node, is a Text node whose parent is null, or is node, then throw a "HierarchyRequestError" DOMException.
  629. if ((is<ProcessingInstruction>(*m_start_container) || is<Comment>(*m_start_container))
  630. || (is<Text>(*m_start_container) && !m_start_container->parent_node())
  631. || m_start_container.ptr() == node.ptr()) {
  632. return WebIDL::HierarchyRequestError::create(realm(), "Range has inappropriate start node for insertion");
  633. }
  634. // 2. Let referenceNode be null.
  635. JS::GCPtr<Node> reference_node;
  636. // 3. If range’s start node is a Text node, set referenceNode to that Text node.
  637. if (is<Text>(*m_start_container)) {
  638. reference_node = m_start_container;
  639. }
  640. // 4. Otherwise, set referenceNode to the child of start node whose index is start offset, and null if there is no such child.
  641. else {
  642. reference_node = m_start_container->child_at_index(m_start_offset);
  643. }
  644. // 5. Let parent be range’s start node if referenceNode is null, and referenceNode’s parent otherwise.
  645. JS::GCPtr<Node> parent;
  646. if (!reference_node)
  647. parent = m_start_container;
  648. else
  649. parent = reference_node->parent();
  650. // 6. Ensure pre-insertion validity of node into parent before referenceNode.
  651. TRY(parent->ensure_pre_insertion_validity(node, reference_node));
  652. // 7. If range’s start node is a Text node, set referenceNode to the result of splitting it with offset range’s start offset.
  653. if (is<Text>(*m_start_container))
  654. reference_node = TRY(static_cast<Text&>(*m_start_container).split_text(m_start_offset));
  655. // 8. If node is referenceNode, set referenceNode to its next sibling.
  656. if (node == reference_node)
  657. reference_node = reference_node->next_sibling();
  658. // 9. If node’s parent is non-null, then remove node.
  659. if (node->parent())
  660. node->remove();
  661. // 10. Let newOffset be parent’s length if referenceNode is null, and referenceNode’s index otherwise.
  662. size_t new_offset = 0;
  663. if (!reference_node)
  664. new_offset = parent->length();
  665. else
  666. new_offset = reference_node->index();
  667. // 11. Increase newOffset by node’s length if node is a DocumentFragment node, and one otherwise.
  668. if (is<DocumentFragment>(*node))
  669. new_offset += node->length();
  670. else
  671. new_offset += 1;
  672. // 12. Pre-insert node into parent before referenceNode.
  673. (void)TRY(parent->pre_insert(node, reference_node));
  674. // 13. If range is collapsed, then set range’s end to (parent, newOffset).
  675. if (collapsed())
  676. TRY(set_end(*parent, new_offset));
  677. return {};
  678. }
  679. // https://dom.spec.whatwg.org/#dom-range-surroundcontents
  680. WebIDL::ExceptionOr<void> Range::surround_contents(JS::NonnullGCPtr<Node> new_parent)
  681. {
  682. // 1. If a non-Text node is partially contained in this, then throw an "InvalidStateError" DOMException.
  683. Node* start_non_text_node = start_container();
  684. if (is<Text>(*start_non_text_node))
  685. start_non_text_node = start_non_text_node->parent_node();
  686. Node* end_non_text_node = end_container();
  687. if (is<Text>(*end_non_text_node))
  688. end_non_text_node = end_non_text_node->parent_node();
  689. if (start_non_text_node != end_non_text_node)
  690. return WebIDL::InvalidStateError::create(realm(), "Non-Text node is partially contained in range.");
  691. // 2. If newParent is a Document, DocumentType, or DocumentFragment node, then throw an "InvalidNodeTypeError" DOMException.
  692. if (is<Document>(*new_parent) || is<DocumentType>(*new_parent) || is<DocumentFragment>(*new_parent))
  693. return WebIDL::InvalidNodeTypeError::create(realm(), "Invalid parent node type");
  694. // 3. Let fragment be the result of extracting this.
  695. auto fragment = TRY(extract());
  696. // 4. If newParent has children, then replace all with null within newParent.
  697. if (new_parent->has_children())
  698. new_parent->replace_all(nullptr);
  699. // 5. Insert newParent into this.
  700. TRY(insert(new_parent));
  701. // 6. Append fragment to newParent.
  702. (void)TRY(new_parent->append_child(fragment));
  703. // 7. Select newParent within this.
  704. return select(*new_parent);
  705. }
  706. // https://dom.spec.whatwg.org/#dom-range-clonecontents
  707. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::clone_contents()
  708. {
  709. return clone_the_contents();
  710. }
  711. // https://dom.spec.whatwg.org/#concept-range-clone
  712. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::clone_the_contents()
  713. {
  714. // 1. Let fragment be a new DocumentFragment node whose node document is range’s start node’s node document.
  715. auto* fragment = heap().allocate<DOM::DocumentFragment>(realm(), const_cast<Document&>(start_container()->document()));
  716. // 2. If range is collapsed, then return fragment.
  717. if (collapsed())
  718. return JS::NonnullGCPtr(*fragment);
  719. // 3. Let original start node, original start offset, original end node, and original end offset
  720. // be range’s start node, start offset, end node, and end offset, respectively.
  721. JS::NonnullGCPtr<Node> original_start_node = m_start_container;
  722. auto original_start_offset = m_start_offset;
  723. JS::NonnullGCPtr<Node> original_end_node = m_end_container;
  724. auto original_end_offset = m_end_offset;
  725. // 4. If original start node is original end node and it is a CharacterData node, then:
  726. if (original_start_node.ptr() == original_end_node.ptr() && is<CharacterData>(*original_start_node)) {
  727. // 1. Let clone be a clone of original start node.
  728. auto clone = original_start_node->clone_node();
  729. // 2. Set the data of clone to the result of substringing data with node original start node,
  730. // offset original start offset, and count original end offset minus original start offset.
  731. auto result = TRY(static_cast<CharacterData const&>(*original_start_node).substring_data(original_start_offset, original_end_offset - original_start_offset));
  732. verify_cast<CharacterData>(*clone).set_data(move(result));
  733. // 3. Append clone to fragment.
  734. TRY(fragment->append_child(clone));
  735. // 4. Return fragment.
  736. return JS::NonnullGCPtr(*fragment);
  737. }
  738. // 5. Let common ancestor be original start node.
  739. JS::NonnullGCPtr<Node> common_ancestor = original_start_node;
  740. // 6. While common ancestor is not an inclusive ancestor of original end node, set common ancestor to its own parent.
  741. while (!common_ancestor->is_inclusive_ancestor_of(original_end_node))
  742. common_ancestor = common_ancestor->parent_node();
  743. // 7. Let first partially contained child be null.
  744. JS::GCPtr<Node> first_partially_contained_child;
  745. // 8. If original start node is not an inclusive ancestor of original end node,
  746. // set first partially contained child to the first child of common ancestor that is partially contained in range.
  747. if (!original_start_node->is_inclusive_ancestor_of(original_end_node)) {
  748. for (auto* child = common_ancestor->first_child(); child; child = child->next_sibling()) {
  749. if (partially_contains_node(*child)) {
  750. first_partially_contained_child = child;
  751. break;
  752. }
  753. }
  754. }
  755. // 9. Let last partially contained child be null.
  756. JS::GCPtr<Node> last_partially_contained_child;
  757. // 10. If original end node is not an inclusive ancestor of original start node,
  758. // set last partially contained child to the last child of common ancestor that is partially contained in range.
  759. if (!original_end_node->is_inclusive_ancestor_of(original_start_node)) {
  760. for (auto* child = common_ancestor->last_child(); child; child = child->previous_sibling()) {
  761. if (partially_contains_node(*child)) {
  762. last_partially_contained_child = child;
  763. break;
  764. }
  765. }
  766. }
  767. // 11. Let contained children be a list of all children of common ancestor that are contained in range, in tree order.
  768. Vector<JS::NonnullGCPtr<Node>> contained_children;
  769. for (Node const* node = common_ancestor->first_child(); node; node = node->next_sibling()) {
  770. if (contains_node(*node))
  771. contained_children.append(*node);
  772. }
  773. // 12. If any member of contained children is a doctype, then throw a "HierarchyRequestError" DOMException.
  774. for (auto const& child : contained_children) {
  775. if (is<DocumentType>(*child))
  776. return WebIDL::HierarchyRequestError::create(realm(), "Contained child is a DocumentType");
  777. }
  778. // 13. If first partially contained child is a CharacterData node, then:
  779. if (first_partially_contained_child && is<CharacterData>(*first_partially_contained_child)) {
  780. // 1. Let clone be a clone of original start node.
  781. auto clone = original_start_node->clone_node();
  782. // 2. Set the data of clone to the result of substringing data with node original start node, offset original start offset,
  783. // and count original start node’s length minus original start offset.
  784. auto result = TRY(static_cast<CharacterData const&>(*original_start_node).substring_data(original_start_offset, original_start_node->length() - original_start_offset));
  785. verify_cast<CharacterData>(*clone).set_data(move(result));
  786. // 3. Append clone to fragment.
  787. TRY(fragment->append_child(clone));
  788. }
  789. // 14. Otherwise, if first partially contained child is not null:
  790. else if (first_partially_contained_child) {
  791. // 1. Let clone be a clone of first partially contained child.
  792. auto clone = first_partially_contained_child->clone_node();
  793. // 2. Append clone to fragment.
  794. TRY(fragment->append_child(clone));
  795. // 3. Let subrange be a new live range whose start is (original start node, original start offset) and whose end is (first partially contained child, first partially contained child’s length).
  796. auto subrange = Range::create(original_start_node, original_start_offset, *first_partially_contained_child, first_partially_contained_child->length());
  797. // 4. Let subfragment be the result of cloning the contents of subrange.
  798. auto subfragment = TRY(subrange->clone_the_contents());
  799. // 5. Append subfragment to clone.
  800. TRY(clone->append_child(subfragment));
  801. }
  802. // 15. For each contained child in contained children.
  803. for (auto& contained_child : contained_children) {
  804. // 1. Let clone be a clone of contained child with the clone children flag set.
  805. auto clone = contained_child->clone_node(nullptr, true);
  806. // 2. Append clone to fragment.
  807. TRY(fragment->append_child(move(clone)));
  808. }
  809. // 16. If last partially contained child is a CharacterData node, then:
  810. if (last_partially_contained_child && is<CharacterData>(*last_partially_contained_child)) {
  811. // 1. Let clone be a clone of original end node.
  812. auto clone = original_end_node->clone_node();
  813. // 2. Set the data of clone to the result of substringing data with node original end node, offset 0, and count original end offset.
  814. auto result = TRY(static_cast<CharacterData const&>(*original_end_node).substring_data(0, original_end_offset));
  815. verify_cast<CharacterData>(*clone).set_data(move(result));
  816. // 3. Append clone to fragment.
  817. TRY(fragment->append_child(clone));
  818. }
  819. // 17. Otherwise, if last partially contained child is not null:
  820. else if (last_partially_contained_child) {
  821. // 1. Let clone be a clone of last partially contained child.
  822. auto clone = last_partially_contained_child->clone_node();
  823. // 2. Append clone to fragment.
  824. TRY(fragment->append_child(clone));
  825. // 3. Let subrange be a new live range whose start is (last partially contained child, 0) and whose end is (original end node, original end offset).
  826. auto subrange = Range::create(*last_partially_contained_child, 0, original_end_node, original_end_offset);
  827. // 4. Let subfragment be the result of cloning the contents of subrange.
  828. auto subfragment = TRY(subrange->clone_the_contents());
  829. // 5. Append subfragment to clone.
  830. TRY(clone->append_child(subfragment));
  831. }
  832. // 18. Return fragment.
  833. return JS::NonnullGCPtr(*fragment);
  834. }
  835. // https://dom.spec.whatwg.org/#dom-range-deletecontents
  836. WebIDL::ExceptionOr<void> Range::delete_contents()
  837. {
  838. // 1. If this is collapsed, then return.
  839. if (collapsed())
  840. return {};
  841. // 2. Let original start node, original start offset, original end node, and original end offset be this’s start node, start offset, end node, and end offset, respectively.
  842. JS::NonnullGCPtr<Node> original_start_node = m_start_container;
  843. auto original_start_offset = m_start_offset;
  844. JS::NonnullGCPtr<Node> original_end_node = m_end_container;
  845. auto original_end_offset = m_end_offset;
  846. // 3. If original start node is original end node and it is a CharacterData node, then replace data with node original start node, offset original start offset,
  847. // count original end offset minus original start offset, and data the empty string, and then return.
  848. if (original_start_node.ptr() == original_end_node.ptr() && is<CharacterData>(*original_start_node)) {
  849. TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_end_offset - original_start_offset, ""));
  850. return {};
  851. }
  852. // 4. Let nodes to remove be a list of all the nodes that are contained in this, in tree order, omitting any node whose parent is also contained in this.
  853. JS::MarkedVector<Node*> nodes_to_remove(heap());
  854. for (Node const* node = start_container(); node != end_container()->next_in_pre_order(); node = node->next_in_pre_order()) {
  855. if (contains_node(*node) && (!node->parent_node() || !contains_node(*node->parent_node())))
  856. nodes_to_remove.append(const_cast<Node*>(node));
  857. }
  858. JS::GCPtr<Node> new_node;
  859. size_t new_offset = 0;
  860. // 5. If original start node is an inclusive ancestor of original end node, set new node to original start node and new offset to original start offset.
  861. if (original_start_node->is_inclusive_ancestor_of(original_end_node)) {
  862. new_node = original_start_node;
  863. new_offset = original_start_offset;
  864. }
  865. // 6. Otherwise
  866. else {
  867. // 1. Let reference node equal original start node.
  868. auto reference_node = original_start_node;
  869. // 2. While reference node’s parent is not null and is not an inclusive ancestor of original end node, set reference node to its parent.
  870. while (reference_node->parent_node() && !reference_node->parent_node()->is_inclusive_ancestor_of(original_end_node))
  871. reference_node = reference_node->parent_node();
  872. // 3. Set new node to the parent of reference node, and new offset to one plus the index of reference node.
  873. new_node = reference_node->parent_node();
  874. new_offset = 1 + reference_node->index();
  875. }
  876. // 7. If original start node is a CharacterData node, then replace data with node original start node, offset original start offset, count original start node’s length minus original start offset, data the empty string.
  877. if (is<CharacterData>(*original_start_node))
  878. TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_start_node->length() - original_start_offset, ""));
  879. // 8. For each node in nodes to remove, in tree order, remove node.
  880. for (auto& node : nodes_to_remove)
  881. node->remove();
  882. // 9. If original end node is a CharacterData node, then replace data with node original end node, offset 0, count original end offset and data the empty string.
  883. if (is<CharacterData>(*original_end_node))
  884. TRY(static_cast<CharacterData&>(*original_end_node).replace_data(0, original_end_offset, ""));
  885. // 10. Set start and end to (new node, new offset).
  886. TRY(set_start(*new_node, new_offset));
  887. TRY(set_end(*new_node, new_offset));
  888. return {};
  889. }
  890. // https://w3c.github.io/csswg-drafts/cssom-view/#dom-range-getboundingclientrect
  891. JS::NonnullGCPtr<Geometry::DOMRect> Range::get_bounding_client_rect() const
  892. {
  893. dbgln("(STUBBED) Range::get_bounding_client_rect()");
  894. return Geometry::DOMRect::construct_impl(realm(), 0, 0, 0, 0);
  895. }
  896. }