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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use dom_struct::dom_struct;
use html5ever::{LocalName, Namespace};

use crate::dom::bindings::codegen::Bindings::MutationRecordBinding::MutationRecord_Binding::MutationRecordMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::node::{window_from_node, Node};
use crate::dom::nodelist::NodeList;

#[dom_struct]
pub struct MutationRecord {
    reflector_: Reflector,
    record_type: DOMString,
    target: Dom<Node>,
    attribute_name: Option<DOMString>,
    attribute_namespace: Option<DOMString>,
    old_value: Option<DOMString>,
    added_nodes: MutNullableDom<NodeList>,
    removed_nodes: MutNullableDom<NodeList>,
    next_sibling: Option<Dom<Node>>,
    prev_sibling: Option<Dom<Node>>,
}

impl MutationRecord {
    #[allow(crown::unrooted_must_root)]
    pub fn attribute_mutated(
        target: &Node,
        attribute_name: &LocalName,
        attribute_namespace: Option<&Namespace>,
        old_value: Option<DOMString>,
    ) -> DomRoot<MutationRecord> {
        let record = Box::new(MutationRecord::new_inherited(
            "attributes",
            target,
            Some(DOMString::from(&**attribute_name)),
            attribute_namespace.map(|n| DOMString::from(&**n)),
            old_value,
            None,
            None,
            None,
            None,
        ));
        reflect_dom_object(record, &*window_from_node(target))
    }

    pub fn character_data_mutated(
        target: &Node,
        old_value: Option<DOMString>,
    ) -> DomRoot<MutationRecord> {
        reflect_dom_object(
            Box::new(MutationRecord::new_inherited(
                "characterData",
                target,
                None,
                None,
                old_value,
                None,
                None,
                None,
                None,
            )),
            &*window_from_node(target),
        )
    }

    pub fn child_list_mutated(
        target: &Node,
        added_nodes: Option<&[&Node]>,
        removed_nodes: Option<&[&Node]>,
        next_sibling: Option<&Node>,
        prev_sibling: Option<&Node>,
    ) -> DomRoot<MutationRecord> {
        let window = window_from_node(target);
        let added_nodes = added_nodes.map(|list| NodeList::new_simple_list_slice(&window, list));
        let removed_nodes =
            removed_nodes.map(|list| NodeList::new_simple_list_slice(&window, list));

        reflect_dom_object(
            Box::new(MutationRecord::new_inherited(
                "childList",
                target,
                None,
                None,
                None,
                added_nodes.as_deref(),
                removed_nodes.as_deref(),
                next_sibling,
                prev_sibling,
            )),
            &*window,
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn new_inherited(
        record_type: &str,
        target: &Node,
        attribute_name: Option<DOMString>,
        attribute_namespace: Option<DOMString>,
        old_value: Option<DOMString>,
        added_nodes: Option<&NodeList>,
        removed_nodes: Option<&NodeList>,
        next_sibling: Option<&Node>,
        prev_sibling: Option<&Node>,
    ) -> MutationRecord {
        MutationRecord {
            reflector_: Reflector::new(),
            record_type: DOMString::from(record_type),
            target: Dom::from_ref(target),
            attribute_name,
            attribute_namespace,
            old_value,
            added_nodes: MutNullableDom::new(added_nodes),
            removed_nodes: MutNullableDom::new(removed_nodes),
            next_sibling: next_sibling.map(Dom::from_ref),
            prev_sibling: prev_sibling.map(Dom::from_ref),
        }
    }
}

impl MutationRecordMethods for MutationRecord {
    // https://dom.spec.whatwg.org/#dom-mutationrecord-type
    fn Type(&self) -> DOMString {
        self.record_type.clone()
    }

    // https://dom.spec.whatwg.org/#dom-mutationrecord-target
    fn Target(&self) -> DomRoot<Node> {
        DomRoot::from_ref(&*self.target)
    }

    // https://dom.spec.whatwg.org/#dom-mutationrecord-attributename
    fn GetAttributeName(&self) -> Option<DOMString> {
        self.attribute_name.clone()
    }

    // https://dom.spec.whatwg.org/#dom-mutationrecord-attributenamespace
    fn GetAttributeNamespace(&self) -> Option<DOMString> {
        self.attribute_namespace.clone()
    }

    // https://dom.spec.whatwg.org/#dom-mutationrecord-oldvalue
    fn GetOldValue(&self) -> Option<DOMString> {
        self.old_value.clone()
    }

    // https://dom.spec.whatwg.org/#dom-mutationrecord-addednodes
    fn AddedNodes(&self) -> DomRoot<NodeList> {
        self.added_nodes.or_init(|| {
            let window = window_from_node(&*self.target);
            NodeList::empty(&window)
        })
    }

    // https://dom.spec.whatwg.org/#dom-mutationrecord-removednodes
    fn RemovedNodes(&self) -> DomRoot<NodeList> {
        self.removed_nodes.or_init(|| {
            let window = window_from_node(&*self.target);
            NodeList::empty(&window)
        })
    }

    // https://dom.spec.whatwg.org/#dom-mutationrecord-previoussibling
    fn GetPreviousSibling(&self) -> Option<DomRoot<Node>> {
        self.prev_sibling
            .as_ref()
            .map(|node| DomRoot::from_ref(&**node))
    }

    // https://dom.spec.whatwg.org/#dom-mutationrecord-previoussibling
    fn GetNextSibling(&self) -> Option<DomRoot<Node>> {
        self.next_sibling
            .as_ref()
            .map(|node| DomRoot::from_ref(&**node))
    }
}