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#pragma once
10
11#include <LibWeb/DOM/AbstractRange.h>
12#include <LibWeb/Selection/Selection.h>
13
14namespace Web::DOM {
15
16enum class RelativeBoundaryPointPosition {
17 Equal,
18 Before,
19 After,
20};
21
22// https://dom.spec.whatwg.org/#concept-range-bp-position
23RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(Node const& node_a, u32 offset_a, Node const& node_b, u32 offset_b);
24
25class Range final : public AbstractRange {
26 WEB_PLATFORM_OBJECT(Range, AbstractRange);
27
28public:
29 static WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> create(Document&);
30 static WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> create(HTML::Window&);
31 static WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> create(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
32 static WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> construct_impl(JS::Realm&);
33
34 virtual ~Range() override;
35
36 // FIXME: There are a ton of methods missing here.
37
38 WebIDL::ExceptionOr<void> set_start(Node& node, u32 offset);
39 WebIDL::ExceptionOr<void> set_end(Node& node, u32 offset);
40 WebIDL::ExceptionOr<void> set_start_before(Node& node);
41 WebIDL::ExceptionOr<void> set_start_after(Node& node);
42 WebIDL::ExceptionOr<void> set_end_before(Node& node);
43 WebIDL::ExceptionOr<void> set_end_after(Node& node);
44 WebIDL::ExceptionOr<void> select_node(Node& node);
45 void collapse(bool to_start);
46 WebIDL::ExceptionOr<void> select_node_contents(Node&);
47
48 // https://dom.spec.whatwg.org/#dom-range-start_to_start
49 enum HowToCompareBoundaryPoints : u16 {
50 START_TO_START = 0,
51 START_TO_END = 1,
52 END_TO_END = 2,
53 END_TO_START = 3,
54 };
55
56 WebIDL::ExceptionOr<i16> compare_boundary_points(u16 how, Range const& source_range) const;
57
58 JS::NonnullGCPtr<Range> inverted() const;
59 JS::NonnullGCPtr<Range> normalized() const;
60 JS::NonnullGCPtr<Range> clone_range() const;
61
62 JS::NonnullGCPtr<Node> common_ancestor_container() const;
63
64 // https://dom.spec.whatwg.org/#dom-range-detach
65 void detach() const
66 {
67 // The detach() method steps are to do nothing.
68 // Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility.
69 }
70
71 bool intersects_node(Node const&) const;
72 WebIDL::ExceptionOr<bool> is_point_in_range(Node const&, u32 offset) const;
73 WebIDL::ExceptionOr<i16> compare_point(Node const&, u32 offset) const;
74
75 WebIDL::ExceptionOr<void> delete_contents();
76 WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> extract_contents();
77 WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> clone_contents();
78
79 WebIDL::ExceptionOr<void> insert_node(JS::NonnullGCPtr<Node>);
80 WebIDL::ExceptionOr<void> surround_contents(JS::NonnullGCPtr<Node> new_parent);
81
82 DeprecatedString to_deprecated_string() const;
83
84 static HashTable<Range*>& live_ranges();
85
86 JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
87
88 bool contains_node(Node const&) const;
89
90 void set_associated_selection(Badge<Selection::Selection>, JS::GCPtr<Selection::Selection>);
91
92 WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> create_contextual_fragment(DeprecatedString const& fragment);
93
94private:
95 explicit Range(Document&);
96 Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
97
98 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
99 virtual void visit_edges(Cell::Visitor&) override;
100
101 Node& root();
102 Node const& root() const;
103
104 void update_associated_selection();
105
106 enum class StartOrEnd {
107 Start,
108 End,
109 };
110
111 WebIDL::ExceptionOr<void> set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end);
112 WebIDL::ExceptionOr<void> select(Node& node);
113
114 WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> extract();
115 WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> clone_the_contents();
116 WebIDL::ExceptionOr<void> insert(JS::NonnullGCPtr<Node>);
117
118 bool partially_contains_node(Node const&) const;
119
120 JS::GCPtr<Selection::Selection> m_associated_selection;
121};
122
123}