Repository navigation
Expand file tree
/
Copy pathDOMDiffer.js
More file actions
executable file
·1697 lines (1398 loc) · 61.8 KB
/
Copy pathDOMDiffer.js
File metadata and controls
executable file
·1697 lines (1398 loc) · 61.8 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//https://github.com/oliverfoster/DOMDiffer 2016-03-09
// Uses CommonJS, AMD or browser globals to register library
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(function() {
return (root.DOMDiffer = factory());
});
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = factory();
} else {
// Browser globals
this.DOMDiffer = factory();
}
}(this, function () {
var trim_regex = /^\s+|\s+$/g;
var svgNS = "http://www.w3.org/2000/svg";
var proto = {
setOptions: function setOptions(options) {
this.options = options || {};
this._ignoreAttributesWithPrefix = null;
this._ignoreAttributes = null;
this._ignoreClass = null;
var ignoreAttributesWithPrefix = this.options.ignoreAttributesWithPrefix;
var ignoreAttributes = this.options.ignoreAttributes;
var ignoreClasses = this.options.ignoreClasses;
if ((!ignoreAttributesWithPrefix || !ignoreAttributesWithPrefix.length )
&& (!ignoreAttributes || !ignoreAttributes.length )
&& (!ignoreClasses || !ignoreClasses.length )) return;
var regex = "";
var lastIndex;
if (ignoreAttributesWithPrefix && ignoreAttributesWithPrefix.length > 0) {
regex+="^("
lastIndex = ignoreAttributesWithPrefix.length-1;
for (var i = 0, l = ignoreAttributesWithPrefix.length; i < l; i++) {
var prefix = ignoreAttributesWithPrefix[i];
regex+=this._escapeRegExp(prefix);
if (i !== lastIndex) {
regex+="|";
}
}
regex+=")";
this._ignoreAttributesWithPrefix = new RegExp(regex, "i");
}
if (ignoreAttributes && ignoreAttributes.length > 0) {
this._ignoreAttributes = {};
for (var i = 0, l = ignoreAttributes.length; i < l; i++) {
this._ignoreAttributes[ignoreAttributes[i]] = true;
}
}
if (ignoreClasses) {
this._ignoreClass = {};
for (var i = 0, l = ignoreClasses.length; i < l; i++) {
this._ignoreClass[ignoreClasses[i]] = true;
}
}
},
_escapeRegExp: function _escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
},
//turn dom nodes into vnodes and diff
nodesDiff: function nodesDiff(source, destination, options) {
var vsource = this.nodeToVNode(source);
var vdestination = this.nodeToVNode(destination);
return this.vNodesDiff(vsource, vdestination, options);
},
//turn dom node into vnode
nodeToVNode: function nodeToVNode(DOMNode, options, context) {
options = options || {};
if (!context ) {
context = {
depth: 0,
index: 0,
uid: 0,
parentUid: -1
};
}
//build vNode
var vNode = this._vNodeFromNode(DOMNode, context);
this._vNodeAttributes(DOMNode, vNode);
this._injectSpecialAttributes(DOMNode, vNode);
if (!options.ignoreChildren) {
var allowedSubTree = this._isAllowedSubTree(vNode);
if (!allowedSubTree) return vNode;
//capture deep from childNodes
var deep = 0;
var vChildNodes = vNode.childNodes;
if (DOMNode.childNodes.length) deep++;
var childNode;
var childNodeType;
var vChildNode;
var childContext;
for (var i = 0, l = DOMNode.childNodes.length; i < l; i++) {
childNode = DOMNode.childNodes[i];
childNodeType = childNode.nodeType;
switch (childNodeType) {
case 1:
childContext = {
depth: vNode.depth+1,
index: i,
uid: context.uid, // carry current uid count through
parentUid: vNode.uid
};
vChildNode = this.nodeToVNode(childNode, options, childContext);
deep = deep+vChildNode.deep;
context.uid = childContext.uid;
break;
case 3:
//add text node
vChildNode = {
DOMNode: childNode,
nodeType: childNodeType,
nodeName: childNode.nodeName,
data: childNode.data,
trimmed: this._trim(childNode.data),
index: i,
depth: vNode.depth+1,
deep: 0,
uid: context.uid++,
parentUid: vNode.uid
};
break;
}
vChildNodes.push(vChildNode);
}
vNode.deep = deep;
}
return vNode;
},
_vNodeFromNode: function _vNodeFromNode(DOMNode, context) {
//capture depth and index from parent
var depth = context.depth;
var index = context.index;
var vNode = {
DOMNode: DOMNode,
nodeType: DOMNode.nodeType,
nodeName: DOMNode.nodeName,
attributes: {},
id: "",
classes: {},
childNodes: [],
depth: depth,
index: index,
deep: 0,
uid: context.uid++,
parentUid: context.parentUid
};
return vNode;
},
_vNodeAttributes: function _vNodeAttributes(DOMNode, vNode) {
//build vNode attributes
var nodeAttributes = DOMNode.attributes;
var vNodeAttributes = vNode.attributes;
var vNodeClasses = vNode.classes;
var attribute;
var attributeName;
var attributeValue;
var forbiddenAttribute;
for (var i = 0, l = nodeAttributes.length; i < l; i++) {
attribute = nodeAttributes.item(i);
attributeName = attribute.name;
attributeValue = attribute.value;
if (this._ignoreAttributes) {
if (this._ignoreAttributes[attributeName]) continue;
}
if (this._ignoreAttributesWithPrefix) {
forbiddenAttribute = this._ignoreAttributesWithPrefix.test(attributeName);
if (forbiddenAttribute) continue;
}
vNodeAttributes[attributeName] = attributeValue;
}
var classValue = vNodeAttributes['class'];
if (classValue) {
var classes = classValue.split(" ");
var className;
var allowedClass;
for (var c = 0, cl = classes.length; c < cl; c++) {
className = classes[c];
if (!className) continue;
if (this._ignoreClass) {
if (this._ignoreClass[className]) continue;
}
vNodeClasses[className] = true;
}
}
var idValue = vNodeAttributes.id;
if (idValue) {
vNode.id = idValue;
}
delete vNodeAttributes['class'];
delete vNodeAttributes.id;
},
_injectSpecialAttributes: function _injectSpecialAttributes(DOMNode, vNode) {
var vNodeAttributes = vNode.attributes;
switch (vNode.nodeName) {
case "svg":
if (!vNodeAttributes["xmlns"]) vNodeAttributes["xmlns"] = svgNS;
break;
}
},
_isAllowedSubTree: function _isAllowedSubTree(vNode) {
//don't stop at root nodes
if (vNode.parentUid === -1) return true;
var ignoreSubTreesWithAttributes = this.options.ignoreSubTreesWithAttributes;
if (!ignoreSubTreesWithAttributes || !ignoreSubTreesWithAttributes.length) return true;
//if node has attribute then stop building tree here
var attr;
for (var i = 0, l = ignoreSubTreesWithAttributes.length; i < l; i++) {
attr = ignoreSubTreesWithAttributes[i];
if (vNode.attributes[attr] !== undefined) {
return false;
}
}
return true;
},
//trim whitespace from a string ends
_trim: function _trim(string) {
return string.replace(trim_regex, '');
},
//flatten vnodes and diff
vNodesDiff: function vNodesDiff(vsource, vdestination, options) {
var fVSource = this._vNodeToFVNode(vsource);
var fVDestination = this._vNodeToFVNode(vdestination);
return this._fVNodesDiff(fVSource, fVDestination, options);
},
//flatten a vnode
_vNodeToFVNode: function _vNodeToFVNode(vNode, rtn, index) {
index = index || {};
rtn = rtn || [];
switch (vNode.nodeType) {
case 1:
rtn.push(vNode);
index[vNode.uid] = vNode;
var childNodes = vNode.childNodes;
for (var i = 0, l = childNodes.length; i < l; i++) {
this._vNodeToFVNode(childNodes[i], rtn, index);
}
break;
case 3:
rtn.push(vNode);
index[vNode.uid] = vNode;
break;
}
return rtn;
},
//create a differential of flattened vnodes
//1. match source nodes to the best destination node
//2. create matches to remove all left-over source nodes with no matches
//4. create matches to add all left-over destination nodes
//6. find the start destination node
//7. rebuild destination tree from source tree using added nodes where necessary and returning the order of the differences
//8. use the differential to turn a copy of the source tree into the destination tree, removing redundant diffs on the way
//9. return finished differential
_fVNodesDiff: function _fVNodesDiff(fVSource, fVDestination, options) {
options = options || {};
//create editable arrays to preserve original arrays
var fVSource2 = fVSource.slice(0);
var fVDestination2 = fVDestination.slice(0);
//try to match containers
var sourceMatches = [];
var uidIndexes = {
bySourceUid: {},
byDestinationUid: {}
};
fVSource2.unmatchedCount = fVSource2.length;
for (var minRating = 1; minRating >= 0.20; minRating-=0.20) {
if (!fVSource2.unmatchedCount) break;
this._compareAndRemoveFVNodes(fVSource2, fVDestination2, minRating, sourceMatches, uidIndexes, options);
}
var removes = this._createRemoveMatches(fVSource2, sourceMatches, uidIndexes);
this._createAddMatches(fVDestination2, sourceMatches, uidIndexes);
fVSource2 = undefined;
fVDestination2 = undefined;
var destinationStartVNode = this._fVNodeToVNode(fVDestination);
var orderedMatches = [];
this._rebuildDestinationFromSourceMatches(orderedMatches, destinationStartVNode, sourceMatches, uidIndexes);
if (options.ignoreContainer && orderedMatches[0] && orderedMatches[0].sourceParentUid === -1) {
//remove container from diff
orderedMatches.splice(0,1);
}
sourceMatches = undefined;
var differential = [].concat(
removes, //re-add removes as they get lost in the ordering
orderedMatches
)
//find the start node on the original source
var sourceStartVNode = this._fVNodeToVNode(fVSource);
//remove redundant differentials by test-applying the diff
//use performOnVNode: false so as not to change the original source vnode
//use performOnDOM: false so as not to change the original dom structure
options = this._cloneObject(options);
options.performOnDOM = options.performOnDOM || false;
options.performOnVNode = options.performOnVNode || false;
this.vNodeDiffApply(sourceStartVNode, differential, options);
this._sanitizeDifferential(differential);
return differential;
},
//compare each source vnode with each destination vnode
//when a match is found, remove both the source and destination from their original flattened arrays and add a match diff object
_compareAndRemoveFVNodes: function _compareAndRemoveFVNodes(fVSource, fVDestination, minRate, sourceMatches, uidIndexes, options) {
if (!fVSource.length || !fVDestination.length ) return;
var mode = 0;
if (fVSource.unmatchedCount > 50) mode = 1;
//always remove root containers as matches first
if (fVSource[0] && fVDestination[0] && fVSource[0].parentUid === -1 && fVDestination[0].parentUid === -1) {
var source = fVSource[0];
var destination = fVDestination[0];
var rate = this._rateCompare(source, destination, minRate);
switch(mode) {
case 0:
fVSource[0] = undefined;
fVDestination[0] = undefined;
break;
case 1:
fVSource.splice(0,1);
fVDestination.splice(0,1);
}
fVSource.unmatchedCount--;
var diffObj = {
source: source,
destination: destination,
nodeType: source.nodeType,
sourceUid: source.uid,
sourceParentUid: source.parentUid,
sourceIndex: source.index,
destinationUid: destination.uid,
destinationParentUid: destination.parentUid,
destinationIndex: destination.index,
isEqual: rate === 1,
rate: rate
};
this._expandDifferences(diffObj, options);
sourceMatches.push(diffObj);
uidIndexes.bySourceUid[diffObj.sourceUid] = diffObj;
uidIndexes.byDestinationUid[diffObj.destinationUid] = diffObj;
}
var fIndex = fVSource.length-1;
var f2Index = fVDestination.length-1;
var maxRating = -1, maxRated, maxRatedIndex;
//match each source piece to the best destination piece
//this way the fewest source moves will be made
var sourceLength = fVSource.length;
var source;
var sourceUid;
var destinationLength = fVDestination.length;
var destination;
var destinationUid;
var diffObj;
var rate;
var bySourceUid = uidIndexes.bySourceUid;
var byDestinationUid = uidIndexes.byDestinationUid;
for (var sIndex = 0; sIndex < sourceLength; sIndex++) {
if (fVSource.unmatchedCount > 50) mode = 1;
else mode = 0;
source = fVSource[sIndex];
if (!source) {
switch (mode) {
case 1:
fVSource.splice(sIndex,1);
sIndex--;
sourceLength--;
}
continue;
}
sourceUid = source.uid;
for (var dIndex = 0; dIndex < destinationLength; dIndex++) {
destination = fVDestination[dIndex];
if (!destination) {
switch (mode) {
case 1:
fVDestination.splice(dIndex,1);
dIndex--;
destinationLength--;
}
continue;
}
destinationUid = destination.uid;
if (destination.nodeType !== source.nodeType) continue;
if (source.nodeName !== destination.nodeName) continue;
rate = this._rateCompare(destination, source, minRate);
if (rate < minRate || rate <= maxRating) continue;
maxRated = destination;
maxRating = rate;
maxRatedIndex = dIndex;
if (rate !== 1) continue;
switch(mode) {
case 0:
fVSource[sIndex] = undefined;
fVDestination[dIndex] = undefined;
break;
case 1:
fVSource.splice(sIndex,1);
fVDestination.splice(dIndex,1);
sourceLength--;
destinationLength--;
}
fVSource.unmatchedCount--;
diffObj = {
source: source,
destination: destination,
nodeType: source.nodeType,
sourceUid: sourceUid,
sourceParentUid: source.parentUid,
sourceIndex: source.index,
destinationUid: destination.uid,
destinationParentUid: destination.parentUid,
destinationIndex: destination.index,
isEqual: rate === 1,
rate: rate
};
this._expandDifferences(diffObj, options);
sourceMatches.push(diffObj);
bySourceUid[diffObj.sourceUid] = diffObj;
byDestinationUid[diffObj.destinationUid] = diffObj;
maxRating = 0;
maxRated = undefined;
maxRatedIndex = undefined;
sIndex = -1;
break;
}
if (!maxRated) continue;
switch(mode) {
case 0:
fVSource[sIndex] = undefined;
fVDestination[maxRatedIndex] = undefined;
break;
case 1:
fVSource.splice(sIndex,1);
fVDestination.splice(maxRatedIndex,1);
sourceLength--;
destinationLength--;
}
fVSource.unmatchedCount--;
diffObj = {
source: source,
destination: maxRated,
nodeType: source.nodeType,
sourceUid: source.uid,
sourceParentUid: source.parentUid,
sourceIndex: source.index,
destinationUid: maxRated.uid,
destinationParentUid: maxRated.parentUid,
destinationIndex: maxRated.index,
isEqual: false,
rate: maxRating
};
this._expandDifferences(diffObj, options);
sourceMatches.push(diffObj);
bySourceUid[diffObj.sourceUid] = diffObj;
byDestinationUid[diffObj.destinationUid] = diffObj;
maxRating = 0;
maxRated = undefined;
maxRatedIndex = undefined;
sIndex = -1;
}
},
//create a percentage difference value for two vnodes
_rateCompare: function _rateCompare(vdestination, vsource, minRate) {
var value = 0;
var rate = -1;
switch (vdestination.nodeType) {
case 1:
value+=vsource.id===vdestination.id?3:0;
value+=vsource.depth === vdestination.depth? 3 : 0;
if (minRate > 0.36 && value < 6) return -1;
value+=this._keyValueCompare(vsource.classes, vdestination.classes) * 3;
if (minRate > 0.53 && value < 9) return -1;
value+=this._keyValueCompareIgnore(vsource.attributes, vdestination.attributes, {"style":true}) * 2;
value+=(vsource.childNodes.length) === (vdestination.childNodes.length)? 2 : 0;
value+=vsource.childNodes.length === vdestination.childNodes.length? 2 : 0;
value+=vsource.deep === vdestination.deep? 1 : 0;
value+=vsource.index === vdestination.index? 1 : 0;
rate = (value / 17);
break;
case 3:
value+=vsource.depth === vdestination.depth? 3 : 0;
if (minRate > 0.43 && value < 3) return -1;
value+=vsource.index === vdestination.index? 1 : 0;
value+=vsource.trimmed === vdestination.trimmed? 2 : 0;
value+=vsource.data === vdestination.data? 1 : 0;
rate = (value / 7);
}
return rate;
},
//create a percentage difference value for two vnodes
_rateCompareNoChildren: function _rateCompareNoChildren(vdestination, vsource) {
var value = 0;
if (vdestination.nodeType !== vsource.nodeType) return -1;
if (vsource.nodeName !== vdestination.nodeName) return -1;
var rate = -1;
switch (vdestination.nodeType) {
case 1:
value+=vsource.id===vdestination.id?3:0;
value+=this._keyValueCompare(vsource.classes, vdestination.classes) * 3;
value+=this._keyValueCompareIgnore(vsource.attributes, vdestination.attributes, {"style":true}) * 2;
rate = (value / 8);
break;
case 3:
value+=vsource.trimmed === vdestination.trimmed? 2 : 0;
value+=vsource.data === vdestination.data? 1 : 0;
rate = (value / 3)
}
return rate;
},
_rateCompareNoDepth: function _rateCompareNoDepth(vdestination, vsource) {
var value = 0;
if (vdestination.nodeType !== vsource.nodeType) return -1;
if (vsource.nodeName !== vdestination.nodeName) return -1;
var rate = -1;
switch (vdestination.nodeType) {
case 1:
value+=vsource.id===vdestination.id?3:0;
value+=this._keyValueCompare(vsource.classes, vdestination.classes) * 3;
value+=this._keyValueCompareIgnore(vsource.attributes, vdestination.attributes, {"style":true}) * 2;
value+=vsource.index === vdestination.index? 1 : 0;
rate = (value / 9);
break;
case 3:
value+=vsource.index === vdestination.index? 1 : 0;
value+=vsource.trimmed === vdestination.trimmed? 2 : 0;
value+=vsource.data === vdestination.data? 1 : 0;
rate = (value / 4);
}
return rate;
},
//compare two key value pair objects
//return percentage match 0-1
_keyValueCompare: function _keyValueCompare(object1, object2) {
var matchingValues = 0;
var totalKeys = 0;
var o2value;
for (var k1 in object1) {
totalKeys++;
if (object1[k1] === object2[k1]) {
matchingValues++;
}
}
for (var k2 in object2) {
if (object1[k2] === undefined) {
totalKeys++;
}
}
if (!totalKeys) return 1;
return (matchingValues / totalKeys) || -1;
},
_keyValueCompareIgnore: function _keyValueCompareIgnore(object1, object2, ignores) {
var matchingValues = 0;
var totalKeys = 0;
var o2value;
for (var k1 in object1) {
if (ignores[k1]) continue;
totalKeys++;
if (object1[k1] === object2[k1]) {
matchingValues++;
}
}
for (var k2 in object2) {
if (ignores[k2]) continue;
if (object1[k2] === undefined) {
totalKeys++;
}
}
if (!totalKeys) return 1;
return (matchingValues / totalKeys) || -1;
},
//manufacture 'matches' for the items to remove from the source tree
_createRemoveMatches: function _createRemoveMatches(fVSource2, sourceMatches, uidIndexes) {
if (!fVSource2.length ) return [];
var removes = [];
var deleteSourceRoots = [];
var sourceParentUids = {};
var source;
var diffObj;
for (var f2Index = 0, l = fVSource2.length; f2Index < l; f2Index++) {
source = fVSource2[f2Index];
if (!source) continue;
diffObj = {
changeRemove: true,
source: source,
nodeType: source.nodeType,
sourceUid: source.uid,
sourceParentUid: source.parentUid,
};
sourceMatches.push(diffObj);
uidIndexes.bySourceUid[diffObj.sourceUid] = diffObj;
uidIndexes.byDestinationUid[diffObj.destinationUid] = diffObj;
sourceParentUids[source.uid] = true;
if (!sourceParentUids[source.parentUid]) {
deleteSourceRoots.push(source);
//only add source root deletion to output diff
removes.push(diffObj);
} else {
diffObj.redundant = true;
}
}
return removes;
},
//manufacture 'matches' for the items to add to the source tree from the destination
_createAddMatches: function _createAddMatches(fVDestination2, sourceMatches, uidIndexes) {
if (!fVDestination2.length ) return [];
//create new source pieces to add by cloning the needed destination pieces
var newDestinationRoots = [];
var destinationParentUids = {};
var destination;
for (var f2Index = 0, l = fVDestination2.length; f2Index < l; f2Index++) {
destination = fVDestination2[f2Index];
if (!destination) continue;
destinationParentUids[destination.uid] = true;
if (!destinationParentUids[destination.parentUid]) {
newDestinationRoots.push(destination);
}
}
//create matches for new objects to that sourceUids don't conflict with preexisting sourceNodes
//assign new item.sourceUids from the negative spectrum
var newSourceUids = -1;
var translateOldDestionationUidToNewSourceUid = {};
for (var i = 0, l = newDestinationRoots.length; i < l; i++) {
var fVSource = this._vNodeToFVNode(newDestinationRoots[i]);
var fVDestination = this._vNodeToFVNode(newDestinationRoots[i]);
for (var c = 0, cl = fVDestination.length; c < cl; c++) {
var destination = fVDestination[c];
var oldDestinationParentUid = destination.parentUid;
var oldDestionationUid = destination.uid;
var newSourceParentUid = translateOldDestionationUidToNewSourceUid[oldDestinationParentUid];
//check if there is an indexed matching destination
var existingDiff = uidIndexes.byDestinationUid[destination.uid];
if (existingDiff) {
//no need to create new nodes as nodes will be moved from existing source
translateOldDestionationUidToNewSourceUid[oldDestionationUid] = existingDiff.sourceUid;
continue;
}
var source = this.vNodeToOuterVNode(fVSource[c], {performOnVNode: false});
var newSourceUid = newSourceUids--;
translateOldDestionationUidToNewSourceUid[oldDestionationUid] = newSourceUid;
//if we're dealing with a child of a new root
if (!newSourceParentUid ) {
//if no translation to a new uid, not a child of a new root
//assume new node is connected to a preexisting source node
newSourceParentUid = uidIndexes.byDestinationUid[oldDestinationParentUid].sourceUid;
}
//configure new source nodes
source.uid = newSourceUid;
source.parentUid = newSourceParentUid;
source.DOMNode = undefined;
var vNode = {};
switch (source.nodeType) {
case 1:
vNode.attributes = source.attributes;
vNode.classes = source.classes;
vNode.id = source.id;
vNode.nodeName = source.nodeName;
vNode.nodeType = source.nodeType;
vNode.childNodes = [];
break;
case 3:
vNode.data = source.data;
vNode.nodeType = source.nodeType;
vNode.nodeName = source.nodeName;
vNode.trimmed = source.trimmed;
}
vNode.uid = newSourceUid;
vNode.parentUid = newSourceParentUid;
vNode.deep = source.deep;
vNode.depth = source.depth;
vNode.index = source.index;
var diffObj = {
changeAdd: true,
changeHierachyData: true,
destination: destination,
nodeType: destination.nodeType,
destinationUid: oldDestionationUid,
destinationParentUid: oldDestinationParentUid,
depth: destination.depth,
deep: destination.deep,
source: source,
vNode: vNode,
sourceUid: newSourceUid,
sourceParentUid: newSourceParentUid,
sourceIndex: source.index,
destinationIndex: destination.index
}
sourceMatches.push(diffObj);
uidIndexes.bySourceUid[newSourceUid] = diffObj;
uidIndexes.byDestinationUid[oldDestionationUid] = diffObj;
}
}
},
//add attributes to the match to express the differences between each pair
//this makes each match-pair into a match-diff
//strip DOMNodes
_expandDifferences: function _expandDifferences(match, options) {
if (match.changeRemove || match.changeAdd) return;
var source = match.source;
var destination = match.destination;
if (source.parentUid === -1 && (options.ignoreContainer || this.options.ignoreContainer) ) return;
if (source.deep !== destination.deep
|| source.depth !== destination.depth) {
match.changeHierachyData = true;
match.depth = destination.depth;
match.deep = destination.deep;
match.isEqual = false;
}
switch(match.nodeType) {
case 1:
var changeAttributes = this._diffKeyValues(source.attributes, destination.attributes);
if (!changeAttributes.isEqual) {
match.changeAttributes = true;
match.attributes = changeAttributes;
match.isEqual = false;
}
var changeClasses = this._diffKeys(source.classes, destination.classes);
if (!changeClasses.isEqual) {
match.changeClasses = true;
match.classes = changeClasses;
match.isEqual = false;
}
if (source.id !== destination.id) {
match.changeId = true;
match.id = destination.id;
match.isEqual = false;
}
break;
case 3:
if (source.data !== destination.data) {
match.changeData = true;
match.data = destination.data;
match.isEqual = false;
}
break;
}
},
//describe the differences between two objects (source & destination attributes, or source & destination classes)
_diffKeys: function _diffKeys (source, destination) {
var diff = {
removed: [],
addedLength: 0,
added: {},
isEqual: true,
value: ""
};
var nodeValue;
for (var k in source) {
nodeValue = destination[k];
if (nodeValue === undefined) {
diff.removed.push(k);
continue;
}
if (source[k] !== nodeValue) {
diff.added[k] = nodeValue;
diff.addedLength++;
}
}
var destKeys = [];
for (var k in destination) {
destKeys.push(k);
if (source[k] === undefined) {
diff.added[k] = destination[k];
diff.addedLength++;
}
}
if (diff.removed.length || diff.addedLength) {
diff.isEqual = false;
diff.value = destKeys.join(" ");
}
return diff;
},
_diffKeyValues: function _diffKeyValues (source, destination) {
var diff = {
removed: [],
addedLength: 0,
added: {},
isEqual: true
};
var nodeValue;
for (var k in source) {
nodeValue = destination[k];
if (nodeValue === undefined) {
diff.removed.push(k);
continue;
}
if (source[k] !== nodeValue) {
diff.added[k] = nodeValue;
diff.addedLength++;
}
}
for (var k in destination) {
if (source[k] === undefined) {
diff.added[k] = destination[k];
diff.addedLength++;
}
}
if (diff.removed.length || diff.addedLength) {
diff.isEqual = false;
}
return diff;
},
//find the first vnode in a flattened vnode list
_fVNodeToVNode: function _fVNodeToVNode(fVNode) {
var startVNode;
for (var i = 0, vNode; vNode = fVNode[i++];) {
if (vNode.parentUid === -1) {
startVNode = vNode;
break;
}
}
if (!startVNode) throw "cannot find start node";
return startVNode;
},
//recursively go through the destination tree, checking each source mapped node (or added node) and outputing the match-diffs where necessary
//this filters and orders the match-diffs creating a preliminary differential
_rebuildDestinationFromSourceMatches: function _rebuildDestinationFromSourceMatches(diffs, destinationStartVNode, sourceMatches, uidIndexes, destinationParentVNode, newIndex) {
var diff = uidIndexes.byDestinationUid[destinationStartVNode.uid];
var isNotRootNode = (diff.sourceParentUid !== -1);
if (isNotRootNode) {
var sourceParentDiff = uidIndexes.bySourceUid[diff.sourceParentUid];
var destinationParentDiff = uidIndexes.byDestinationUid[destinationParentVNode.uid];
//if source parent destination match, is not the same as the expected destination then move
if (sourceParentDiff.destinationUid !== destinationParentVNode.uid) {
var moveToSourceUid = destinationParentDiff.sourceUid;
//mark to move into a different parent
diff.isEqual = false;
diff.changeParent = true;