Range.cpp 47 KB

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