Range.cpp 48 KB

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