|
254 | 254 | //return obj; |
255 | 255 | }; |
256 | 256 |
|
257 | | - var nodeToObj = function(node) { |
258 | | - var objNode = {}; |
259 | | - objNode.nodeName = node.nodeName; |
260 | | - if (objNode.nodeName === '#text' || objNode.nodeName === '#comment') { |
261 | | - objNode.data = node.data; |
262 | | - } else { |
263 | | - if (node.attributes && node.attributes.length > 0) { |
264 | | - objNode.attributes = {}; |
265 | | - Array.prototype.slice.call(node.attributes).forEach( |
266 | | - function(attribute) { |
267 | | - objNode.attributes[attribute.name] = attribute.value; |
268 | | - } |
269 | | - ); |
270 | | - } |
271 | | - if (node.childNodes && node.childNodes.length > 0) { |
272 | | - objNode.childNodes = []; |
273 | | - Array.prototype.slice.call(node.childNodes).forEach( |
274 | | - function(childNode) { |
275 | | - objNode.childNodes.push(nodeToObj(childNode)); |
276 | | - } |
277 | | - ); |
278 | | - } |
279 | | - if (node.value) { |
280 | | - objNode.value = node.value; |
281 | | - } |
282 | | - if (node.checked) { |
283 | | - objNode.checked = node.checked; |
284 | | - } |
285 | | - if (node.selected) { |
286 | | - objNode.selected = node.selected; |
287 | | - } |
288 | | - } |
289 | | - |
290 | | - return objNode; |
291 | | - }; |
292 | | - |
293 | | - |
294 | | - var objToNode = function(objNode, insideSvg) { |
295 | | - var node; |
296 | | - if (objNode.nodeName === '#text') { |
297 | | - node = document.createTextNode(objNode.data); |
298 | | - |
299 | | - } else if (objNode.nodeName === '#comment') { |
300 | | - node = document.createComment(objNode.data); |
301 | | - } else { |
302 | | - if (objNode.nodeName === 'svg' || insideSvg) { |
303 | | - node = document.createElementNS('http://www.w3.org/2000/svg', objNode.nodeName); |
304 | | - insideSvg = true; |
305 | | - } else { |
306 | | - node = document.createElement(objNode.nodeName); |
307 | | - } |
308 | | - if (objNode.attributes) { |
309 | | - Object.keys(objNode.attributes).forEach(function(attribute) { |
310 | | - node.setAttribute(attribute, objNode.attributes[attribute]); |
311 | | - }); |
312 | | - } |
313 | | - if (objNode.childNodes) { |
314 | | - objNode.childNodes.forEach(function(childNode) { |
315 | | - node.appendChild(objToNode(childNode, insideSvg)); |
316 | | - }); |
317 | | - } |
318 | | - if (objNode.value) { |
319 | | - node.value = objNode.value; |
320 | | - } |
321 | | - if (objNode.checked) { |
322 | | - node.checked = objNode.checked; |
323 | | - } |
324 | | - if (objNode.selected) { |
325 | | - node.selected = objNode.selected; |
326 | | - } |
327 | | - } |
328 | | - return node; |
329 | | - }; |
330 | | - |
331 | | - |
332 | | - |
333 | 257 | /** |
334 | 258 | * based on https://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring#JavaScript |
335 | 259 | */ |
|
537 | 461 |
|
538 | 462 | diff: function(t1Node, t2Node) { |
539 | 463 |
|
540 | | - var t1 = nodeToObj(t1Node), |
541 | | - t2 = nodeToObj(t2Node); |
| 464 | + var t1 = this.nodeToObj(t1Node), |
| 465 | + t2 = this.nodeToObj(t2Node); |
542 | 466 |
|
543 | 467 | diffcount = 0; |
544 | 468 |
|
545 | 469 | if (this.debug) { |
546 | | - this.t1Orig = nodeToObj(t1Node); |
547 | | - this.t2Orig = nodeToObj(t2Node); |
| 470 | + this.t1Orig = this.nodeToObj(t1Node); |
| 471 | + this.t2Orig = this.nodeToObj(t2Node); |
548 | 472 | } |
549 | 473 |
|
550 | 474 | this.tracker = new DiffTracker(); |
|
694 | 618 |
|
695 | 619 | return diffs; |
696 | 620 | }, |
| 621 | + nodeToObj: function(node) { |
| 622 | + var objNode = {}, dobj = this; |
| 623 | + objNode.nodeName = node.nodeName; |
| 624 | + if (objNode.nodeName === '#text' || objNode.nodeName === '#comment') { |
| 625 | + objNode.data = node.data; |
| 626 | + } else { |
| 627 | + if (node.attributes && node.attributes.length > 0) { |
| 628 | + objNode.attributes = {}; |
| 629 | + Array.prototype.slice.call(node.attributes).forEach( |
| 630 | + function(attribute) { |
| 631 | + objNode.attributes[attribute.name] = attribute.value; |
| 632 | + } |
| 633 | + ); |
| 634 | + } |
| 635 | + if (node.childNodes && node.childNodes.length > 0) { |
| 636 | + objNode.childNodes = []; |
| 637 | + Array.prototype.slice.call(node.childNodes).forEach( |
| 638 | + function(childNode) { |
| 639 | + objNode.childNodes.push(dobj.nodeToObj(childNode)); |
| 640 | + } |
| 641 | + ); |
| 642 | + } |
| 643 | + if (this.valueDiffing) { |
| 644 | + if (node.value) { |
| 645 | + objNode.value = node.value; |
| 646 | + } |
| 647 | + if (node.checked) { |
| 648 | + objNode.checked = node.checked; |
| 649 | + } |
| 650 | + if (node.selected) { |
| 651 | + objNode.selected = node.selected; |
| 652 | + } |
| 653 | + } |
| 654 | + } |
| 655 | + |
| 656 | + return objNode; |
| 657 | + }, |
| 658 | + objToNode: function(objNode, insideSvg) { |
| 659 | + var node, dobj = this; |
| 660 | + if (objNode.nodeName === '#text') { |
| 661 | + node = document.createTextNode(objNode.data); |
| 662 | + |
| 663 | + } else if (objNode.nodeName === '#comment') { |
| 664 | + node = document.createComment(objNode.data); |
| 665 | + } else { |
| 666 | + if (objNode.nodeName === 'svg' || insideSvg) { |
| 667 | + node = document.createElementNS('http://www.w3.org/2000/svg', objNode.nodeName); |
| 668 | + insideSvg = true; |
| 669 | + } else { |
| 670 | + node = document.createElement(objNode.nodeName); |
| 671 | + } |
| 672 | + if (objNode.attributes) { |
| 673 | + Object.keys(objNode.attributes).forEach(function(attribute) { |
| 674 | + node.setAttribute(attribute, objNode.attributes[attribute]); |
| 675 | + }); |
| 676 | + } |
| 677 | + if (objNode.childNodes) { |
| 678 | + objNode.childNodes.forEach(function(childNode) { |
| 679 | + node.appendChild(dobj.objToNode(childNode, insideSvg)); |
| 680 | + }); |
| 681 | + } |
| 682 | + if (this.valueDiffing) { |
| 683 | + if (objNode.value) { |
| 684 | + node.value = objNode.value; |
| 685 | + } |
| 686 | + if (objNode.checked) { |
| 687 | + node.checked = objNode.checked; |
| 688 | + } |
| 689 | + if (objNode.selected) { |
| 690 | + node.selected = objNode.selected; |
| 691 | + } |
| 692 | + } |
| 693 | + } |
| 694 | + return node; |
| 695 | + }, |
697 | 696 | findInnerDiff: function(t1, t2, route) { |
698 | 697 |
|
699 | 698 | var subtrees = (t1.childNodes && t2.childNodes) ? markSubTrees(t1, t2) : [], |
|
933 | 932 | diffs.forEach(function(diff) { |
934 | 933 | // console.log(JSON.stringify(diff)); |
935 | 934 | // console.log(JSON.stringify(tree)); |
936 | | - // console.log(objToNode(tree).outerHTML); |
| 935 | + // console.log(this.objToNode(tree).outerHTML); |
937 | 936 | dobj.applyVirtualDiff(tree, diff); |
938 | 937 | // console.log(JSON.stringify(tree)); |
939 | | - // console.log(objToNode(tree).outerHTML); |
| 938 | + // console.log(this.objToNode(tree).outerHTML); |
940 | 939 | }); |
941 | 940 | return true; |
942 | 941 | }, |
|
1177 | 1176 | node.selected = diff.newValue; |
1178 | 1177 | break; |
1179 | 1178 | case 'replaceElement': |
1180 | | - node.parentNode.replaceChild(objToNode(diff.newValue), node); |
| 1179 | + node.parentNode.replaceChild(this.objToNode(diff.newValue), node); |
1181 | 1180 | break; |
1182 | 1181 | case 'relocateGroup': |
1183 | 1182 | Array.apply(null, new Array(diff.groupLength)).map(function() { |
|
1196 | 1195 | route = diff.route.slice(); |
1197 | 1196 | c = route.splice(route.length - 1, 1)[0]; |
1198 | 1197 | node = this.getFromRoute(tree, route); |
1199 | | - node.insertBefore(objToNode(diff.element), node.childNodes[c]); |
| 1198 | + node.insertBefore(this.objToNode(diff.element), node.childNodes[c]); |
1200 | 1199 | break; |
1201 | 1200 | case 'removeTextElement': |
1202 | 1201 | if (!node || node.nodeType !== 3) { |
|
0 commit comments