summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM/MutationObserver.h
blob: e7dcf942eeeead47b031af11383b333a1ba45ae9 (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
100
101
102
103
104
105
106
107
108
/*
 * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/NonnullRefPtrVector.h>
#include <AK/RefCounted.h>
#include <LibJS/Heap/Handle.h>
#include <LibWeb/Bindings/CallbackType.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/DOM/MutationRecord.h>

namespace Web::DOM {

// https://dom.spec.whatwg.org/#dictdef-mutationobserverinit
struct MutationObserverInit {
    bool child_list { false };
    Optional<bool> attributes;
    Optional<bool> character_data;
    bool subtree { false };
    Optional<bool> attribute_old_value;
    Optional<bool> character_data_old_value;
    Optional<Vector<String>> attribute_filter;
};

// https://dom.spec.whatwg.org/#mutationobserver
class MutationObserver final
    : public RefCounted<MutationObserver>
    , public Bindings::Wrappable {
public:
    using WrapperType = Bindings::MutationObserverWrapper;

    static NonnullRefPtr<MutationObserver> create_with_global_object(Bindings::WindowObject& window_object, Bindings::CallbackType callback)
    {
        return adopt_ref(*new MutationObserver(window_object, move(callback)));
    }

    virtual ~MutationObserver() override = default;

    ExceptionOr<void> observe(Node& target, MutationObserverInit options = {});
    void disconnect();
    NonnullRefPtrVector<MutationRecord> take_records();

    Vector<WeakPtr<Node>>& node_list() { return m_node_list; }
    Vector<WeakPtr<Node>> const& node_list() const { return m_node_list; }

    Bindings::CallbackType& callback() { return m_callback; }

    void enqueue_record(Badge<Node>, NonnullRefPtr<MutationRecord> mutation_record)
    {
        m_record_queue.append(move(mutation_record));
    }

private:
    MutationObserver(Bindings::WindowObject& window_object, Bindings::CallbackType callback);

    // https://dom.spec.whatwg.org/#concept-mo-callback
    Bindings::CallbackType m_callback;

    // https://dom.spec.whatwg.org/#mutationobserver-node-list
    Vector<WeakPtr<Node>> m_node_list;

    // https://dom.spec.whatwg.org/#concept-mo-queue
    NonnullRefPtrVector<MutationRecord> m_record_queue;
};

// https://dom.spec.whatwg.org/#registered-observer
struct RegisteredObserver : public RefCounted<RegisteredObserver> {
    static NonnullRefPtr<RegisteredObserver> create(MutationObserver& observer, MutationObserverInit& options)
    {
        return adopt_ref(*new RegisteredObserver(observer, options));
    }

    RegisteredObserver(MutationObserver& observer, MutationObserverInit& options)
        : observer(observer)
        , options(options)
    {
    }

    virtual ~RegisteredObserver() = default;

    NonnullRefPtr<MutationObserver> observer;
    MutationObserverInit options;
};

// https://dom.spec.whatwg.org/#transient-registered-observer
struct TransientRegisteredObserver final : public RegisteredObserver {
    static NonnullRefPtr<TransientRegisteredObserver> create(MutationObserver& observer, MutationObserverInit& options, RegisteredObserver& source)
    {
        return adopt_ref(*new TransientRegisteredObserver(observer, options, source));
    }

    TransientRegisteredObserver(MutationObserver& observer, MutationObserverInit& options, RegisteredObserver& source)
        : RegisteredObserver(observer, options)
        , source(source)
    {
    }

    virtual ~TransientRegisteredObserver() override = default;

    NonnullRefPtr<RegisteredObserver> source;
};

}