2 cached_set::{CacheId, CachedSet},
3 change_list::ChangeList,
4 events::EventsRegistry,
5 node::{Attribute, ElementNode, Listener, Node, NodeKind, TextNode},
10 cached_set: &CachedSet,
11 change_list: &mut ChangeList,
12 registry: &mut EventsRegistry,
15 cached_roots: &mut bumpalo::collections::Vec<CacheId>,
17 match (&new.kind, &old.kind) {
19 &NodeKind::Text(TextNode { text: new_text }),
20 &NodeKind::Text(TextNode { text: old_text }),
22 debug!(" both are text nodes");
23 if new_text != old_text {
24 debug!(" text needs updating");
25 change_list.emit_set_text(new_text);
29 (&NodeKind::Text(_), &NodeKind::Element(_)) => {
30 debug!(" replacing a text node with an element");
31 create(cached_set, change_list, registry, new, cached_roots);
32 registry.remove_subtree(&old);
33 change_list.emit_replace_with();
36 (&NodeKind::Element(_), &NodeKind::Text(_)) => {
37 debug!(" replacing an element with a text node");
38 create(cached_set, change_list, registry, new, cached_roots);
39 // Note: text nodes cannot have event listeners, so we don't need to
40 // remove the old node's listeners from our registry her.
41 change_list.emit_replace_with();
45 &NodeKind::Element(ElementNode {
46 tag_name: new_tag_name,
47 listeners: new_listeners,
48 attributes: new_attributes,
49 children: new_children,
50 namespace: new_namespace,
52 &NodeKind::Element(ElementNode {
53 tag_name: old_tag_name,
54 listeners: old_listeners,
55 attributes: old_attributes,
56 children: old_children,
57 namespace: old_namespace,
60 debug!(" updating an element");
61 if new_tag_name != old_tag_name || new_namespace != old_namespace {
62 debug!(" different tag names or namespaces; creating new element and replacing old element");
63 create(cached_set, change_list, registry, new, cached_roots);
64 registry.remove_subtree(&old);
65 change_list.emit_replace_with();
68 diff_listeners(change_list, registry, old_listeners, new_listeners);
69 diff_attributes(change_list, old_attributes, new_attributes);
80 // Both the new and old nodes are cached.
81 (&NodeKind::Cached(ref new), &NodeKind::Cached(ref old)) => {
82 cached_roots.push(new.id);
85 // This is the same cached node, so nothing has changed!
89 let new = cached_set.get(new.id);
90 let old = cached_set.get(old.id);
91 diff(cached_set, change_list, registry, old, new, cached_roots);
94 // New cached node when the old node was not cached. In this scenario,
95 // we assume that they are pretty different, and it isn't worth diffing
96 // the subtrees, so we just create the new cached node afresh.
97 (&NodeKind::Cached(ref c), _) => {
98 cached_roots.push(c.id);
99 let new = cached_set.get(c.id);
100 create(cached_set, change_list, registry, new, cached_roots);
101 registry.remove_subtree(&old);
102 change_list.emit_replace_with();
105 // Old cached node and new non-cached node. Again, assume that they are
106 // probably pretty different and create the new non-cached node afresh.
107 (_, &NodeKind::Cached(_)) => {
108 create(cached_set, change_list, registry, new, cached_roots);
109 registry.remove_subtree(&old);
110 change_list.emit_replace_with();
116 change_list: &mut ChangeList,
117 registry: &mut EventsRegistry,
121 debug!(" updating event listeners");
123 'outer1: for new_l in new {
125 // Safety relies on removing `new_l` from the registry manually at
126 // the end of its lifetime. This happens below in the `'outer2`
127 // loop, and elsewhere in diffing when removing old dom trees.
132 if new_l.event == old_l.event {
133 change_list.emit_update_event_listener(new_l);
138 change_list.emit_new_event_listener(new_l);
141 'outer2: for old_l in old {
142 registry.remove(old_l);
145 if new_l.event == old_l.event {
149 change_list.emit_remove_event_listener(old_l.event);
153fn diff_attributes(change_list: &mut ChangeList, old: &[Attribute], new: &[Attribute]) {
154 debug!(" updating attributes");
156 // Do O(n^2) passes to add/update and remove attributes, since
157 // there are almost always very few attributes.
158 'outer: for new_attr in new {
159 if new_attr.is_volatile() {
160 change_list.emit_set_attribute(new_attr.name, new_attr.value);
162 for old_attr in old {
163 if old_attr.name == new_attr.name {
164 if old_attr.value != new_attr.value {
165 change_list.emit_set_attribute(new_attr.name, new_attr.value);
170 change_list.emit_set_attribute(new_attr.name, new_attr.value);
174 'outer2: for old_attr in old {
175 for new_attr in new {
176 if old_attr.name == new_attr.name {
180 change_list.emit_remove_attribute(old_attr.name);
185 cached_set: &CachedSet,
186 change_list: &mut ChangeList,
187 registry: &mut EventsRegistry,
190 cached_roots: &mut bumpalo::collections::Vec<CacheId>,
192 debug!(" updating children shared by old and new");
194 let num_children_to_diff = cmp::min(new.len(), old.len());
195 let mut new_children = new.iter();
196 let mut old_children = old.iter();
197 let mut pushed = false;
199 for (i, (new_child, old_child)) in new_children
201 .zip(old_children.by_ref())
202 .take(num_children_to_diff)
206 change_list.emit_push_first_child();
209 debug_assert!(pushed);
210 change_list.emit_pop_push_next_sibling();
223 if old_children.next().is_some() {
224 debug!(" removing extra old children");
225 debug_assert!(new_children.next().is_none());
227 change_list.emit_push_first_child();
229 change_list.emit_pop_push_next_sibling();
231 change_list.emit_remove_self_and_next_siblings();
234 debug!(" creating new children");
235 for (i, new_child) in new_children.enumerate() {
236 if i == 0 && pushed {
237 change_list.emit_pop();
247 change_list.emit_append_child();
251 debug!(" done updating children");
253 change_list.emit_pop();
258 cached_set: &CachedSet,
259 change_list: &mut ChangeList,
260 registry: &mut EventsRegistry,
262 cached_roots: &mut bumpalo::collections::Vec<CacheId>,
265 NodeKind::Text(TextNode { text }) => {
266 change_list.emit_create_text_node(text);
268 NodeKind::Element(ElementNode {
275 if let Some(namespace) = namespace {
276 change_list.emit_create_element_ns(tag_name, namespace);
278 change_list.emit_create_element(tag_name);
284 change_list.emit_new_event_listener(l);
286 for attr in attributes {
287 if namespace.is_none() || attr.name.starts_with("xmlns") {
288 change_list.emit_set_attribute(&attr.name, &attr.value);
290 change_list.emit_set_attribute_ns(&attr.name, &attr.value);
293 for child in children {
301 change_list.emit_append_child();
304 NodeKind::Cached(c) => {
305 cached_roots.push(c.id);
306 let node = cached_set.get(c.id);
307 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