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
/* 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 std::slice;
use js::context::NoGC;
use crate::dom::Node;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::element::Element;
pub(crate) enum ChildrenMutation<'a> {
Append {
prev: &'a Node,
added: &'a [&'a Node],
},
Insert {
prev: &'a Node,
added: &'a [&'a Node],
next: &'a Node,
},
Prepend {
added: &'a [&'a Node],
next: &'a Node,
},
Replace {
prev: Option<&'a Node>,
removed: &'a Node,
added: &'a [&'a Node],
next: Option<&'a Node>,
},
ReplaceAll {
removed: &'a [&'a Node],
added: &'a [&'a Node],
},
/// Mutation for when a Text node's data is modified.
/// This doesn't change the structure of the list, which is what the other
/// variants' fields are stored for at the moment, so this can just have no
/// fields.
ChangeText,
}
impl<'a> ChildrenMutation<'a> {
pub(super) fn insert(
prev: Option<&'a Node>,
added: &'a [&'a Node],
next: Option<&'a Node>,
) -> ChildrenMutation<'a> {
match (prev, next) {
(None, None) => ChildrenMutation::ReplaceAll {
removed: &[],
added,
},
(Some(prev), None) => ChildrenMutation::Append { prev, added },
(None, Some(next)) => ChildrenMutation::Prepend { added, next },
(Some(prev), Some(next)) => ChildrenMutation::Insert { prev, added, next },
}
}
pub(super) fn replace(
prev: Option<&'a Node>,
removed: &'a Option<&'a Node>,
added: &'a [&'a Node],
next: Option<&'a Node>,
) -> ChildrenMutation<'a> {
if let Some(ref removed) = *removed {
if let (None, None) = (prev, next) {
ChildrenMutation::ReplaceAll {
removed: slice::from_ref(removed),
added,
}
} else {
ChildrenMutation::Replace {
prev,
removed,
added,
next,
}
}
} else {
ChildrenMutation::insert(prev, added, next)
}
}
pub(super) fn replace_all(
removed: &'a [&'a Node],
added: &'a [&'a Node],
) -> ChildrenMutation<'a> {
ChildrenMutation::ReplaceAll { removed, added }
}
/// Get the child that follows the added or removed children.
/// Currently only used when this mutation might force us to
/// restyle later children (see HAS_SLOW_SELECTOR_LATER_SIBLINGS and
/// Element's implementation of VirtualMethods::children_changed).
pub(crate) fn next_child(&self) -> Option<&Node> {
match *self {
ChildrenMutation::Append { .. } => None,
ChildrenMutation::Insert { next, .. } => Some(next),
ChildrenMutation::Prepend { next, .. } => Some(next),
ChildrenMutation::Replace { next, .. } => next,
ChildrenMutation::ReplaceAll { .. } => None,
ChildrenMutation::ChangeText => None,
}
}
/// If nodes were added or removed at the start or end of a container, return any
/// previously-existing child whose ":first-child" or ":last-child" status *may* have changed.
///
/// NOTE: This does not check whether the inserted/removed nodes were elements, so in some
/// cases it will return a false positive. This doesn't matter for correctness, because at
/// worst the returned element will be restyled unnecessarily.
pub(crate) fn modified_edge_element(&self, no_gc: &NoGC) -> Option<DomRoot<Node>> {
match *self {
// Add/remove at start of container: Return the first following element.
ChildrenMutation::Prepend { next, .. } |
ChildrenMutation::Replace {
prev: None,
next: Some(next),
..
} => next
.inclusively_following_siblings_unrooted(no_gc)
.find(|node| node.is::<Element>())
.map(|node| node.as_rooted()),
// Add/remove at end of container: Return the last preceding element.
ChildrenMutation::Append { prev, .. } |
ChildrenMutation::Replace {
prev: Some(prev),
next: None,
..
} => prev
.inclusively_preceding_siblings_unrooted(no_gc)
.find(|node| node.is::<Element>())
.map(|node| node.as_rooted()),
// Insert or replace in the middle:
ChildrenMutation::Insert { prev, next, .. } |
ChildrenMutation::Replace {
prev: Some(prev),
next: Some(next),
..
} => {
if prev
.inclusively_preceding_siblings_unrooted(no_gc)
.all(|node| !node.is::<Element>())
{
// Before the first element: Return the first following element.
next.inclusively_following_siblings_unrooted(no_gc)
.find(|node| node.is::<Element>())
.map(|node| node.as_rooted())
} else if next
.inclusively_following_siblings_unrooted(no_gc)
.all(|node| !node.is::<Element>())
{
// After the last element: Return the last preceding element.
prev.inclusively_preceding_siblings_unrooted(no_gc)
.find(|node| node.is::<Element>())
.map(|node| node.as_rooted())
} else {
None
}
},
ChildrenMutation::Replace {
prev: None,
next: None,
..
} => unreachable!(),
ChildrenMutation::ReplaceAll { .. } => None,
ChildrenMutation::ChangeText => None,
}
}
}