Skip to content

Commit 14993fb

Browse files
committed
improved code commenting, function names
1 parent 03c447a commit 14993fb

File tree

1 file changed

+48
-52
lines changed

1 file changed

+48
-52
lines changed

diffDOM.js

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,8 @@
702702
diffs, i, last, e1, e2;
703703

704704
// no correspondence whatsoever
705-
// if t1 or t2 contain differences that are not text nodes, return a diff.
706-
707-
// two text nodes with differences
708705
if (mappings === 0) {
706+
// two text nodes with differences
709707
if (t1.nodeName === '#text' && t2.nodeName === '#text' && t1.data !== t2.data) {
710708
return [new Diff({
711709
action: 'modiFyTextElement',
@@ -721,8 +719,7 @@
721719
for (i = 0; i < last; i += 1) {
722720
e1 = t1_child_nodes[i];
723721
e2 = t2_child_nodes[i];
724-
// TODO: This is a similar code path to the one
725-
// in findFirstInnerDiff. Can we unify these?
722+
726723
if (e1 && !e2) {
727724
if (e1.nodeName === '#text') {
728725
return [new Diff({
@@ -761,63 +758,26 @@
761758
}
762759

763760
// one or more differences: find first diff
764-
return this.findFirstInnerDiff(t1, t2, subtrees, route);
761+
return this.attemptGroupRelocation(t1, t2, subtrees, route);
765762
},
766763

767-
findValueDiff: function(t1, t2, route) {
768-
// Differences of value. Only useful if the value/selection/checked value
769-
// differs from what is represented in the DOM. For example in the case
770-
// of filled out forms, etc.
771-
var diffs = [];
772-
773-
if (t1.selected !== t2.selected) {
774-
diffs.push(new Diff({
775-
action: 'modifySelected',
776-
oldValue: t1.selected,
777-
newValue: t2.selected,
778-
route: route
779-
}));
780-
}
781-
782-
if ((t1.value || t2.value) && t1.value !== t2.value && t1.nodeName !== 'OPTION') {
783-
diffs.push(new Diff({
784-
action: 'modifyValue',
785-
oldValue: t1.value,
786-
newValue: t2.value,
787-
route: route
788-
}));
789-
}
790-
if (t1.checked !== t2.checked) {
791-
diffs.push(new Diff({
792-
action: 'modifyChecked',
793-
oldValue: t1.checked,
794-
newValue: t2.checked,
795-
route: route
796-
}));
797-
}
798-
799-
return diffs;
800-
},
801-
802-
803-
findFirstInnerDiff: function(t1, t2, subtrees, route) {
804-
if (subtrees.length === 0) {
805-
return [];
806-
}
764+
attemptGroupRelocation: function(t1, t2, subtrees, route) {
765+
/* Once t1.childNodes and t2.childNodes have the same length,
766+
* attempts are made at equalizing the two. First all initial elements
767+
* with no group affiliation (gaps=true) are removed (if in t1) or
768+
* added (if in t2). Then the creation of a group relocation diff is
769+
* attempted.
770+
*/
807771

808772
var gapInformation = getGapInformation(t1, t2, subtrees),
809773
gaps1 = gapInformation.gaps1,
810-
gl1 = gaps1.length,
811774
gaps2 = gapInformation.gaps2,
812-
gl2 = gaps1.length,
813775
destinationDifferent, toGroup,
776+
group, node, similarNode, testI,
814777
i, j;
815778

816-
// Check for correct submap sequencing (irrespective of gaps) first:
817-
var group, node, similarNode, testI, shortest = gl1 < gl2 ? gaps1 : gaps2;
818-
819779
// group relocation
820-
for (i = 0; i < shortest.length; i += 1) {
780+
for (i = 0; i < gaps1.length; i += 1) {
821781
if (gaps1[i] === true) {
822782
node = t1.childNodes[i];
823783
if (node.nodeName === '#text') {
@@ -891,6 +851,42 @@
891851
}
892852
return [];
893853
},
854+
855+
findValueDiff: function(t1, t2, route) {
856+
// Differences of value. Only useful if the value/selection/checked value
857+
// differs from what is represented in the DOM. For example in the case
858+
// of filled out forms, etc.
859+
var diffs = [];
860+
861+
if (t1.selected !== t2.selected) {
862+
diffs.push(new Diff({
863+
action: 'modifySelected',
864+
oldValue: t1.selected,
865+
newValue: t2.selected,
866+
route: route
867+
}));
868+
}
869+
870+
if ((t1.value || t2.value) && t1.value !== t2.value && t1.nodeName !== 'OPTION') {
871+
diffs.push(new Diff({
872+
action: 'modifyValue',
873+
oldValue: t1.value,
874+
newValue: t2.value,
875+
route: route
876+
}));
877+
}
878+
if (t1.checked !== t2.checked) {
879+
diffs.push(new Diff({
880+
action: 'modifyChecked',
881+
oldValue: t1.checked,
882+
newValue: t2.checked,
883+
route: route
884+
}));
885+
}
886+
887+
return diffs;
888+
},
889+
894890
// ===== Apply a virtual diff =====
895891

896892
applyVirtual: function(tree, diffs) {

0 commit comments

Comments
 (0)