on 5a3aa52 help sign in
dodrio/src/diff.rs
920 lines 30 kB history raw
1use crate::{
2 cached_set::{CacheId, CachedSet},
3 change_list::ChangeListBuilder,
4 events::EventsRegistry,
5 node::{Attribute, ElementNode, Listener, Node, NodeKind, TextNode},
6};
7use fxhash::{FxHashMap, FxHashSet};
8use std::cmp::Ordering;
9use std::ops::Range;
10use std::u32;
12// Diff the `old` node with the `new` node. Emits instructions to modify a
13// physical DOM node that reflects `old` into something that reflects `new`.
14//
15// Upon entry to this function, the physical DOM node must be on the top of the
16// stack:
17//
18// [... node]
19//
20// The stack is in the same state when this function exits.
21pub(crate) fn diff(
22 cached_set: &CachedSet,
23 change_list: &mut ChangeListBuilder,
24 registry: &mut EventsRegistry,
25 old: &Node,
26 new: &Node,
27 cached_roots: &mut FxHashSet<CacheId>,
28) {
29 match (&new.kind, &old.kind) {
30 (
31 &NodeKind::Text(TextNode { text: new_text }),
32 &NodeKind::Text(TextNode { text: old_text }),
33 ) => {
34 if new_text != old_text {
35 change_list.set_text(new_text);
36 }
37 }
39 (&NodeKind::Text(_), &NodeKind::Element(_)) => {
40 create(cached_set, change_list, registry, new, cached_roots);
41 registry.remove_subtree(&old);
42 change_list.replace_with();
43 }
45 (&NodeKind::Element(_), &NodeKind::Text(_)) => {
46 create(cached_set, change_list, registry, new, cached_roots);
47 // Note: text nodes cannot have event listeners, so we don't need to
48 // remove the old node's listeners from our registry her.
49 change_list.replace_with();
50 }
52 (
53 &NodeKind::Element(ElementNode {
54 key: _,
55 tag_name: new_tag_name,
56 listeners: new_listeners,
57 attributes: new_attributes,
58 children: new_children,
59 namespace: new_namespace,
60 }),
61 &NodeKind::Element(ElementNode {
62 key: _,
63 tag_name: old_tag_name,
64 listeners: old_listeners,
65 attributes: old_attributes,
66 children: old_children,
67 namespace: old_namespace,
68 }),
69 ) => {
70 if new_tag_name != old_tag_name || new_namespace != old_namespace {
71 create(cached_set, change_list, registry, new, cached_roots);
72 registry.remove_subtree(&old);
73 change_list.replace_with();
74 return;
75 }
76 diff_listeners(change_list, registry, old_listeners, new_listeners);
77 diff_attributes(change_list, old_attributes, new_attributes);
78 diff_children(
79 cached_set,
80 change_list,
81 registry,
82 old_children,
83 new_children,
84 cached_roots,
85 );
86 }
88 // Both the new and old nodes are cached.
89 (&NodeKind::Cached(ref new), &NodeKind::Cached(ref old)) => {
90 cached_roots.insert(new.id);
92 if new.id == old.id {
93 // This is the same cached node, so nothing has changed!
94 return;
95 }
97 let new = cached_set.get(new.id);
98 let old = cached_set.get(old.id);
99 diff(cached_set, change_list, registry, old, new, cached_roots);
100 }
102 // New cached node when the old node was not cached. In this scenario,
103 // we assume that they are pretty different, and it isn't worth diffing
104 // the subtrees, so we just create the new cached node afresh.
105 (&NodeKind::Cached(ref c), _) => {
106 cached_roots.insert(c.id);
107 let new = cached_set.get(c.id);
108 create(cached_set, change_list, registry, new, cached_roots);
109 registry.remove_subtree(&old);
110 change_list.replace_with();
111 }
113 // Old cached node and new non-cached node. Again, assume that they are
114 // probably pretty different and create the new non-cached node afresh.
115 (_, &NodeKind::Cached(_)) => {
116 create(cached_set, change_list, registry, new, cached_roots);
117 registry.remove_subtree(&old);
118 change_list.replace_with();
119 }
120 }
123// Diff event listeners between `old` and `new`.
124//
125// The listeners' node must be on top of the stack:
126//
127// [... node]
128//
129// The stack is left unchanged.
130fn diff_listeners(
131 change_list: &mut ChangeListBuilder,
132 registry: &mut EventsRegistry,
133 old: &[Listener],
134 new: &[Listener],
135) {
136 'outer1: for new_l in new {
137 unsafe {
138 // Safety relies on removing `new_l` from the registry manually at
139 // the end of its lifetime. This happens below in the `'outer2`
140 // loop, and elsewhere in diffing when removing old dom trees.
141 registry.add(new_l);
142 }
144 for old_l in old {
145 if new_l.event == old_l.event {
146 change_list.update_event_listener(new_l);
147 continue 'outer1;
148 }
149 }
151 change_list.new_event_listener(new_l);
152 }
154 'outer2: for old_l in old {
155 registry.remove(old_l);
157 for new_l in new {
158 if new_l.event == old_l.event {
159 continue 'outer2;
160 }
161 }
162 change_list.remove_event_listener(old_l.event);
163 }
166// Diff a node's attributes.
167//
168// The attributes' node must be on top of the stack:
169//
170// [... node]
171//
172// The stack is left unchanged.
173fn diff_attributes(change_list: &mut ChangeListBuilder, old: &[Attribute], new: &[Attribute]) {
174 // Do O(n^2) passes to add/update and remove attributes, since
175 // there are almost always very few attributes.
176 'outer: for new_attr in new {
177 if new_attr.is_volatile() {
178 change_list.set_attribute(new_attr.name, new_attr.value);
179 } else {
180 for old_attr in old {
181 if old_attr.name == new_attr.name {
182 if old_attr.value != new_attr.value {
183 change_list.set_attribute(new_attr.name, new_attr.value);
184 }
185 continue 'outer;
186 }
187 }
188 change_list.set_attribute(new_attr.name, new_attr.value);
189 }
190 }
192 'outer2: for old_attr in old {
193 for new_attr in new {
194 if old_attr.name == new_attr.name {
195 continue 'outer2;
196 }
197 }
198 change_list.remove_attribute(old_attr.name);
199 }
202// Diff the given set of old and new children.
203//
204// The parent must be on top of the stack when this function is entered:
205//
206// [... parent]
207//
208// The stack is in the same state when this function returns.
209fn diff_children(
210 cached_set: &CachedSet,
211 change_list: &mut ChangeListBuilder,
212 registry: &mut EventsRegistry,
213 old: &[Node],
214 new: &[Node],
215 cached_roots: &mut FxHashSet<CacheId>,
216) {
217 if new.is_empty() {
218 if !old.is_empty() {
219 remove_all_children(change_list, registry, old);
220 }
221 return;
222 }
224 if old.is_empty() {
225 create_and_append_children(cached_set, change_list, registry, new, cached_roots);
226 return;
227 }
229 let new_is_keyed = new[0].key().is_some();
230 let old_is_keyed = old[0].key().is_some();
232 debug_assert!(
233 new.iter().all(|n| n.key().is_some() == new_is_keyed),
234 "all siblings must be keyed or all siblings must be non-keyed"
235 );
236 debug_assert!(
237 old.iter().all(|o| o.key().is_some() == old_is_keyed),
238 "all siblings must be keyed or all siblings must be non-keyed"
239 );
241 if new_is_keyed && old_is_keyed {
242 let t = change_list.next_temporary();
243 diff_keyed_children(cached_set, change_list, registry, old, new, cached_roots);
244 change_list.set_next_temporary(t);
245 } else {
246 diff_non_keyed_children(cached_set, change_list, registry, old, new, cached_roots);
247 }
250// Diffing "keyed" children.
251//
252// With keyed children, we care about whether we delete, move, or create nodes
253// versus mutate existing nodes in place. Presumably there is some sort of CSS
254// transition animation that makes the virtual DOM diffing algorithm
255// observable. By specifying keys for nodes, we know which virtual DOM nodes
256// must reuse (or not reuse) the same physical DOM nodes.
257//
258// This is loosely based on Inferno's keyed patching implementation. However, we
259// have to modify the algorithm since we are compiling the diff down into change
260// list instructions that will be executed later, rather than applying the
261// changes to the DOM directly as we compare virtual DOMs.
262//
263// https://github.com/infernojs/inferno/blob/36fd96/packages/inferno/src/DOM/patching.ts#L530-L739
264//
265// When entering this function, the parent must be on top of the stack:
266//
267// [... parent]
268//
269// Upon exiting, the stack is in the same state.
270fn diff_keyed_children(
271 cached_set: &CachedSet,
272 change_list: &mut ChangeListBuilder,
273 registry: &mut EventsRegistry,
274 old: &[Node],
275 new: &[Node],
276 cached_roots: &mut FxHashSet<CacheId>,
277) {
278 // First up, we diff all the nodes with the same key at the beginning of the
279 // children.
280 //
281 // `shared_prefix_count` is the count of how many nodes at the start of
282 // `new` and `old` share the same keys.
283 let shared_prefix_count =
284 match diff_keyed_prefix(cached_set, change_list, registry, old, new, cached_roots) {
285 KeyedPrefixResult::Finished => return,
286 KeyedPrefixResult::MoreWorkToDo(count) => count,
287 };
289 // Next, we find out how many of the nodes at the end of the children have
290 // the same key. We do _not_ diff them yet, since we want to emit the change
291 // list instructions such that they can be applied in a single pass over the
292 // DOM. Instead, we just save this information for later.
293 //
294 // `shared_suffix_count` is the count of how many nodes at the end of `new`
295 // and `old` share the same keys.
296 let shared_suffix_count = old[shared_prefix_count..]
297 .iter()
298 .rev()
299 .zip(new[shared_prefix_count..].iter().rev())
300 .take_while(|&(old, new)| old.key() == new.key())
301 .count();
303 let old_shared_suffix_start = old.len() - shared_suffix_count;
304 let new_shared_suffix_start = new.len() - shared_suffix_count;
306 // Ok, we now hopefully have a smaller range of children in the middle
307 // within which to re-order nodes with the same keys, remove old nodes with
308 // now-unused keys, and create new nodes with fresh keys.
309 diff_keyed_middle(
310 cached_set,
311 change_list,
312 registry,
313 &old[shared_prefix_count..old_shared_suffix_start],
314 &new[shared_prefix_count..new_shared_suffix_start],
315 cached_roots,
316 shared_prefix_count,
317 shared_suffix_count,
318 old_shared_suffix_start,
319 );
321 // Finally, diff the nodes at the end of `old` and `new` that share keys.
322 let old_suffix = &old[old_shared_suffix_start..];
323 let new_suffix = &new[new_shared_suffix_start..];
324 debug_assert_eq!(old_suffix.len(), new_suffix.len());
325 if !old_suffix.is_empty() {
326 diff_keyed_suffix(
327 cached_set,
328 change_list,
329 registry,
330 old_suffix,
331 new_suffix,
332 cached_roots,
333 new_shared_suffix_start,
334 );
335 }
338enum KeyedPrefixResult {
339 // Fast path: we finished diffing all the children just by looking at the
340 // prefix of shared keys!
341 Finished,
342 // There is more diffing work to do. Here is a count of how many children at
343 // the beginning of `new` and `old` we already processed.
344 MoreWorkToDo(usize),
347// Diff the prefix of children in `new` and `old` that share the same keys in
348// the same order.
349//
350// Upon entry of this function, the stack must be:
351//
352// [... parent]
353//
354// Upon exit, the stack is the same.
355fn diff_keyed_prefix(
356 cached_set: &CachedSet,
357 change_list: &mut ChangeListBuilder,
358 registry: &mut EventsRegistry,
359 old: &[Node],
360 new: &[Node],
361 cached_roots: &mut FxHashSet<CacheId>,
362) -> KeyedPrefixResult {
363 let mut pushed = false;
364 let mut shared_prefix_count = 0;
366 for (old, new) in old.iter().zip(new.iter()) {
367 if old.key() != new.key() {
368 break;
369 }
371 if pushed {
372 debug_assert!(shared_prefix_count > 0);
373 change_list.pop_push_next_sibling();
374 } else {
375 debug_assert_eq!(shared_prefix_count, 0);
376 change_list.push_first_child();
377 pushed = true;
378 }
380 diff(cached_set, change_list, registry, old, new, cached_roots);
381 shared_prefix_count += 1;
383 // At the end of the loop, the stack looks like
384 //
385 // [... parent child_we_just_diffed]
386 debug_assert!(pushed);
387 }
389 // If that was all of the old children, then create and append the remaining
390 // new children and we're finished.
391 if shared_prefix_count == old.len() {
392 debug_assert!(
393 pushed,
394 "we handle the case of empty children before calling this method, so
395 `shared_prefix_count` must be greater than zero, and therefore we
396 must have pushed in the above loop"
397 );
398 change_list.pop();
399 create_and_append_children(
400 cached_set,
401 change_list,
402 registry,
403 &new[shared_prefix_count..],
404 cached_roots,
405 );
406 return KeyedPrefixResult::Finished;
407 }
409 // And if that was all of the new children, then remove all of the remaining
410 // old children and we're finished.
411 if shared_prefix_count == new.len() {
412 // Same as above.
413 debug_assert!(pushed);
414 change_list.pop_push_next_sibling();
415 change_list.remove_self_and_next_siblings();
416 return KeyedPrefixResult::Finished;
417 }
419 if pushed {
420 debug_assert!(shared_prefix_count > 0);
421 change_list.pop();
422 }
423 KeyedPrefixResult::MoreWorkToDo(shared_prefix_count)
426// The most-general, expensive code path for keyed children diffing.
427//
428// We find the longest subsequence within `old` of children that are relatively
429// ordered the same way in `new` (via finding a longest-increasing-subsequence
430// of the old child's index within `new`). The children that are elements of
431// this subsequence will remain in place, minimizing the number of DOM moves we
432// will have to do.
433//
434// Upon entry to this function, the stack must be:
435//
436// [... parent]
437//
438// Upon exit from this function, it will be restored to that same state.
439fn diff_keyed_middle(
440 cached_set: &CachedSet,
441 change_list: &mut ChangeListBuilder,
442 registry: &mut EventsRegistry,
443 old: &[Node],
444 new: &[Node],
445 cached_roots: &mut FxHashSet<CacheId>,
446 shared_prefix_count: usize,
447 shared_suffix_count: usize,
448 old_shared_suffix_start: usize,
449) {
450 // Should have already diffed the shared-key prefixes and suffixes.
451 debug_assert_ne!(new.first().map(|n| n.key()), old.first().map(|o| o.key()));
452 debug_assert_ne!(new.last().map(|n| n.key()), old.last().map(|o| o.key()));
454 // The algorithm below relies upon using `u32::MAX` as a sentinel
455 // value, so if we have that many new nodes, it won't work. This
456 // check is a bit academic (hence only enabled in debug), since
457 // wasm32 doesn't have enough address space to hold that many nodes
458 // in memory.
459 debug_assert!(new.len() < u32::MAX as usize);
461 // Map from each `old` node's key to its index within `old`.
462 let mut old_key_to_old_index = FxHashMap::default();
463 old_key_to_old_index.reserve(old.len());
464 old_key_to_old_index.extend(old.iter().enumerate().map(|(i, o)| (o.key(), i)));
466 // The set of shared keys between `new` and `old`.
467 let mut shared_keys = FxHashSet::default();
468 // Map from each index in `new` to the index of the node in `old` that
469 // has the same key.
470 let mut new_index_to_old_index = Vec::with_capacity(new.len());
471 new_index_to_old_index.extend(new.iter().map(|n| {
472 let key = n.key();
473 if let Some(&i) = old_key_to_old_index.get(&key) {
474 shared_keys.insert(key);
475 i
476 } else {
477 u32::MAX as usize
478 }
479 }));
481 // If none of the old keys are reused by the new children, then we
482 // remove all the remaining old children and create the new children
483 // afresh.
484 if shared_suffix_count == 0 && shared_keys.is_empty() {
485 if shared_prefix_count == 0 {
486 remove_all_children(change_list, registry, old);
487 } else {
488 change_list.pop_push_next_sibling();
489 remove_self_and_next_siblings(change_list, registry, &old[shared_prefix_count..]);
490 }
491 create_and_append_children(cached_set, change_list, registry, new, cached_roots);
492 return;
493 }
495 // The longest increasing subsequence within `new_index_to_old_index` in
496 // reverse order (since `lis_with` adds the indices in reverse
497 // order). We will leave these nodes in place in the DOM, and only move
498 // nodes that are not part of the LIS. This results in the minimum
499 // number of DOM nodes moved.
500 let mut reverse_lis = Vec::with_capacity(new_index_to_old_index.len());
501 let mut predecessors = vec![0; new_index_to_old_index.len()];
502 let mut starts = vec![0; new_index_to_old_index.len()];
503 longest_increasing_subsequence::lis_with(
504 &new_index_to_old_index,
505 &mut reverse_lis,
506 |a, b| a < b,
507 &mut predecessors,
508 &mut starts,
509 );
511 // Save each of the old children whose keys are reused in the new
512 // children.
513 let mut old_index_to_temp = vec![u32::MAX; old.len()];
514 let mut start = 0;
515 loop {
516 let end = (start..old.len())
517 .find(|&i| {
518 let key = old[i].key();
519 !shared_keys.contains(&key)
520 })
521 .unwrap_or(old.len());
523 if end - start > 0 {
524 let mut t = change_list.save_children_to_temporaries(
525 shared_prefix_count + start,
526 shared_prefix_count + end,
527 );
528 for i in start..end {
529 old_index_to_temp[i] = t;
530 t += 1;
531 }
532 }
534 debug_assert!(end <= old.len());
535 if end == old.len() {
536 break;
537 } else {
538 start = end + 1;
539 }
540 }
542 // Remove any old children whose keys were not reused in the new
543 // children. Remove from the end first so that we don't mess up indices.
544 let mut removed_count = 0;
545 for (i, old_child) in old.iter().enumerate().rev() {
546 if !shared_keys.contains(&old_child.key()) {
547 change_list.remove_child(i + shared_prefix_count);
548 removed_count += 1;
549 }
550 }
552 // Whether we have pushed a child onto the stack or not.
553 let mut pushed = false;
555 // Now iterate from the end of the new children back to the beginning,
556 // moving old children to their new destination, diffing them, and
557 // creating new children as necessary. Note that iterating in reverse
558 // order lets us use `Node.prototype.insertBefore` to move/insert
559 // children.
560 if shared_suffix_count > 0 {
561 change_list.push_child(old_shared_suffix_start - removed_count);
562 pushed = true;
563 }
564 let mut segment_end = new.len();
565 for lis_index in reverse_lis {
566 // Because we use `u32::MAX` as a sentinel value representing "a child
567 // with this key is not a member of `new`", we filter that out here.
568 let old_index = new_index_to_old_index[lis_index];
569 if old_index == u32::MAX as usize {
570 continue;
571 }
573 // A segment begins with a member of the LIS, which will remain in
574 // place in the DOM, followed by zero or more new children that are
575 // not members of the LIS. If one of these new children shares a key
576 // with one of the old children, then we will move that old child in
577 // the DOM. Otherwise, we create the new child afresh.
578 debug_assert!(segment_end - lis_index > 0);
580 // Go through each of the non-LIS child and move-and-diff or create
581 // it.
582 diff_and_move_or_create_segment(
583 cached_set,
584 change_list,
585 registry,
586 old,
587 new,
588 cached_roots,
589 lis_index + 1..segment_end,
590 &new_index_to_old_index,
591 &old_index_to_temp,
592 &mut pushed,
593 );
595 // Now we push the member of the LIS and diff it with its
596 // correspondingly-keyed new child.
597 let new_child = &new[lis_index];
598 let temp = old_index_to_temp[old_index];
599 debug_assert_ne!(temp, u32::MAX);
600 if pushed {
601 change_list.pop();
602 }
603 change_list.push_temporary(temp);
604 pushed = true;
605 diff(
606 cached_set,
607 change_list,
608 registry,
609 &old[old_index],
610 new_child,
611 cached_roots,
612 );
614 segment_end = lis_index;
615 }
617 // And now diff-and-move or create each of the non-LIS children that
618 // appear before the first LIS-member.
619 diff_and_move_or_create_segment(
620 cached_set,
621 change_list,
622 registry,
623 old,
624 new,
625 cached_roots,
626 0..segment_end,
627 &new_index_to_old_index,
628 &old_index_to_temp,
629 &mut pushed,
630 );
632 if pushed {
633 change_list.pop();
634 }
637// Diff-and-then-move or create each new node in the given segment. The
638// segment's nodes must _not_ be members of the LIS (those nodes we will diff in
639// place elsewhere, and we should not move them nor create them afresh).
640//
641// On entering this function, the stack has the parent and optionally the next
642// sibling after this segment of children:
643//
644// [... parent next_sibling] if *pushed == true
645// [... parent] otherwise
646//
647// After exiting, if any children were moved or created, then `pushed` is set to
648// true and the stack has the first child in this segment on top. Otherwise the
649// stack is left as it was on entering.
650//
651// [... parent child] if *pushed == true
652// [... parent] otherwise
653fn diff_and_move_or_create_segment(
654 cached_set: &CachedSet,
655 change_list: &mut ChangeListBuilder,
656 registry: &mut EventsRegistry,
657 old: &[Node],
658 new: &[Node],
659 cached_roots: &mut FxHashSet<CacheId>,
660 segment: Range<usize>,
661 new_index_to_old_index: &[usize],
662 old_index_to_temp: &[u32],
663 pushed: &mut bool,
664) {
665 for new_index in segment.rev() {
666 let new_child = &new[new_index];
667 let old_index = new_index_to_old_index[new_index];
668 if old_index == u32::MAX as usize {
669 // The key is not reused. Create this new child afresh.
670 create(cached_set, change_list, registry, new_child, cached_roots);
671 } else {
672 // The key is reused. Diff it with the old node and then
673 // move the old node into its final destination.
674 let temp = old_index_to_temp[old_index];
675 debug_assert_ne!(temp, u32::MAX);
676 change_list.push_temporary(temp);
677 diff(
678 cached_set,
679 change_list,
680 registry,
681 new_child,
682 &old[old_index],
683 cached_roots,
684 );
685 }
687 // At this point the stack can have one of two shapes.
688 if *pushed {
689 // [... parent next_child new_child]
690 change_list.insert_before();
691 // [... parent new_child]
692 } else {
693 // [... parent new_child]
694 change_list.append_child();
695 // [... parent]
696 change_list.push_last_child();
697 // [... parent new_child]
698 *pushed = true;
699 }
701 // At the end of each loop iteration, the stack looks like:
702 //
703 // [... parent new_child]
704 debug_assert!(*pushed);
705 }
708// Diff the suffix of keyed children that share the same keys in the same order.
709//
710// The parent must be on the stack when we enter this function:
711//
712// [... parent]
713//
714// When this function exits, the stack remains the same.
715fn diff_keyed_suffix(
716 cached_set: &CachedSet,
717 change_list: &mut ChangeListBuilder,
718 registry: &mut EventsRegistry,
719 old: &[Node],
720 new: &[Node],
721 cached_roots: &mut FxHashSet<CacheId>,
722 new_shared_suffix_start: usize,
723) {
724 debug_assert_eq!(old.len(), new.len());
725 debug_assert!(!old.is_empty());
727 // [... parent]
728 change_list.push_child(new_shared_suffix_start);
729 // [... parent new_child]
731 for (old_child, new_child) in old.iter().zip(new.iter()) {
732 diff(
733 cached_set,
734 change_list,
735 registry,
736 old_child,
737 new_child,
738 cached_roots,
739 );
741 // [... parent this_new_child]
742 change_list.pop_push_next_sibling();
743 // [... parent next_new_child]
744 }
746 // [... parent]
747 change_list.pop();
750// Diff children that are not keyed.
751//
752// The parent must be on the top of the stack when entering this function:
753//
754// [... parent]
755//
756// The stack is in the same state when this function returns.
757fn diff_non_keyed_children(
758 cached_set: &CachedSet,
759 change_list: &mut ChangeListBuilder,
760 registry: &mut EventsRegistry,
761 old: &[Node],
762 new: &[Node],
763 cached_roots: &mut FxHashSet<CacheId>,
764) {
765 // Handled these cases in `diff_children` before calling this function.
766 debug_assert!(!new.is_empty());
767 debug_assert!(!old.is_empty());
769 for (i, (new_child, old_child)) in new.iter().zip(old.iter()).enumerate() {
770 if i == 0 {
771 change_list.push_first_child();
772 } else {
773 change_list.pop_push_next_sibling();
774 }
776 diff(
777 cached_set,
778 change_list,
779 registry,
780 old_child,
781 new_child,
782 cached_roots,
783 );
784 }
786 match old.len().cmp(&new.len()) {
787 Ordering::Greater => {
788 change_list.pop_push_next_sibling();
789 remove_self_and_next_siblings(change_list, registry, &old[new.len()..]);
790 }
791 Ordering::Less => {
792 change_list.pop();
793 create_and_append_children(
794 cached_set,
795 change_list,
796 registry,
797 &new[old.len()..],
798 cached_roots,
799 );
800 }
801 Ordering::Equal => {
802 change_list.pop();
803 }
804 }
807// Create the given children and append them to the parent node.
808//
809// The parent node must currently be on top of the stack:
810//
811// [... parent]
812//
813// When this function returns, the stack is in the same state.
814fn create_and_append_children(
815 cached_set: &CachedSet,
816 change_list: &mut ChangeListBuilder,
817 registry: &mut EventsRegistry,
818 new: &[Node],
819 cached_roots: &mut FxHashSet<CacheId>,
820) {
821 for child in new {
822 create(cached_set, change_list, registry, child, cached_roots);
823 change_list.append_child();
824 }
827// Remove all of a node's children.
828//
829// The stack must have this shape upon entry to this function:
830//
831// [... parent]
832//
833// When this function returns, the stack is in the same state.
834fn remove_all_children(
835 change_list: &mut ChangeListBuilder,
836 registry: &mut EventsRegistry,
837 old: &[Node],
838) {
839 for child in old {
840 registry.remove_subtree(child);
841 }
842 // Fast way to remove all children: set the node's textContent to an empty
843 // string.
844 change_list.set_text("");
847// Remove the current child and all of its following siblings.
848//
849// The stack must have this shape upon entry to this function:
850//
851// [... parent child]
852//
853// After the function returns, the child is no longer on the stack:
854//
855// [... parent]
856fn remove_self_and_next_siblings(
857 change_list: &mut ChangeListBuilder,
858 registry: &mut EventsRegistry,
859 old: &[Node],
860) {
861 for child in old {
862 registry.remove_subtree(child);
863 }
864 change_list.remove_self_and_next_siblings();
867// Emit instructions to create the given virtual node.
868//
869// The stack may have any shape upon entering this function:
870//
871// [...]
872//
873// When this function returns, the new node is on top of the stack:
874//
875// [... node]
876fn create(
877 cached_set: &CachedSet,
878 change_list: &mut ChangeListBuilder,
879 registry: &mut EventsRegistry,
880 node: &Node,
881 cached_roots: &mut FxHashSet<CacheId>,
882) {
883 match node.kind {
884 NodeKind::Text(TextNode { text }) => {
885 change_list.create_text_node(text);
886 }
887 NodeKind::Element(ElementNode {
888 key: _,
889 tag_name,
890 listeners,
891 attributes,
892 children,
893 namespace,
894 }) => {
895 if let Some(namespace) = namespace {
896 change_list.create_element_ns(tag_name, namespace);
897 } else {
898 change_list.create_element(tag_name);
899 }
900 for l in listeners {
901 unsafe {
902 registry.add(l);
903 }
904 change_list.new_event_listener(l);
905 }
906 for attr in attributes {
907 change_list.set_attribute(&attr.name, &attr.value);
908 }
909 for child in children {
910 create(cached_set, change_list, registry, child, cached_roots);
911 change_list.append_child();
912 }
913 }
914 NodeKind::Cached(ref c) => {
915 cached_roots.insert(c.id);
916 let node = cached_set.get(c.id);
917 create(cached_set, change_list, registry, node, cached_roots)
918 }
919 }