2 cached_set::{CacheId, CachedSet},
3 change_list::ChangeListBuilder,
4 events::EventsRegistry,
5 node::{Attribute, ElementNode, Listener, Node, NodeKind, TextNode},
11 cached_set: &CachedSet,
12 change_list: &mut ChangeListBuilder,
13 registry: &mut EventsRegistry,
16 cached_roots: &mut FxHashSet<CacheId>,
18 match (&new.kind, &old.kind) {
20 &NodeKind::Text(TextNode { text: new_text }),
21 &NodeKind::Text(TextNode { text: old_text }),
23 debug!(" both are text nodes");
24 if new_text != old_text {
25 debug!(" text needs updating");
26 change_list.set_text(new_text);
30 (&NodeKind::Text(_), &NodeKind::Element(_)) => {
31 debug!(" replacing a text node with an element");
32 create(cached_set, change_list, registry, new, cached_roots);
33 registry.remove_subtree(&old);
34 change_list.replace_with();
37 (&NodeKind::Element(_), &NodeKind::Text(_)) => {
38 debug!(" replacing an element with a text node");
39 create(cached_set, change_list, registry, new, cached_roots);
40 // Note: text nodes cannot have event listeners, so we don't need to
41 // remove the old node's listeners from our registry her.
42 change_list.replace_with();
46 &NodeKind::Element(ElementNode {
47 tag_name: new_tag_name,
48 listeners: new_listeners,
49 attributes: new_attributes,
50 children: new_children,
51 namespace: new_namespace,
53 &NodeKind::Element(ElementNode {
54 tag_name: old_tag_name,
55 listeners: old_listeners,
56 attributes: old_attributes,
57 children: old_children,
58 namespace: old_namespace,
61 debug!(" updating an element");
62 if new_tag_name != old_tag_name || new_namespace != old_namespace {
63 debug!(" different tag names or namespaces; creating new element and replacing old element");
64 create(cached_set, change_list, registry, new, cached_roots);
65 registry.remove_subtree(&old);
66 change_list.replace_with();
69 diff_listeners(change_list, registry, old_listeners, new_listeners);
70 diff_attributes(change_list, old_attributes, new_attributes);
81 // Both the new and old nodes are cached.
82 (&NodeKind::Cached(ref new), &NodeKind::Cached(ref old)) => {
83 cached_roots.insert(new.id);
86 // This is the same cached node, so nothing has changed!
90 let new = cached_set.get(new.id);
91 let old = cached_set.get(old.id);
92 diff(cached_set, change_list, registry, old, new, cached_roots);
95 // New cached node when the old node was not cached. In this scenario,
96 // we assume that they are pretty different, and it isn't worth diffing
97 // the subtrees, so we just create the new cached node afresh.
98 (&NodeKind::Cached(ref c), _) => {
99 cached_roots.insert(c.id);
100 let new = cached_set.get(c.id);
101 create(cached_set, change_list, registry, new, cached_roots);
102 registry.remove_subtree(&old);
103 change_list.replace_with();
106 // Old cached node and new non-cached node. Again, assume that they are
107 // probably pretty different and create the new non-cached node afresh.
108 (_, &NodeKind::Cached(_)) => {
109 create(cached_set, change_list, registry, new, cached_roots);
110 registry.remove_subtree(&old);
111 change_list.replace_with();
117 change_list: &mut ChangeListBuilder,
118 registry: &mut EventsRegistry,
122 debug!(" updating event listeners");
124 'outer1: for new_l in new {
126 // Safety relies on removing `new_l` from the registry manually at
127 // the end of its lifetime. This happens below in the `'outer2`
128 // loop, and elsewhere in diffing when removing old dom trees.
133 if new_l.event == old_l.event {
134 change_list.update_event_listener(new_l);
139 change_list.new_event_listener(new_l);
142 'outer2: for old_l in old {
143 registry.remove(old_l);
146 if new_l.event == old_l.event {
150 change_list.remove_event_listener(old_l.event);
154fn diff_attributes(change_list: &mut ChangeListBuilder, old: &[Attribute], new: &[Attribute]) {
155 debug!(" updating attributes");
157 // Do O(n^2) passes to add/update and remove attributes, since
158 // there are almost always very few attributes.
159 'outer: for new_attr in new {
160 if new_attr.is_volatile() {
161 change_list.set_attribute(new_attr.name, new_attr.value);
163 for old_attr in old {
164 if old_attr.name == new_attr.name {
165 if old_attr.value != new_attr.value {
166 change_list.set_attribute(new_attr.name, new_attr.value);
171 change_list.set_attribute(new_attr.name, new_attr.value);
175 'outer2: for old_attr in old {
176 for new_attr in new {
177 if old_attr.name == new_attr.name {
181 change_list.remove_attribute(old_attr.name);
186 cached_set: &CachedSet,
187 change_list: &mut ChangeListBuilder,
188 registry: &mut EventsRegistry,
191 cached_roots: &mut FxHashSet<CacheId>,
193 debug!(" updating children shared by old and new");
195 let num_children_to_diff = cmp::min(new.len(), old.len());
196 let mut new_children = new.iter();
197 let mut old_children = old.iter();
198 let mut pushed = false;
200 for (i, (new_child, old_child)) in new_children
202 .zip(old_children.by_ref())
203 .take(num_children_to_diff)
207 change_list.push_first_child();
210 debug_assert!(pushed);
211 change_list.pop_push_next_sibling();
224 if old_children.next().is_some() {
225 debug!(" removing extra old children");
226 debug_assert!(new_children.next().is_none());
228 change_list.pop_push_next_sibling();
230 change_list.push_first_child();
232 change_list.remove_self_and_next_siblings();
235 debug!(" creating new children");
236 for (i, new_child) in new_children.enumerate() {
237 if i == 0 && pushed {
248 change_list.append_child();
252 debug!(" done updating children");
259 cached_set: &CachedSet,
260 change_list: &mut ChangeListBuilder,
261 registry: &mut EventsRegistry,
263 cached_roots: &mut FxHashSet<CacheId>,
266 NodeKind::Text(TextNode { text }) => {
267 change_list.create_text_node(text);
269 NodeKind::Element(ElementNode {
276 if let Some(namespace) = namespace {
277 change_list.create_element_ns(tag_name, namespace);
279 change_list.create_element(tag_name);
285 change_list.new_event_listener(l);
287 for attr in attributes {
288 change_list.set_attribute(&attr.name, &attr.value);
290 for child in children {
298 change_list.append_child();
301 NodeKind::Cached(c) => {
302 cached_roots.insert(c.id);
303 let node = cached_set.get(c.id);
304 create(cached_set, change_list, registry, node, cached_roots)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list