|
700 | 700 | t1_child_nodes = t1.childNodes ? t1.childNodes : [], |
701 | 701 | t2_child_nodes = t2.childNodes ? t2.childNodes : [], |
702 | 702 | childNodesLengthDifference, diffs = [], |
703 | | - index = 0, last, e1, e2, i; |
| 703 | + index = 0, |
| 704 | + last, e1, e2, i; |
704 | 705 |
|
705 | 706 | if (subtrees.length > 1) { |
706 | 707 | /* Two or more groups have been identified among the childnodes of t1 |
|
738 | 739 | })); |
739 | 740 | index -= 1; |
740 | 741 | } else { |
741 | | - diffs.push(new Diff({ |
742 | | - action: 'removeElement', |
743 | | - route: route.concat(index), |
744 | | - element: cloneObj(e1) |
745 | | - })); |
746 | | - index -= 1; |
| 742 | + diffs.push(new Diff({ |
| 743 | + action: 'removeElement', |
| 744 | + route: route.concat(index), |
| 745 | + element: cloneObj(e1) |
| 746 | + })); |
| 747 | + index -= 1; |
747 | 748 | } |
748 | 749 |
|
749 | 750 | } else if (e2 && !e1) { |
|
754 | 755 | value: e2.data |
755 | 756 | })); |
756 | 757 | } else { |
757 | | - diffs.push(new Diff({ |
758 | | - action: 'addElement', |
759 | | - route: route.concat(index), |
760 | | - element: cloneObj(e2) |
761 | | - })); |
| 758 | + diffs.push(new Diff({ |
| 759 | + action: 'addElement', |
| 760 | + route: route.concat(index), |
| 761 | + element: cloneObj(e2) |
| 762 | + })); |
762 | 763 | } |
763 | 764 | } |
764 | 765 | } |
765 | 766 | /* We are now guaranteed that childNodes e1 and e2 exist, |
766 | 767 | * and that they can be diffed. |
767 | 768 | */ |
768 | | - /* Diffs in child nodes should not affect the parent node, |
769 | | - * so we let these diffs be submitted together with other |
770 | | - * diffs. |
771 | | - */ |
| 769 | + /* Diffs in child nodes should not affect the parent node, |
| 770 | + * so we let these diffs be submitted together with other |
| 771 | + * diffs. |
| 772 | + */ |
772 | 773 |
|
773 | 774 | if (e1 && e2) { |
774 | 775 | diffs = diffs.concat(this.findNextDiff(e1, e2, route.concat(index))); |
|
796 | 797 | gaps2 = gapInformation.gaps2, |
797 | 798 | shortest = Math.min(gaps1.length, gaps2.length), |
798 | 799 | destinationDifferent, toGroup, |
799 | | - group, node, similarNode, testI, |
| 800 | + group, node, similarNode, testI, diffs = [], |
| 801 | + index = 0, |
800 | 802 | i, j; |
801 | 803 |
|
802 | | - // group relocation |
| 804 | + |
803 | 805 | for (i = 0; i < shortest; i += 1) { |
804 | 806 | if (gaps1[i] === true) { |
805 | 807 | node = t1.childNodes[i]; |
|
814 | 816 | } |
815 | 817 | } |
816 | 818 | if (!similarNode) { |
817 | | - return [new Diff({ |
| 819 | + diffs.push(new Diff({ |
818 | 820 | action: 'modifyTextElement', |
819 | | - route: route.concat(i), |
| 821 | + route: route.concat(index), |
820 | 822 | oldValue: node.data, |
821 | 823 | newValue: t2.childNodes[i].data |
822 | | - })]; |
| 824 | + })); |
823 | 825 | } |
824 | 826 | } |
825 | | - return [new Diff({ |
| 827 | + diffs.push(new Diff({ |
826 | 828 | action: 'removeTextElement', |
827 | | - route: route.concat(i), |
| 829 | + route: route.concat(index), |
828 | 830 | value: node.data |
829 | | - })]; |
| 831 | + })); |
| 832 | + index -= 1; |
| 833 | + return diffs; |
| 834 | + } else { |
| 835 | + diffs.push(new Diff({ |
| 836 | + action: 'removeElement', |
| 837 | + route: route.concat(index), |
| 838 | + element: cloneObj(node) |
| 839 | + })); |
| 840 | + index -= 1; |
830 | 841 | } |
831 | | - return [new Diff({ |
832 | | - action: 'removeElement', |
833 | | - route: route.concat(i), |
834 | | - element: cloneObj(node) |
835 | | - })]; |
836 | | - } |
837 | | - if (gaps2[i] === true) { |
| 842 | + |
| 843 | + } else if (gaps2[i] === true) { |
838 | 844 | node = t2.childNodes[i]; |
839 | 845 | if (node.nodeName === '#text') { |
840 | | - return [new Diff({ |
| 846 | + diffs.push(new Diff({ |
841 | 847 | action: 'addTextElement', |
842 | | - route: route.concat(i), |
| 848 | + route: route.concat(index), |
843 | 849 | value: node.data |
844 | | - })]; |
| 850 | + })); |
| 851 | + index += 1; |
| 852 | + } else { |
| 853 | + diffs.push(new Diff({ |
| 854 | + action: 'addElement', |
| 855 | + route: route.concat(index), |
| 856 | + element: cloneObj(node) |
| 857 | + })); |
| 858 | + index += 1; |
845 | 859 | } |
846 | | - return [new Diff({ |
847 | | - action: 'addElement', |
848 | | - route: route.concat(i), |
849 | | - element: cloneObj(node) |
850 | | - })]; |
851 | | - } |
852 | | - if (gaps1[i] !== gaps2[i]) { |
| 860 | + |
| 861 | + } else if (gaps1[i] !== gaps2[i]) { |
| 862 | + if (diffs.length > 0) { |
| 863 | + return diffs; |
| 864 | + } |
| 865 | + // group relocation |
853 | 866 | group = subtrees[gaps1[i]]; |
854 | 867 | toGroup = Math.min(group.new, (t1.childNodes.length - group.length)); |
855 | 868 | if (toGroup !== group.old) { |
|
868 | 881 | to: toGroup, |
869 | 882 | route: route |
870 | 883 | })]; |
871 | | - } |
| 884 | + } |
872 | 885 | } |
873 | 886 | } |
| 887 | + index += 1; |
874 | 888 | } |
875 | | - return []; |
| 889 | + return diffs; |
876 | 890 | }, |
877 | 891 |
|
878 | 892 | findValueDiff: function(t1, t2, route) { |
|
0 commit comments