Range.cpp 48 KB

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