|
15 | 15 | MODIFY_VALUE = 10, |
16 | 16 | MODIFY_CHECKED = 11, |
17 | 17 | MODIFY_SELECTED = 12, |
18 | | - ACTION = 13, |
19 | | - ROUTE = 14, |
20 | | - OLD_VALUE = 15, |
21 | | - NEW_VALUE = 16, |
22 | | - ELEMENT = 17, |
23 | | - GROUP = 18, |
24 | | - FROM = 19, |
25 | | - TO = 20, |
26 | | - NAME = 21, |
27 | | - VALUE = 22, |
28 | | - TEXT = 23, |
29 | | - ATTRIBUTES = 24, |
30 | | - NODE_NAME = 25, |
31 | | - COMMENT = 26, |
32 | | - CHILD_NODES = 27, |
33 | | - CHECKED = 28, |
34 | | - SELECTED = 29; |
35 | | - |
| 18 | + MODIFY_DATA = 13, |
| 19 | + ACTION = 14, |
| 20 | + ROUTE = 15, |
| 21 | + OLD_VALUE = 16, |
| 22 | + NEW_VALUE = 17, |
| 23 | + ELEMENT = 18, |
| 24 | + GROUP = 19, |
| 25 | + FROM = 20, |
| 26 | + TO = 21, |
| 27 | + NAME = 22, |
| 28 | + VALUE = 23, |
| 29 | + TEXT = 24, |
| 30 | + ATTRIBUTES = 25, |
| 31 | + NODE_NAME = 26, |
| 32 | + COMMENT = 27, |
| 33 | + CHILD_NODES = 28, |
| 34 | + CHECKED = 29, |
| 35 | + SELECTED = 30; |
36 | 36 |
|
37 | 37 | var Diff = function (options) { |
38 | 38 | var diff = this; |
|
93 | 93 | // Clone a node with contents and add values manually, |
94 | 94 | // to avoid https://bugzilla.mozilla.org/show_bug.cgi?id=230307 |
95 | 95 | var clonedNode = node.cloneNode(true), |
96 | | - textareas, clonedTextareas, i; |
| 96 | + textareas, clonedTextareas, options, clonedOptions, i; |
97 | 97 |
|
98 | 98 | if (node.nodeType != 8 && node.nodeType != 3) { |
99 | 99 |
|
|
107 | 107 | if (node.value && (node.value !== clonedNode.value)) { |
108 | 108 | clonedNode.value = node.value; |
109 | 109 | } |
| 110 | + options = node.querySelectorAll('option'); |
| 111 | + clonedOptions = clonedNode.querySelectorAll('option'); |
| 112 | + for (i = 0; i < options.length; i++) { |
| 113 | + if (options[i].selected && !(clonedOptions[i].selected)) { |
| 114 | + clonedOptions[i].selected = true; |
| 115 | + } else if (!(options[i].selected) && clonedOptions[i].selected) { |
| 116 | + clonedOptions[i].selected = false; |
| 117 | + } |
| 118 | + } |
110 | 119 | if (node.selected && !(clonedNode.selected)) { |
111 | 120 | clonedNode.selected = true; |
112 | 121 | } else if (!(node.selected) && clonedNode.selected) { |
|
502 | 511 | throw new Error("surpassed diffcap:" + JSON.stringify(this.t1Orig) + " -> " + JSON.stringify(this.t2Orig)); |
503 | 512 | } |
504 | 513 | } |
505 | | - |
506 | 514 | difflist = this.findFirstDiff(t1, t2, []); |
507 | 515 | if (difflist) { |
508 | 516 | if (!difflist.length) { |
|
549 | 557 | byName = function (a, b) { |
550 | 558 | return a.name > b.name; |
551 | 559 | }, |
552 | | - attr1 = slice.call(t1.attributes).sort(byName), |
553 | | - attr2 = slice.call(t2.attributes).sort(byName), |
| 560 | + attr1 = t1.attributes ? slice.call(t1.attributes).sort(byName) : [], |
| 561 | + attr2 = t2.attributes ? slice.call(t2.attributes).sort(byName) : [], |
554 | 562 | find = function (attr, list) { |
555 | 563 | for (var i = 0, last = list.length; i < last; i++) { |
556 | 564 | if (list[i].name === attr.name) |
|
559 | 567 | return -1; |
560 | 568 | }, |
561 | 569 | diffs = []; |
562 | | - |
563 | | - |
564 | 570 | if ((t1.value || t2.value) && t1.value !== t2.value && t1.nodeName !== 'OPTION') { |
565 | 571 | k = {}; |
566 | 572 | k[ACTION] = MODIFY_VALUE; |
|
600 | 606 | k[NEW_VALUE] = a2.value; |
601 | 607 |
|
602 | 608 | diffs.push(new Diff(k)); |
603 | | - |
| 609 | + // console.log(diffs); |
604 | 610 | } |
605 | 611 | }); |
| 612 | + if (!t1.attributes && t1.data !== t2.data) { |
| 613 | + k = {}; |
| 614 | + k[ACTION] = MODIFY_DATA; |
| 615 | + k[ROUTE] = route; |
| 616 | + k[OLD_VALUE] = t1.data; |
| 617 | + k[NEW_VALUE] = t2.data; |
| 618 | + diffs.push(new Diff(k)); |
| 619 | + } |
606 | 620 | if (diffs.length > 0) { |
607 | 621 | return diffs; |
608 | 622 | }; |
|
756 | 770 | if (!node || typeof node.value === 'undefined') |
757 | 771 | return false; |
758 | 772 | node.value = diff[NEW_VALUE]; |
| 773 | + } else if (diff[ACTION] === MODIFY_DATA) { |
| 774 | + if (!node || typeof node.data === 'undefined') |
| 775 | + return false; |
| 776 | + node.data = diff[NEW_VALUE]; |
759 | 777 | } else if (diff[ACTION] === MODIFY_CHECKED) { |
760 | 778 | if (!node || typeof node.checked === 'undefined') |
761 | 779 | return false; |
|
854 | 872 | } else if (diff[ACTION] === MODIFY_VALUE) { |
855 | 873 | swap(diff, OLD_VALUE, NEW_VALUE); |
856 | 874 | this.applyDiff(tree, diff); |
| 875 | + } else if (diff[ACTION] === MODIFY_DATA) { |
| 876 | + swap(diff, OLD_VALUE, NEW_VALUE); |
| 877 | + this.applyDiff(tree, diff); |
857 | 878 | } else if (diff[ACTION] === MODIFY_CHECKED) { |
858 | 879 | swap(diff, OLD_VALUE, NEW_VALUE); |
859 | 880 | this.applyDiff(tree, diff); |
|
0 commit comments