|
702 | 702 | diffs, i, last, e1, e2; |
703 | 703 |
|
704 | 704 | // 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 |
708 | 705 | if (mappings === 0) { |
| 706 | + // two text nodes with differences |
709 | 707 | if (t1.nodeName === '#text' && t2.nodeName === '#text' && t1.data !== t2.data) { |
710 | 708 | return [new Diff({ |
711 | 709 | action: 'modiFyTextElement', |
|
721 | 719 | for (i = 0; i < last; i += 1) { |
722 | 720 | e1 = t1_child_nodes[i]; |
723 | 721 | e2 = t2_child_nodes[i]; |
724 | | - // TODO: This is a similar code path to the one |
725 | | - // in findFirstInnerDiff. Can we unify these? |
| 722 | + |
726 | 723 | if (e1 && !e2) { |
727 | 724 | if (e1.nodeName === '#text') { |
728 | 725 | return [new Diff({ |
|
761 | 758 | } |
762 | 759 |
|
763 | 760 | // one or more differences: find first diff |
764 | | - return this.findFirstInnerDiff(t1, t2, subtrees, route); |
| 761 | + return this.attemptGroupRelocation(t1, t2, subtrees, route); |
765 | 762 | }, |
766 | 763 |
|
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 | + */ |
807 | 771 |
|
808 | 772 | var gapInformation = getGapInformation(t1, t2, subtrees), |
809 | 773 | gaps1 = gapInformation.gaps1, |
810 | | - gl1 = gaps1.length, |
811 | 774 | gaps2 = gapInformation.gaps2, |
812 | | - gl2 = gaps1.length, |
813 | 775 | destinationDifferent, toGroup, |
| 776 | + group, node, similarNode, testI, |
814 | 777 | i, j; |
815 | 778 |
|
816 | | - // Check for correct submap sequencing (irrespective of gaps) first: |
817 | | - var group, node, similarNode, testI, shortest = gl1 < gl2 ? gaps1 : gaps2; |
818 | | - |
819 | 779 | // group relocation |
820 | | - for (i = 0; i < shortest.length; i += 1) { |
| 780 | + for (i = 0; i < gaps1.length; i += 1) { |
821 | 781 | if (gaps1[i] === true) { |
822 | 782 | node = t1.childNodes[i]; |
823 | 783 | if (node.nodeName === '#text') { |
|
891 | 851 | } |
892 | 852 | return []; |
893 | 853 | }, |
| 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 | + |
894 | 890 | // ===== Apply a virtual diff ===== |
895 | 891 |
|
896 | 892 | applyVirtual: function(tree, diffs) { |
|
0 commit comments