summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM/Range.h
blob: 57a784de2681c4f877b6c0ec84228fe0d67863ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2020, the SerenityOS developers.
 * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibWeb/DOM/AbstractRange.h>

namespace Web::DOM {

class Range final : public AbstractRange {
public:
    using WrapperType = Bindings::RangeWrapper;

    virtual ~Range() override;

    static NonnullRefPtr<Range> create(Document&);
    static NonnullRefPtr<Range> create(HTML::Window&);
    static NonnullRefPtr<Range> create(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
    static NonnullRefPtr<Range> create_with_global_object(Bindings::WindowObject&);

    // FIXME: There are a ton of methods missing here.

    ExceptionOr<void> set_start(Node& node, u32 offset);
    ExceptionOr<void> set_end(Node& node, u32 offset);
    ExceptionOr<void> set_start_before(Node& node);
    ExceptionOr<void> set_start_after(Node& node);
    ExceptionOr<void> set_end_before(Node& node);
    ExceptionOr<void> set_end_after(Node& node);
    ExceptionOr<void> select_node(Node& node);
    void collapse(bool to_start);
    ExceptionOr<void> select_node_contents(Node const&);

    // https://dom.spec.whatwg.org/#dom-range-start_to_start
    enum HowToCompareBoundaryPoints : u16 {
        START_TO_START = 0,
        START_TO_END = 1,
        END_TO_END = 2,
        END_TO_START = 3,
    };

    ExceptionOr<i16> compare_boundary_points(u16 how, Range const& source_range) const;

    NonnullRefPtr<Range> inverted() const;
    NonnullRefPtr<Range> normalized() const;
    NonnullRefPtr<Range> clone_range() const;

    NonnullRefPtr<Node> common_ancestor_container() const;

    // https://dom.spec.whatwg.org/#dom-range-detach
    void detach() const
    {
        // The detach() method steps are to do nothing.
        // Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility.
    }

    bool intersects_node(Node const&) const;
    ExceptionOr<bool> is_point_in_range(Node const&, u32 offset) const;
    ExceptionOr<i16> compare_point(Node const&, u32 offset) const;

    ExceptionOr<void> delete_contents();
    ExceptionOr<NonnullRefPtr<DocumentFragment>> extract_contents();
    ExceptionOr<NonnullRefPtr<DocumentFragment>> clone_contents();

    ExceptionOr<void> insert_node(NonnullRefPtr<Node>);
    ExceptionOr<void> surround_contents(NonnullRefPtr<Node> new_parent);

    String to_string() const;

    static HashTable<Range*>& live_ranges();

private:
    explicit Range(Document&);

    Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);

    Node& root();
    Node const& root() const;

    enum class StartOrEnd {
        Start,
        End,
    };

    ExceptionOr<void> set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end);
    ExceptionOr<void> select(Node& node);

    ExceptionOr<NonnullRefPtr<DocumentFragment>> extract();
    ExceptionOr<NonnullRefPtr<DocumentFragment>> clone_the_contents();
    ExceptionOr<void> insert(NonnullRefPtr<Node>);

    bool contains_node(Node const&) const;
    bool partially_contains_node(Node const&) const;
};

}