Range.cpp 49 KB

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