Serenity Operating System
1/*
2 * Copyright (c) 2020, the SerenityOS developers.
3 * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
4 * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 */
8
9#include <LibWeb/Bindings/Intrinsics.h>
10#include <LibWeb/DOM/Comment.h>
11#include <LibWeb/DOM/Document.h>
12#include <LibWeb/DOM/DocumentFragment.h>
13#include <LibWeb/DOM/DocumentType.h>
14#include <LibWeb/DOM/ElementFactory.h>
15#include <LibWeb/DOM/Node.h>
16#include <LibWeb/DOM/ProcessingInstruction.h>
17#include <LibWeb/DOM/Range.h>
18#include <LibWeb/DOM/Text.h>
19#include <LibWeb/DOMParsing/InnerHTML.h>
20#include <LibWeb/Geometry/DOMRect.h>
21#include <LibWeb/HTML/HTMLHtmlElement.h>
22#include <LibWeb/HTML/Window.h>
23#include <LibWeb/Layout/Viewport.h>
24#include <LibWeb/Namespace.h>
25
26namespace Web::DOM {
27
28HashTable<Range*>& Range::live_ranges()
29{
30 static HashTable<Range*> ranges;
31 return ranges;
32}
33
34WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> Range::create(HTML::Window& window)
35{
36 return Range::create(window.associated_document());
37}
38
39WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> Range::create(Document& document)
40{
41 auto& realm = document.realm();
42 return MUST_OR_THROW_OOM(realm.heap().allocate<Range>(realm, document));
43}
44
45WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> Range::create(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
46{
47 auto& realm = start_container.realm();
48 return MUST_OR_THROW_OOM(realm.heap().allocate<Range>(realm, start_container, start_offset, end_container, end_offset));
49}
50
51WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> Range::construct_impl(JS::Realm& realm)
52{
53 auto& window = verify_cast<HTML::Window>(realm.global_object());
54 return Range::create(window);
55}
56
57Range::Range(Document& document)
58 : Range(document, 0, document, 0)
59{
60}
61
62Range::Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
63 : AbstractRange(start_container, start_offset, end_container, end_offset)
64{
65 live_ranges().set(this);
66}
67
68Range::~Range()
69{
70 live_ranges().remove(this);
71}
72
73JS::ThrowCompletionOr<void> Range::initialize(JS::Realm& realm)
74{
75 MUST_OR_THROW_OOM(Base::initialize(realm));
76 set_prototype(&Bindings::ensure_web_prototype<Bindings::RangePrototype>(realm, "Range"));
77
78 return {};
79}
80
81void Range::visit_edges(Cell::Visitor& visitor)
82{
83 Base::visit_edges(visitor);
84 visitor.visit(m_associated_selection);
85}
86
87void Range::set_associated_selection(Badge<Selection::Selection>, JS::GCPtr<Selection::Selection> selection)
88{
89 m_associated_selection = selection;
90 update_associated_selection();
91}
92
93void Range::update_associated_selection()
94{
95 if (!m_associated_selection)
96 return;
97 if (auto* layout_root = m_associated_selection->document()->layout_node()) {
98 layout_root->recompute_selection_states();
99 layout_root->set_needs_display();
100 }
101}
102
103// https://dom.spec.whatwg.org/#concept-range-root
104Node& Range::root()
105{
106 // The root of a live range is the root of its start node.
107 return m_start_container->root();
108}
109
110Node const& Range::root() const
111{
112 return m_start_container->root();
113}
114
115// https://dom.spec.whatwg.org/#concept-range-bp-position
116RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(Node const& node_a, u32 offset_a, Node const& node_b, u32 offset_b)
117{
118 // 1. Assert: nodeA and nodeB have the same root.
119 VERIFY(&node_a.root() == &node_b.root());
120
121 // 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.
122 if (&node_a == &node_b) {
123 if (offset_a == offset_b)
124 return RelativeBoundaryPointPosition::Equal;
125
126 if (offset_a < offset_b)
127 return RelativeBoundaryPointPosition::Before;
128
129 return RelativeBoundaryPointPosition::After;
130 }
131
132 // 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.
133 if (node_a.is_following(node_b)) {
134 auto relative_position = position_of_boundary_point_relative_to_other_boundary_point(node_b, offset_b, node_a, offset_a);
135
136 if (relative_position == RelativeBoundaryPointPosition::Before)
137 return RelativeBoundaryPointPosition::After;
138
139 if (relative_position == RelativeBoundaryPointPosition::After)
140 return RelativeBoundaryPointPosition::Before;
141 }
142
143 // 4. If nodeA is an ancestor of nodeB:
144 if (node_a.is_ancestor_of(node_b)) {
145 // 1. Let child be nodeB.
146 JS::NonnullGCPtr<Node const> child = node_b;
147
148 // 2. While child is not a child of nodeA, set child to its parent.
149 while (!node_a.is_parent_of(child)) {
150 auto* parent = child->parent();
151 VERIFY(parent);
152 child = *parent;
153 }
154
155 // 3. If child’s index is less than offsetA, then return after.
156 if (child->index() < offset_a)
157 return RelativeBoundaryPointPosition::After;
158 }
159
160 // 5. Return before.
161 return RelativeBoundaryPointPosition::Before;
162}
163
164WebIDL::ExceptionOr<void> Range::set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end)
165{
166 // To set the start or end of a range to a boundary point (node, offset), run these steps:
167
168 // 1. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
169 if (is<DocumentType>(node))
170 return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType.");
171
172 // 2. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
173 if (offset > node.length())
174 return WebIDL::IndexSizeError::create(realm(), DeprecatedString::formatted("Node does not contain a child at offset {}", offset));
175
176 // 3. Let bp be the boundary point (node, offset).
177
178 if (start_or_end == StartOrEnd::Start) {
179 // -> If these steps were invoked as "set the start"
180
181 // 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.
182 if (&root() != &node.root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset) == RelativeBoundaryPointPosition::After) {
183 m_end_container = node;
184 m_end_offset = offset;
185 }
186
187 // 2. Set range’s start to bp.
188 m_start_container = node;
189 m_start_offset = offset;
190 } else {
191 // -> If these steps were invoked as "set the end"
192 VERIFY(start_or_end == StartOrEnd::End);
193
194 // 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.
195 if (&root() != &node.root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset) == RelativeBoundaryPointPosition::Before) {
196 m_start_container = node;
197 m_start_offset = offset;
198 }
199
200 // 2. Set range’s end to bp.
201 m_end_container = node;
202 m_end_offset = offset;
203 }
204
205 update_associated_selection();
206 return {};
207}
208
209// https://dom.spec.whatwg.org/#concept-range-bp-set
210WebIDL::ExceptionOr<void> Range::set_start(Node& node, u32 offset)
211{
212 // The setStart(node, offset) method steps are to set the start of this to boundary point (node, offset).
213 return set_start_or_end(node, offset, StartOrEnd::Start);
214}
215
216WebIDL::ExceptionOr<void> Range::set_end(Node& node, u32 offset)
217{
218 // The setEnd(node, offset) method steps are to set the end of this to boundary point (node, offset).
219 return set_start_or_end(node, offset, StartOrEnd::End);
220}
221
222// https://dom.spec.whatwg.org/#dom-range-setstartbefore
223WebIDL::ExceptionOr<void> Range::set_start_before(Node& node)
224{
225 // 1. Let parent be node’s parent.
226 auto* parent = node.parent();
227
228 // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
229 if (!parent)
230 return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
231
232 // 3. Set the start of this to boundary point (parent, node’s index).
233 return set_start_or_end(*parent, node.index(), StartOrEnd::Start);
234}
235
236// https://dom.spec.whatwg.org/#dom-range-setstartafter
237WebIDL::ExceptionOr<void> Range::set_start_after(Node& node)
238{
239 // 1. Let parent be node’s parent.
240 auto* parent = node.parent();
241
242 // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
243 if (!parent)
244 return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
245
246 // 3. Set the start of this to boundary point (parent, node’s index plus 1).
247 return set_start_or_end(*parent, node.index() + 1, StartOrEnd::Start);
248}
249
250// https://dom.spec.whatwg.org/#dom-range-setendbefore
251WebIDL::ExceptionOr<void> Range::set_end_before(Node& node)
252{
253 // 1. Let parent be node’s parent.
254 auto* parent = node.parent();
255
256 // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
257 if (!parent)
258 return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
259
260 // 3. Set the end of this to boundary point (parent, node’s index).
261 return set_start_or_end(*parent, node.index(), StartOrEnd::End);
262}
263
264// https://dom.spec.whatwg.org/#dom-range-setendafter
265WebIDL::ExceptionOr<void> Range::set_end_after(Node& node)
266{
267 // 1. Let parent be node’s parent.
268 auto* parent = node.parent();
269
270 // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
271 if (!parent)
272 return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
273
274 // 3. Set the end of this to boundary point (parent, node’s index plus 1).
275 return set_start_or_end(*parent, node.index() + 1, StartOrEnd::End);
276}
277
278// https://dom.spec.whatwg.org/#dom-range-compareboundarypoints
279WebIDL::ExceptionOr<i16> Range::compare_boundary_points(u16 how, Range const& source_range) const
280{
281 // 1. If how is not one of
282 // - START_TO_START,
283 // - START_TO_END,
284 // - END_TO_END, and
285 // - END_TO_START,
286 // then throw a "NotSupportedError" DOMException.
287 if (how != HowToCompareBoundaryPoints::START_TO_START && how != HowToCompareBoundaryPoints::START_TO_END && how != HowToCompareBoundaryPoints::END_TO_END && how != HowToCompareBoundaryPoints::END_TO_START)
288 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));
289
290 // 2. If this’s root is not the same as sourceRange’s root, then throw a "WrongDocumentError" DOMException.
291 if (&root() != &source_range.root())
292 return WebIDL::WrongDocumentError::create(realm(), "This range is not in the same tree as the source range.");
293
294 JS::GCPtr<Node> this_point_node;
295 u32 this_point_offset = 0;
296
297 JS::GCPtr<Node> other_point_node;
298 u32 other_point_offset = 0;
299
300 // 3. If how is:
301 switch (how) {
302 case HowToCompareBoundaryPoints::START_TO_START:
303 // -> START_TO_START:
304 // Let this point be this’s start. Let other point be sourceRange’s start.
305 this_point_node = m_start_container;
306 this_point_offset = m_start_offset;
307
308 other_point_node = source_range.m_start_container;
309 other_point_offset = source_range.m_start_offset;
310 break;
311 case HowToCompareBoundaryPoints::START_TO_END:
312 // -> START_TO_END:
313 // Let this point be this’s end. Let other point be sourceRange’s start.
314 this_point_node = m_end_container;
315 this_point_offset = m_end_offset;
316
317 other_point_node = source_range.m_start_container;
318 other_point_offset = source_range.m_start_offset;
319 break;
320 case HowToCompareBoundaryPoints::END_TO_END:
321 // -> END_TO_END:
322 // Let this point be this’s end. Let other point be sourceRange’s end.
323 this_point_node = m_end_container;
324 this_point_offset = m_end_offset;
325
326 other_point_node = source_range.m_end_container;
327 other_point_offset = source_range.m_end_offset;
328 break;
329 case HowToCompareBoundaryPoints::END_TO_START:
330 // -> END_TO_START:
331 // Let this point be this’s start. Let other point be sourceRange’s end.
332 this_point_node = m_start_container;
333 this_point_offset = m_start_offset;
334
335 other_point_node = source_range.m_end_container;
336 other_point_offset = source_range.m_end_offset;
337 break;
338 default:
339 VERIFY_NOT_REACHED();
340 }
341
342 VERIFY(this_point_node);
343 VERIFY(other_point_node);
344
345 // 4. If the position of this point relative to other point is
346 auto relative_position = position_of_boundary_point_relative_to_other_boundary_point(*this_point_node, this_point_offset, *other_point_node, other_point_offset);
347 switch (relative_position) {
348 case RelativeBoundaryPointPosition::Before:
349 // -> before
350 // Return −1.
351 return -1;
352 case RelativeBoundaryPointPosition::Equal:
353 // -> equal
354 // Return 0.
355 return 0;
356 case RelativeBoundaryPointPosition::After:
357 // -> after
358 // Return 1.
359 return 1;
360 default:
361 VERIFY_NOT_REACHED();
362 }
363}
364
365// https://dom.spec.whatwg.org/#concept-range-select
366WebIDL::ExceptionOr<void> Range::select(Node& node)
367{
368 // 1. Let parent be node’s parent.
369 auto* parent = node.parent();
370
371 // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
372 if (!parent)
373 return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent.");
374
375 // 3. Let index be node’s index.
376 auto index = node.index();
377
378 // 4. Set range’s start to boundary point (parent, index).
379 m_start_container = *parent;
380 m_start_offset = index;
381
382 // 5. Set range’s end to boundary point (parent, index plus 1).
383 m_end_container = *parent;
384 m_end_offset = index + 1;
385
386 update_associated_selection();
387 return {};
388}
389
390// https://dom.spec.whatwg.org/#dom-range-selectnode
391WebIDL::ExceptionOr<void> Range::select_node(Node& node)
392{
393 // The selectNode(node) method steps are to select node within this.
394 return select(node);
395}
396
397// https://dom.spec.whatwg.org/#dom-range-collapse
398void Range::collapse(bool to_start)
399{
400 // The collapse(toStart) method steps are to, if toStart is true, set end to start; otherwise set start to end.
401 if (to_start) {
402 m_end_container = m_start_container;
403 m_end_offset = m_start_offset;
404 } else {
405 m_start_container = m_end_container;
406 m_start_offset = m_end_offset;
407 }
408 update_associated_selection();
409}
410
411// https://dom.spec.whatwg.org/#dom-range-selectnodecontents
412WebIDL::ExceptionOr<void> Range::select_node_contents(Node& node)
413{
414 // 1. If node is a doctype, throw an "InvalidNodeTypeError" DOMException.
415 if (is<DocumentType>(node))
416 return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType.");
417
418 // 2. Let length be the length of node.
419 auto length = node.length();
420
421 // 3. Set start to the boundary point (node, 0).
422 m_start_container = node;
423 m_start_offset = 0;
424
425 // 4. Set end to the boundary point (node, length).
426 m_end_container = node;
427 m_end_offset = length;
428
429 update_associated_selection();
430 return {};
431}
432
433JS::NonnullGCPtr<Range> Range::clone_range() const
434{
435 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();
436}
437
438JS::NonnullGCPtr<Range> Range::inverted() const
439{
440 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();
441}
442
443JS::NonnullGCPtr<Range> Range::normalized() const
444{
445 if (m_start_container.ptr() == m_end_container.ptr()) {
446 if (m_start_offset <= m_end_offset)
447 return clone_range();
448
449 return inverted();
450 }
451
452 if (m_start_container->is_before(m_end_container))
453 return clone_range();
454
455 return inverted();
456}
457
458// https://dom.spec.whatwg.org/#dom-range-commonancestorcontainer
459JS::NonnullGCPtr<Node> Range::common_ancestor_container() const
460{
461 // 1. Let container be start node.
462 auto container = m_start_container;
463
464 // 2. While container is not an inclusive ancestor of end node, let container be container’s parent.
465 while (!container->is_inclusive_ancestor_of(m_end_container)) {
466 VERIFY(container->parent());
467 container = *container->parent();
468 }
469
470 // 3. Return container.
471 return container;
472}
473
474// https://dom.spec.whatwg.org/#dom-range-intersectsnode
475bool Range::intersects_node(Node const& node) const
476{
477 // 1. If node’s root is different from this’s root, return false.
478 if (&node.root() != &root())
479 return false;
480
481 // 2. Let parent be node’s parent.
482 auto* parent = node.parent();
483
484 // 3. If parent is null, return true.
485 if (!parent)
486 return true;
487
488 // 4. Let offset be node’s index.
489 auto offset = node.index();
490
491 // 5. If (parent, offset) is before end and (parent, offset plus 1) is after start, return true
492 auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset, m_end_container, m_end_offset);
493 auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset + 1, m_start_container, m_start_offset);
494 if (relative_position_to_end == RelativeBoundaryPointPosition::Before && relative_position_to_start == RelativeBoundaryPointPosition::After)
495 return true;
496
497 // 6. Return false.
498 return false;
499}
500
501// https://dom.spec.whatwg.org/#dom-range-ispointinrange
502WebIDL::ExceptionOr<bool> Range::is_point_in_range(Node const& node, u32 offset) const
503{
504 // 1. If node’s root is different from this’s root, return false.
505 if (&node.root() != &root())
506 return false;
507
508 // 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
509 if (is<DocumentType>(node))
510 return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType.");
511
512 // 3. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
513 if (offset > node.length())
514 return WebIDL::IndexSizeError::create(realm(), DeprecatedString::formatted("Node does not contain a child at offset {}", offset));
515
516 // 4. If (node, offset) is before start or after end, return false.
517 auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset);
518 auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset);
519 if (relative_position_to_start == RelativeBoundaryPointPosition::Before || relative_position_to_end == RelativeBoundaryPointPosition::After)
520 return false;
521
522 // 5. Return true.
523 return true;
524}
525
526// https://dom.spec.whatwg.org/#dom-range-comparepoint
527WebIDL::ExceptionOr<i16> Range::compare_point(Node const& node, u32 offset) const
528{
529 // 1. If node’s root is different from this’s root, then throw a "WrongDocumentError" DOMException.
530 if (&node.root() != &root())
531 return WebIDL::WrongDocumentError::create(realm(), "Given node is not in the same document as the range.");
532
533 // 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
534 if (is<DocumentType>(node))
535 return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType.");
536
537 // 3. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
538 if (offset > node.length())
539 return WebIDL::IndexSizeError::create(realm(), DeprecatedString::formatted("Node does not contain a child at offset {}", offset));
540
541 // 4. If (node, offset) is before start, return −1.
542 auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset);
543 if (relative_position_to_start == RelativeBoundaryPointPosition::Before)
544 return -1;
545
546 // 5. If (node, offset) is after end, return 1.
547 auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset);
548 if (relative_position_to_end == RelativeBoundaryPointPosition::After)
549 return 1;
550
551 // 6. Return 0.
552 return 0;
553}
554
555// https://dom.spec.whatwg.org/#dom-range-stringifier
556DeprecatedString Range::to_deprecated_string() const
557{
558 // 1. Let s be the empty string.
559 StringBuilder builder;
560
561 // 2. If this’s start node is this’s end node and it is a Text node,
562 // then return the substring of that Text node’s data beginning at this’s start offset and ending at this’s end offset.
563 if (start_container() == end_container() && is<Text>(*start_container()))
564 return static_cast<Text const&>(*start_container()).data().substring(start_offset(), end_offset() - start_offset());
565
566 // 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.
567 if (is<Text>(*start_container()))
568 builder.append(static_cast<Text const&>(*start_container()).data().substring_view(start_offset()));
569
570 // 4. Append the concatenation of the data of all Text nodes that are contained in this, in tree order, to s.
571 for (Node const* node = start_container(); node != end_container()->next_sibling(); node = node->next_in_pre_order()) {
572 if (is<Text>(*node) && contains_node(*node))
573 builder.append(static_cast<Text const&>(*node).data());
574 }
575
576 // 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.
577 if (is<Text>(*end_container()))
578 builder.append(static_cast<Text const&>(*end_container()).data().substring_view(0, end_offset()));
579
580 // 6. Return s.
581 return builder.to_deprecated_string();
582}
583
584// https://dom.spec.whatwg.org/#dom-range-extractcontents
585WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::extract_contents()
586{
587 return extract();
588}
589
590// https://dom.spec.whatwg.org/#concept-range-extract
591WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::extract()
592{
593 // 1. Let fragment be a new DocumentFragment node whose node document is range’s start node’s node document.
594 auto fragment = MUST_OR_THROW_OOM(heap().allocate<DOM::DocumentFragment>(realm(), const_cast<Document&>(start_container()->document())));
595
596 // 2. If range is collapsed, then return fragment.
597 if (collapsed())
598 return fragment;
599
600 // 3. Let original start node, original start offset, original end node, and original end offset
601 // be range’s start node, start offset, end node, and end offset, respectively.
602 JS::NonnullGCPtr<Node> original_start_node = m_start_container;
603 auto original_start_offset = m_start_offset;
604 JS::NonnullGCPtr<Node> original_end_node = m_end_container;
605 auto original_end_offset = m_end_offset;
606
607 // 4. If original start node is original end node and it is a CharacterData node, then:
608 if (original_start_node.ptr() == original_end_node.ptr() && is<CharacterData>(*original_start_node)) {
609 // 1. Let clone be a clone of original start node.
610 auto clone = original_start_node->clone_node();
611
612 // 2. Set the data of clone to the result of substringing data with node original start node,
613 // offset original start offset, and count original end offset minus original start offset.
614 auto result = TRY(static_cast<CharacterData const&>(*original_start_node).substring_data(original_start_offset, original_end_offset - original_start_offset));
615 verify_cast<CharacterData>(*clone).set_data(move(result));
616
617 // 3. Append clone to fragment.
618 TRY(fragment->append_child(clone));
619
620 // 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.
621 TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_end_offset - original_start_offset, ""));
622
623 // 5. Return fragment.
624 return fragment;
625 }
626
627 // 5. Let common ancestor be original start node.
628 JS::NonnullGCPtr<Node> common_ancestor = original_start_node;
629
630 // 6. While common ancestor is not an inclusive ancestor of original end node, set common ancestor to its own parent.
631 while (!common_ancestor->is_inclusive_ancestor_of(original_end_node))
632 common_ancestor = *common_ancestor->parent_node();
633
634 // 7. Let first partially contained child be null.
635 JS::GCPtr<Node> first_partially_contained_child;
636
637 // 8. If original start node is not an inclusive ancestor of original end node,
638 // set first partially contained child to the first child of common ancestor that is partially contained in range.
639 if (!original_start_node->is_inclusive_ancestor_of(original_end_node)) {
640 for (auto* child = common_ancestor->first_child(); child; child = child->next_sibling()) {
641 if (partially_contains_node(*child)) {
642 first_partially_contained_child = child;
643 break;
644 }
645 }
646 }
647
648 // 9. Let last partially contained child be null.
649 JS::GCPtr<Node> last_partially_contained_child;
650
651 // 10. If original end node is not an inclusive ancestor of original start node,
652 // set last partially contained child to the last child of common ancestor that is partially contained in range.
653 if (!original_end_node->is_inclusive_ancestor_of(original_start_node)) {
654 for (auto* child = common_ancestor->last_child(); child; child = child->previous_sibling()) {
655 if (partially_contains_node(*child)) {
656 last_partially_contained_child = child;
657 break;
658 }
659 }
660 }
661
662 // 11. Let contained children be a list of all children of common ancestor that are contained in range, in tree order.
663 Vector<JS::NonnullGCPtr<Node>> contained_children;
664 for (Node* node = common_ancestor->first_child(); node; node = node->next_sibling()) {
665 if (contains_node(*node))
666 contained_children.append(*node);
667 }
668
669 // 12. If any member of contained children is a doctype, then throw a "HierarchyRequestError" DOMException.
670 for (auto const& child : contained_children) {
671 if (is<DocumentType>(*child))
672 return WebIDL::HierarchyRequestError::create(realm(), "Contained child is a DocumentType");
673 }
674
675 JS::GCPtr<Node> new_node;
676 size_t new_offset = 0;
677
678 // 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.
679 if (original_start_node->is_inclusive_ancestor_of(original_end_node)) {
680 new_node = original_start_node;
681 new_offset = original_start_offset;
682 }
683 // 14. Otherwise:
684 else {
685 // 1. Let reference node equal original start node.
686 JS::GCPtr<Node> reference_node = original_start_node;
687
688 // 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.
689 while (reference_node->parent_node() && !reference_node->parent_node()->is_inclusive_ancestor_of(original_end_node))
690 reference_node = reference_node->parent_node();
691
692 // 3. Set new node to the parent of reference node, and new offset to one plus reference node’s index.
693 new_node = reference_node->parent_node();
694 new_offset = 1 + reference_node->index();
695 }
696
697 // 15. If first partially contained child is a CharacterData node, then:
698 if (first_partially_contained_child && is<CharacterData>(*first_partially_contained_child)) {
699 // 1. Let clone be a clone of original start node.
700 auto clone = original_start_node->clone_node();
701
702 // 2. Set the data of clone to the result of substringing data with node original start node, offset original start offset,
703 // and count original start node’s length minus original start offset.
704 auto result = TRY(static_cast<CharacterData const&>(*original_start_node).substring_data(original_start_offset, original_start_node->length() - original_start_offset));
705 verify_cast<CharacterData>(*clone).set_data(move(result));
706
707 // 3. Append clone to fragment.
708 TRY(fragment->append_child(clone));
709
710 // 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.
711 TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_start_node->length() - original_start_offset, ""));
712 }
713 // 16. Otherwise, if first partially contained child is not null:
714 else if (first_partially_contained_child) {
715 // 1. Let clone be a clone of first partially contained child.
716 auto clone = first_partially_contained_child->clone_node();
717
718 // 2. Append clone to fragment.
719 TRY(fragment->append_child(clone));
720
721 // 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).
722 auto subrange = TRY(Range::create(original_start_node, original_start_offset, *first_partially_contained_child, first_partially_contained_child->length()));
723
724 // 4. Let subfragment be the result of extracting subrange.
725 auto subfragment = TRY(subrange->extract());
726
727 // 5. Append subfragment to clone.
728 TRY(clone->append_child(subfragment));
729 }
730
731 // 17. For each contained child in contained children, append contained child to fragment.
732 for (auto& contained_child : contained_children) {
733 TRY(fragment->append_child(contained_child));
734 }
735
736 // 18. If last partially contained child is a CharacterData node, then:
737 if (last_partially_contained_child && is<CharacterData>(*last_partially_contained_child)) {
738 // 1. Let clone be a clone of original end node.
739 auto clone = original_end_node->clone_node();
740
741 // 2. Set the data of clone to the result of substringing data with node original end node, offset 0, and count original end offset.
742 auto result = TRY(static_cast<CharacterData const&>(*original_end_node).substring_data(0, original_end_offset));
743 verify_cast<CharacterData>(*clone).set_data(move(result));
744
745 // 3. Append clone to fragment.
746 TRY(fragment->append_child(clone));
747
748 // 4. Replace data with node original end node, offset 0, count original end offset, and data the empty string.
749 TRY(verify_cast<CharacterData>(*original_end_node).replace_data(0, original_end_offset, ""));
750 }
751 // 19. Otherwise, if last partially contained child is not null:
752 else if (last_partially_contained_child) {
753 // 1. Let clone be a clone of last partially contained child.
754 auto clone = last_partially_contained_child->clone_node();
755
756 // 2. Append clone to fragment.
757 TRY(fragment->append_child(clone));
758
759 // 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).
760 auto subrange = TRY(Range::create(*last_partially_contained_child, 0, original_end_node, original_end_offset));
761
762 // 4. Let subfragment be the result of extracting subrange.
763 auto subfragment = TRY(subrange->extract());
764
765 // 5. Append subfragment to clone.
766 TRY(clone->append_child(subfragment));
767 }
768
769 // 20. Set range’s start and end to (new node, new offset).
770 TRY(set_start(*new_node, new_offset));
771 TRY(set_end(*new_node, new_offset));
772
773 // 21. Return fragment.
774 return fragment;
775}
776
777// https://dom.spec.whatwg.org/#contained
778bool Range::contains_node(Node const& node) const
779{
780 // A node node is contained in a live range range if node’s root is range’s root,
781 if (&node.root() != &root())
782 return false;
783
784 // and (node, 0) is after range’s start,
785 if (position_of_boundary_point_relative_to_other_boundary_point(node, 0, m_start_container, m_start_offset) != RelativeBoundaryPointPosition::After)
786 return false;
787
788 // and (node, node’s length) is before range’s end.
789 if (position_of_boundary_point_relative_to_other_boundary_point(node, node.length(), m_end_container, m_end_offset) != RelativeBoundaryPointPosition::Before)
790 return false;
791
792 return true;
793}
794
795// https://dom.spec.whatwg.org/#partially-contained
796bool Range::partially_contains_node(Node const& node) const
797{
798 // 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.
799 if (node.is_inclusive_ancestor_of(m_start_container) && &node != m_end_container.ptr())
800 return true;
801 if (node.is_inclusive_ancestor_of(m_end_container) && &node != m_start_container.ptr())
802 return true;
803 return false;
804}
805
806// https://dom.spec.whatwg.org/#dom-range-insertnode
807WebIDL::ExceptionOr<void> Range::insert_node(JS::NonnullGCPtr<Node> node)
808{
809 return insert(node);
810}
811
812// https://dom.spec.whatwg.org/#concept-range-insert
813WebIDL::ExceptionOr<void> Range::insert(JS::NonnullGCPtr<Node> node)
814{
815 // 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.
816 if ((is<ProcessingInstruction>(*m_start_container) || is<Comment>(*m_start_container))
817 || (is<Text>(*m_start_container) && !m_start_container->parent_node())
818 || m_start_container.ptr() == node.ptr()) {
819 return WebIDL::HierarchyRequestError::create(realm(), "Range has inappropriate start node for insertion");
820 }
821
822 // 2. Let referenceNode be null.
823 JS::GCPtr<Node> reference_node;
824
825 // 3. If range’s start node is a Text node, set referenceNode to that Text node.
826 if (is<Text>(*m_start_container)) {
827 reference_node = m_start_container;
828 }
829 // 4. Otherwise, set referenceNode to the child of start node whose index is start offset, and null if there is no such child.
830 else {
831 reference_node = m_start_container->child_at_index(m_start_offset);
832 }
833
834 // 5. Let parent be range’s start node if referenceNode is null, and referenceNode’s parent otherwise.
835 JS::GCPtr<Node> parent;
836 if (!reference_node)
837 parent = m_start_container;
838 else
839 parent = reference_node->parent();
840
841 // 6. Ensure pre-insertion validity of node into parent before referenceNode.
842 TRY(parent->ensure_pre_insertion_validity(node, reference_node));
843
844 // 7. If range’s start node is a Text node, set referenceNode to the result of splitting it with offset range’s start offset.
845 if (is<Text>(*m_start_container))
846 reference_node = TRY(static_cast<Text&>(*m_start_container).split_text(m_start_offset));
847
848 // 8. If node is referenceNode, set referenceNode to its next sibling.
849 if (node == reference_node)
850 reference_node = reference_node->next_sibling();
851
852 // 9. If node’s parent is non-null, then remove node.
853 if (node->parent())
854 node->remove();
855
856 // 10. Let newOffset be parent’s length if referenceNode is null, and referenceNode’s index otherwise.
857 size_t new_offset = 0;
858 if (!reference_node)
859 new_offset = parent->length();
860 else
861 new_offset = reference_node->index();
862
863 // 11. Increase newOffset by node’s length if node is a DocumentFragment node, and one otherwise.
864 if (is<DocumentFragment>(*node))
865 new_offset += node->length();
866 else
867 new_offset += 1;
868
869 // 12. Pre-insert node into parent before referenceNode.
870 (void)TRY(parent->pre_insert(node, reference_node));
871
872 // 13. If range is collapsed, then set range’s end to (parent, newOffset).
873 if (collapsed())
874 TRY(set_end(*parent, new_offset));
875
876 return {};
877}
878
879// https://dom.spec.whatwg.org/#dom-range-surroundcontents
880WebIDL::ExceptionOr<void> Range::surround_contents(JS::NonnullGCPtr<Node> new_parent)
881{
882 // 1. If a non-Text node is partially contained in this, then throw an "InvalidStateError" DOMException.
883 Node* start_non_text_node = start_container();
884 if (is<Text>(*start_non_text_node))
885 start_non_text_node = start_non_text_node->parent_node();
886 Node* end_non_text_node = end_container();
887 if (is<Text>(*end_non_text_node))
888 end_non_text_node = end_non_text_node->parent_node();
889 if (start_non_text_node != end_non_text_node)
890 return WebIDL::InvalidStateError::create(realm(), "Non-Text node is partially contained in range.");
891
892 // 2. If newParent is a Document, DocumentType, or DocumentFragment node, then throw an "InvalidNodeTypeError" DOMException.
893 if (is<Document>(*new_parent) || is<DocumentType>(*new_parent) || is<DocumentFragment>(*new_parent))
894 return WebIDL::InvalidNodeTypeError::create(realm(), "Invalid parent node type");
895
896 // 3. Let fragment be the result of extracting this.
897 auto fragment = TRY(extract());
898
899 // 4. If newParent has children, then replace all with null within newParent.
900 if (new_parent->has_children())
901 new_parent->replace_all(nullptr);
902
903 // 5. Insert newParent into this.
904 TRY(insert(new_parent));
905
906 // 6. Append fragment to newParent.
907 (void)TRY(new_parent->append_child(fragment));
908
909 // 7. Select newParent within this.
910 return select(*new_parent);
911}
912
913// https://dom.spec.whatwg.org/#dom-range-clonecontents
914WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::clone_contents()
915{
916 return clone_the_contents();
917}
918
919// https://dom.spec.whatwg.org/#concept-range-clone
920WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::clone_the_contents()
921{
922 // 1. Let fragment be a new DocumentFragment node whose node document is range’s start node’s node document.
923 auto fragment = MUST_OR_THROW_OOM(heap().allocate<DOM::DocumentFragment>(realm(), const_cast<Document&>(start_container()->document())));
924
925 // 2. If range is collapsed, then return fragment.
926 if (collapsed())
927 return fragment;
928
929 // 3. Let original start node, original start offset, original end node, and original end offset
930 // be range’s start node, start offset, end node, and end offset, respectively.
931 JS::NonnullGCPtr<Node> original_start_node = m_start_container;
932 auto original_start_offset = m_start_offset;
933 JS::NonnullGCPtr<Node> original_end_node = m_end_container;
934 auto original_end_offset = m_end_offset;
935
936 // 4. If original start node is original end node and it is a CharacterData node, then:
937 if (original_start_node.ptr() == original_end_node.ptr() && is<CharacterData>(*original_start_node)) {
938 // 1. Let clone be a clone of original start node.
939 auto clone = original_start_node->clone_node();
940
941 // 2. Set the data of clone to the result of substringing data with node original start node,
942 // offset original start offset, and count original end offset minus original start offset.
943 auto result = TRY(static_cast<CharacterData const&>(*original_start_node).substring_data(original_start_offset, original_end_offset - original_start_offset));
944 verify_cast<CharacterData>(*clone).set_data(move(result));
945
946 // 3. Append clone to fragment.
947 TRY(fragment->append_child(clone));
948
949 // 4. Return fragment.
950 return fragment;
951 }
952
953 // 5. Let common ancestor be original start node.
954 JS::NonnullGCPtr<Node> common_ancestor = original_start_node;
955
956 // 6. While common ancestor is not an inclusive ancestor of original end node, set common ancestor to its own parent.
957 while (!common_ancestor->is_inclusive_ancestor_of(original_end_node))
958 common_ancestor = *common_ancestor->parent_node();
959
960 // 7. Let first partially contained child be null.
961 JS::GCPtr<Node> first_partially_contained_child;
962
963 // 8. If original start node is not an inclusive ancestor of original end node,
964 // set first partially contained child to the first child of common ancestor that is partially contained in range.
965 if (!original_start_node->is_inclusive_ancestor_of(original_end_node)) {
966 for (auto* child = common_ancestor->first_child(); child; child = child->next_sibling()) {
967 if (partially_contains_node(*child)) {
968 first_partially_contained_child = child;
969 break;
970 }
971 }
972 }
973
974 // 9. Let last partially contained child be null.
975 JS::GCPtr<Node> last_partially_contained_child;
976
977 // 10. If original end node is not an inclusive ancestor of original start node,
978 // set last partially contained child to the last child of common ancestor that is partially contained in range.
979 if (!original_end_node->is_inclusive_ancestor_of(original_start_node)) {
980 for (auto* child = common_ancestor->last_child(); child; child = child->previous_sibling()) {
981 if (partially_contains_node(*child)) {
982 last_partially_contained_child = child;
983 break;
984 }
985 }
986 }
987
988 // 11. Let contained children be a list of all children of common ancestor that are contained in range, in tree order.
989 Vector<JS::NonnullGCPtr<Node>> contained_children;
990 for (Node* node = common_ancestor->first_child(); node; node = node->next_sibling()) {
991 if (contains_node(*node))
992 contained_children.append(*node);
993 }
994
995 // 12. If any member of contained children is a doctype, then throw a "HierarchyRequestError" DOMException.
996 for (auto const& child : contained_children) {
997 if (is<DocumentType>(*child))
998 return WebIDL::HierarchyRequestError::create(realm(), "Contained child is a DocumentType");
999 }
1000
1001 // 13. If first partially contained child is a CharacterData node, then:
1002 if (first_partially_contained_child && is<CharacterData>(*first_partially_contained_child)) {
1003 // 1. Let clone be a clone of original start node.
1004 auto clone = original_start_node->clone_node();
1005
1006 // 2. Set the data of clone to the result of substringing data with node original start node, offset original start offset,
1007 // and count original start node’s length minus original start offset.
1008 auto result = TRY(static_cast<CharacterData const&>(*original_start_node).substring_data(original_start_offset, original_start_node->length() - original_start_offset));
1009 verify_cast<CharacterData>(*clone).set_data(move(result));
1010
1011 // 3. Append clone to fragment.
1012 TRY(fragment->append_child(clone));
1013 }
1014 // 14. Otherwise, if first partially contained child is not null:
1015 else if (first_partially_contained_child) {
1016 // 1. Let clone be a clone of first partially contained child.
1017 auto clone = first_partially_contained_child->clone_node();
1018
1019 // 2. Append clone to fragment.
1020 TRY(fragment->append_child(clone));
1021
1022 // 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).
1023 auto subrange = TRY(Range::create(original_start_node, original_start_offset, *first_partially_contained_child, first_partially_contained_child->length()));
1024
1025 // 4. Let subfragment be the result of cloning the contents of subrange.
1026 auto subfragment = TRY(subrange->clone_the_contents());
1027
1028 // 5. Append subfragment to clone.
1029 TRY(clone->append_child(subfragment));
1030 }
1031
1032 // 15. For each contained child in contained children.
1033 for (auto& contained_child : contained_children) {
1034 // 1. Let clone be a clone of contained child with the clone children flag set.
1035 auto clone = contained_child->clone_node(nullptr, true);
1036
1037 // 2. Append clone to fragment.
1038 TRY(fragment->append_child(move(clone)));
1039 }
1040
1041 // 16. If last partially contained child is a CharacterData node, then:
1042 if (last_partially_contained_child && is<CharacterData>(*last_partially_contained_child)) {
1043 // 1. Let clone be a clone of original end node.
1044 auto clone = original_end_node->clone_node();
1045
1046 // 2. Set the data of clone to the result of substringing data with node original end node, offset 0, and count original end offset.
1047 auto result = TRY(static_cast<CharacterData const&>(*original_end_node).substring_data(0, original_end_offset));
1048 verify_cast<CharacterData>(*clone).set_data(move(result));
1049
1050 // 3. Append clone to fragment.
1051 TRY(fragment->append_child(clone));
1052 }
1053 // 17. Otherwise, if last partially contained child is not null:
1054 else if (last_partially_contained_child) {
1055 // 1. Let clone be a clone of last partially contained child.
1056 auto clone = last_partially_contained_child->clone_node();
1057
1058 // 2. Append clone to fragment.
1059 TRY(fragment->append_child(clone));
1060
1061 // 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).
1062 auto subrange = TRY(Range::create(*last_partially_contained_child, 0, original_end_node, original_end_offset));
1063
1064 // 4. Let subfragment be the result of cloning the contents of subrange.
1065 auto subfragment = TRY(subrange->clone_the_contents());
1066
1067 // 5. Append subfragment to clone.
1068 TRY(clone->append_child(subfragment));
1069 }
1070
1071 // 18. Return fragment.
1072 return fragment;
1073}
1074
1075// https://dom.spec.whatwg.org/#dom-range-deletecontents
1076WebIDL::ExceptionOr<void> Range::delete_contents()
1077{
1078 // 1. If this is collapsed, then return.
1079 if (collapsed())
1080 return {};
1081
1082 // 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.
1083 JS::NonnullGCPtr<Node> original_start_node = m_start_container;
1084 auto original_start_offset = m_start_offset;
1085 JS::NonnullGCPtr<Node> original_end_node = m_end_container;
1086 auto original_end_offset = m_end_offset;
1087
1088 // 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,
1089 // count original end offset minus original start offset, and data the empty string, and then return.
1090 if (original_start_node.ptr() == original_end_node.ptr() && is<CharacterData>(*original_start_node)) {
1091 TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_end_offset - original_start_offset, ""));
1092 return {};
1093 }
1094
1095 // 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.
1096 JS::MarkedVector<Node*> nodes_to_remove(heap());
1097 for (Node const* node = start_container(); node != end_container()->next_in_pre_order(); node = node->next_in_pre_order()) {
1098 if (contains_node(*node) && (!node->parent_node() || !contains_node(*node->parent_node())))
1099 nodes_to_remove.append(const_cast<Node*>(node));
1100 }
1101
1102 JS::GCPtr<Node> new_node;
1103 size_t new_offset = 0;
1104
1105 // 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.
1106 if (original_start_node->is_inclusive_ancestor_of(original_end_node)) {
1107 new_node = original_start_node;
1108 new_offset = original_start_offset;
1109 }
1110 // 6. Otherwise
1111 else {
1112 // 1. Let reference node equal original start node.
1113 auto reference_node = original_start_node;
1114
1115 // 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.
1116 while (reference_node->parent_node() && !reference_node->parent_node()->is_inclusive_ancestor_of(original_end_node))
1117 reference_node = *reference_node->parent_node();
1118
1119 // 3. Set new node to the parent of reference node, and new offset to one plus the index of reference node.
1120 new_node = reference_node->parent_node();
1121 new_offset = 1 + reference_node->index();
1122 }
1123
1124 // 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.
1125 if (is<CharacterData>(*original_start_node))
1126 TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_start_node->length() - original_start_offset, ""));
1127
1128 // 8. For each node in nodes to remove, in tree order, remove node.
1129 for (auto& node : nodes_to_remove)
1130 node->remove();
1131
1132 // 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.
1133 if (is<CharacterData>(*original_end_node))
1134 TRY(static_cast<CharacterData&>(*original_end_node).replace_data(0, original_end_offset, ""));
1135
1136 // 10. Set start and end to (new node, new offset).
1137 TRY(set_start(*new_node, new_offset));
1138 TRY(set_end(*new_node, new_offset));
1139 return {};
1140}
1141
1142// https://w3c.github.io/csswg-drafts/cssom-view/#dom-range-getboundingclientrect
1143JS::NonnullGCPtr<Geometry::DOMRect> Range::get_bounding_client_rect() const
1144{
1145 dbgln("(STUBBED) Range::get_bounding_client_rect()");
1146 return Geometry::DOMRect::construct_impl(realm(), 0, 0, 0, 0).release_value_but_fixme_should_propagate_errors();
1147}
1148
1149// https://w3c.github.io/DOM-Parsing/#dom-range-createcontextualfragment
1150WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::create_contextual_fragment(DeprecatedString const& fragment)
1151{
1152 // 1. Let node be the context object's start node.
1153 JS::NonnullGCPtr<Node> node = *start_container();
1154
1155 // Let element be as follows, depending on node's interface:
1156 JS::GCPtr<Element> element;
1157 switch (static_cast<NodeType>(node->node_type())) {
1158 case NodeType::DOCUMENT_NODE:
1159 case NodeType::DOCUMENT_FRAGMENT_NODE:
1160 element = nullptr;
1161 break;
1162 case NodeType::ELEMENT_NODE:
1163 element = static_cast<DOM::Element&>(*node);
1164 break;
1165 case NodeType::TEXT_NODE:
1166 case NodeType::COMMENT_NODE:
1167 element = node->parent_element();
1168 break;
1169 case NodeType::DOCUMENT_TYPE_NODE:
1170 case NodeType::PROCESSING_INSTRUCTION_NODE:
1171 // [DOM4] prevents this case.
1172 VERIFY_NOT_REACHED();
1173 default:
1174 VERIFY_NOT_REACHED();
1175 }
1176
1177 // 2. If either element is null or the following are all true:
1178 // - element's node document is an HTML document,
1179 // - element's local name is "html", and
1180 // - element's namespace is the HTML namespace;
1181 if (!element || is<HTML::HTMLHtmlElement>(*element)) {
1182 // let element be a new Element with
1183 // - "body" as its local name,
1184 // - The HTML namespace as its namespace, and
1185 // - The context object's node document as its node document.
1186 element = TRY(DOM::create_element(node->document(), "body"sv, Namespace::HTML));
1187 }
1188
1189 // 3. Let fragment node be the result of invoking the fragment parsing algorithm with fragment as markup, and element as the context element.
1190 auto fragment_node = TRY(DOMParsing::parse_fragment(fragment, *element));
1191
1192 // 4. Unmark all scripts in fragment node as "already started" and as "parser-inserted".
1193 fragment_node->for_each_in_subtree_of_type<HTML::HTMLScriptElement>([&](HTML::HTMLScriptElement& script_element) {
1194 script_element.unmark_as_already_started({});
1195 script_element.unmark_as_parser_inserted({});
1196 return IterationDecision::Continue;
1197 });
1198
1199 // 5. Return the value of fragment node.
1200 return fragment_node;
1201}
1202
1203}