Range.cpp 49 KB

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