@@ -5,23 +5,42 @@ import realNodeMap from './util/real-node-map';
5
5
6
6
const { Node } = window ;
7
7
8
+ function diffNode ( source , destination ) {
9
+ let nodeInstructions = compareNode ( source , destination ) ;
10
+
11
+ // If there are instructions (even an empty array) it means the node can be
12
+ // diffed and doesn't have to be replaced. If the instructions are falsy
13
+ // it means that the nodes are not similar (cannot be changed) and must be
14
+ // replaced instead.
15
+ if ( nodeInstructions ) {
16
+ return nodeInstructions . concat ( diff ( { source, destination } ) ) ;
17
+ }
18
+
19
+ return [ {
20
+ destination,
21
+ source,
22
+ type : types . REPLACE_CHILD
23
+ } ] ;
24
+ }
25
+
8
26
export default function diff ( opts = { } ) {
9
- let src = opts . source ;
10
- let dst = opts . destination ;
11
- let instructions = [ ] ;
27
+ const src = opts . source ;
28
+ const dst = opts . destination ;
12
29
13
30
if ( ! src || ! dst ) {
14
31
return [ ] ;
15
32
}
16
33
17
- let srcChs = src . childNodes ;
18
- let dstChs = dst . childNodes ;
19
- let srcChsLen = srcChs ? srcChs . length : 0 ;
20
- let dstChsLen = dstChs ? dstChs . length : 0 ;
34
+ let instructions = opts . root ? diffNode ( src , dst ) : [ ] ;
35
+
36
+ const srcChs = src . childNodes ;
37
+ const dstChs = dst . childNodes ;
38
+ const srcChsLen = srcChs ? srcChs . length : 0 ;
39
+ const dstChsLen = dstChs ? dstChs . length : 0 ;
21
40
22
41
for ( let a = 0 ; a < dstChsLen ; a ++ ) {
23
- let curSrc = srcChs [ a ] ;
24
- let curDst = dstChs [ a ] ;
42
+ const curSrc = srcChs [ a ] ;
43
+ const curDst = dstChs [ a ] ;
25
44
26
45
// If there is no matching destination node it means we need to remove the
27
46
// current source node from the source.
@@ -41,24 +60,7 @@ export default function diff (opts = {}) {
41
60
}
42
61
}
43
62
44
- let nodeInstructions = compareNode ( curSrc , curDst ) ;
45
-
46
- // If there are instructions (even an empty array) it means the node can be
47
- // diffed and doesn't have to be replaced. If the instructions are falsy
48
- // it means that the nodes are not similar (cannot be changed) and must be
49
- // replaced instead.
50
- if ( nodeInstructions ) {
51
- const newOpts = opts ;
52
- newOpts . destination = curDst ;
53
- newOpts . source = curSrc ;
54
- instructions = instructions . concat ( nodeInstructions , diff ( newOpts ) ) ;
55
- } else {
56
- instructions . push ( {
57
- destination : curDst ,
58
- source : curSrc ,
59
- type : types . REPLACE_CHILD
60
- } ) ;
61
- }
63
+ instructions = instructions . concat ( diffNode ( curSrc , curDst ) ) ;
62
64
}
63
65
64
66
if ( dstChsLen < srcChsLen ) {
0 commit comments