Comparing version 4.2.0 to 4.2.1
@@ -1,2 +0,2 @@ | ||
"use strict";function objToNode(e,t,o){var n;return"#text"===e.nodeName?n=o.document.createTextNode(e.data):"#comment"===e.nodeName?n=o.document.createComment(e.data):("svg"===e.nodeName.toLowerCase()||t?(n=o.document.createElementNS("http://www.w3.org/2000/svg",e.nodeName.toLowerCase()),t=!0):n=o.document.createElement(e.nodeName),e.attributes&&Object.entries(e.attributes).forEach(function(e){var t=e[0],o=e[1];return n.setAttribute(t,o)}),e.childNodes&&e.childNodes.forEach(function(e){return n.appendChild(objToNode(e,t,o))}),o.valueDiffing&&(e.value&&(n.value=e.value),e.checked&&(n.checked=e.checked),e.selected&&(n.selected=e.selected))),n}function getFromRoute(e,t){for(t=t.slice();t.length>0;){if(!e.childNodes)return!1;var o=t.splice(0,1)[0];e=e.childNodes[o]}return e}function applyDiff(e,t,o){var n,s,i,a,l=getFromRoute(e,t[o._const.route]),r={diff:t,node:l};if(o.preDiffApply(r))return!0;switch(t[o._const.action]){case o._const.addAttribute:if(!l||!l.setAttribute)return!1;l.setAttribute(t[o._const.name],t[o._const.value]);break;case o._const.modifyAttribute:if(!l||!l.setAttribute)return!1;l.setAttribute(t[o._const.name],t[o._const.newValue]),"INPUT"===l.nodeName&&"value"===t[o._const.name]&&(l.value=t[o._const.newValue]);break;case o._const.removeAttribute:if(!l||!l.removeAttribute)return!1;l.removeAttribute(t[o._const.name]);break;case o._const.modifyTextElement:if(!l||3!==l.nodeType)return!1;o.textDiff(l,l.data,t[o._const.oldValue],t[o._const.newValue]);break;case o._const.modifyValue:if(!l||void 0===l.value)return!1;l.value=t[o._const.newValue];break;case o._const.modifyComment:if(!l||void 0===l.data)return!1;o.textDiff(l,l.data,t[o._const.oldValue],t[o._const.newValue]);break;case o._const.modifyChecked:if(!l||void 0===l.checked)return!1;l.checked=t[o._const.newValue];break;case o._const.modifySelected:if(!l||void 0===l.selected)return!1;l.selected=t[o._const.newValue];break;case o._const.replaceElement:l.parentNode.replaceChild(objToNode(t[o._const.newValue],"http://www.w3.org/2000/svg"===l.namespaceURI,o),l);break;case o._const.relocateGroup:Array.apply(void 0,new Array(t.groupLength)).map(function(){return l.removeChild(l.childNodes[t[o._const.from]])}).forEach(function(e,n){0===n&&(s=l.childNodes[t[o._const.to]]),l.insertBefore(e,s||null)});break;case o._const.removeElement:l.parentNode.removeChild(l);break;case o._const.addElement:a=(i=t[o._const.route].slice()).splice(i.length-1,1)[0],(l=getFromRoute(e,i)).insertBefore(objToNode(t[o._const.element],"http://www.w3.org/2000/svg"===l.namespaceURI,o),l.childNodes[a]||null);break;case o._const.removeTextElement:if(!l||3!==l.nodeType)return!1;l.parentNode.removeChild(l);break;case o._const.addTextElement:if(a=(i=t[o._const.route].slice()).splice(i.length-1,1)[0],n=o.document.createTextNode(t[o._const.value]),!(l=getFromRoute(e,i))||!l.childNodes)return!1;l.insertBefore(n,l.childNodes[a]||null);break;default:console.log("unknown action")}return r.newNode=n,o.postDiffApply(r),!0}function applyDOM(e,t,o){return t.every(function(t){return applyDiff(e,t,o)})}function swap(e,t,o){var n=e[t];e[t]=e[o],e[o]=n}function undoDiff(e,t,o){switch(t[o._const.action]){case o._const.addAttribute:t[o._const.action]=o._const.removeAttribute,applyDiff(e,t,o);break;case o._const.modifyAttribute:swap(t,o._const.oldValue,o._const.newValue),applyDiff(e,t,o);break;case o._const.removeAttribute:t[o._const.action]=o._const.addAttribute,applyDiff(e,t,o);break;case o._const.modifyTextElement:case o._const.modifyValue:case o._const.modifyComment:case o._const.modifyChecked:case o._const.modifySelected:case o._const.replaceElement:swap(t,o._const.oldValue,o._const.newValue),applyDiff(e,t,o);break;case o._const.relocateGroup:swap(t,o._const.from,o._const.to),applyDiff(e,t,o);break;case o._const.removeElement:t[o._const.action]=o._const.addElement,applyDiff(e,t,o);break;case o._const.addElement:t[o._const.action]=o._const.removeElement,applyDiff(e,t,o);break;case o._const.removeTextElement:t[o._const.action]=o._const.addTextElement,applyDiff(e,t,o);break;case o._const.addTextElement:t[o._const.action]=o._const.removeTextElement,applyDiff(e,t,o);break;default:console.log("unknown action")}}function undoDOM(e,t,o){t.length||(t=[t]),(t=t.slice()).reverse(),t.forEach(function(t){undoDiff(e,t,o)})}Object.defineProperty(exports,"__esModule",{value:!0});var Diff=function(e){var t=this;void 0===e&&(e={}),Object.entries(e).forEach(function(e){var o=e[0],n=e[1];return t[o]=n})};function elementDescriptors(e){var t=[];return"#text"!==e.nodeName&&"#comment"!==e.nodeName&&(t.push(e.nodeName),e.attributes&&(e.attributes.class&&t.push(e.nodeName+"."+e.attributes.class.replace(/ /g,".")),e.attributes.id&&t.push(e.nodeName+"#"+e.attributes.id))),t}function findUniqueDescriptors(e){var t={},o={};return e.forEach(function(e){elementDescriptors(e).forEach(function(e){var n=e in t;n||e in o?n&&(delete t[e],o[e]=!0):t[e]=!0})}),t}function uniqueInBoth(e,t){var o=findUniqueDescriptors(e),n=findUniqueDescriptors(t),s={};return Object.keys(o).forEach(function(e){n[e]&&(s[e]=!0)}),s}function removeDone(e){return delete e.outerDone,delete e.innerDone,delete e.valueDone,!e.childNodes||e.childNodes.every(removeDone)}function isEqual(e,t){if(!["nodeName","value","checked","selected","data"].every(function(o){return e[o]===t[o]}))return!1;if(Boolean(e.attributes)!==Boolean(t.attributes))return!1;if(Boolean(e.childNodes)!==Boolean(t.childNodes))return!1;if(e.attributes){var o=Object.keys(e.attributes),n=Object.keys(t.attributes);if(o.length!==n.length)return!1;if(!o.every(function(o){return e.attributes[o]===t.attributes[o]}))return!1}if(e.childNodes){if(e.childNodes.length!==t.childNodes.length)return!1;if(!e.childNodes.every(function(e,o){return isEqual(e,t.childNodes[o])}))return!1}return!0}function roughlyEqual(e,t,o,n,s){if(!e||!t)return!1;if(e.nodeName!==t.nodeName)return!1;if("#text"===e.nodeName)return!!s||e.data===t.data;if(e.nodeName in o)return!0;if(e.attributes&&t.attributes){if(e.attributes.id){if(e.attributes.id!==t.attributes.id)return!1;if(e.nodeName+"#"+e.attributes.id in o)return!0}if(e.attributes.class&&e.attributes.class===t.attributes.class)if(e.nodeName+"."+e.attributes.class.replace(/ /g,".")in o)return!0}if(n)return!0;var i=e.childNodes?e.childNodes.slice().reverse():[],a=t.childNodes?t.childNodes.slice().reverse():[];if(i.length!==a.length)return!1;if(s)return i.every(function(e,t){return e.nodeName===a[t].nodeName});var l=uniqueInBoth(i,a);return i.every(function(e,t){return roughlyEqual(e,a[t],l,!0,!0)})}function cloneObj(e){return JSON.parse(JSON.stringify(e))}function findCommonSubsets(e,t,o,n){var s=0,i=[],a=e.length,l=t.length,r=Array.apply(void 0,new Array(a+1)).map(function(){return[]}),c=uniqueInBoth(e,t),u=a===l;u&&e.some(function(e,o){var n=elementDescriptors(e),s=elementDescriptors(t[o]);return n.length!==s.length?(u=!1,!0):(n.some(function(e,t){if(e!==s[t])return u=!1,!0}),!u||void 0)});for(var d=0;d<a;d++)for(var f=e[d],h=0;h<l;h++){var p=t[h];o[d]||n[h]||!roughlyEqual(f,p,c,u)?r[d+1][h+1]=0:(r[d+1][h+1]=r[d][h]?r[d][h]+1:1,r[d+1][h+1]>=s&&(s=r[d+1][h+1],i=[d+1,h+1]))}return 0!==s&&{oldValue:i[0]-s,newValue:i[1]-s,length:s}}function makeArray(e,t){return Array.apply(void 0,new Array(e)).map(function(){return t})}function getGapInformation(e,t,o){var n=e.childNodes?makeArray(e.childNodes.length,!0):[],s=t.childNodes?makeArray(t.childNodes.length,!0):[],i=0;return o.forEach(function(e){for(var t=e.oldValue+e.length,o=e.newValue+e.length,a=e.oldValue;a<t;a+=1)n[a]=i;for(var l=e.newValue;l<o;l+=1)s[l]=i;i+=1}),{gaps1:n,gaps2:s}}function markSubTrees(e,t){for(var o=e.childNodes?e.childNodes:[],n=t.childNodes?t.childNodes:[],s=makeArray(o.length,!1),i=makeArray(n.length,!1),a=[],l=!0,r=function(){return arguments[1]};l;){if(l=findCommonSubsets(o,n,s,i))a.push(l),Array.apply(void 0,new Array(l.length)).map(r).forEach(function(e){return t=e,s[l.oldValue+t]=!0,void(i[l.newValue+t]=!0);var t})}return e.subsets=a,e.subsetsAge=100,a}Diff.prototype.toString=function(){return JSON.stringify(this)},Diff.prototype.setValue=function(e,t){return this[e]=t,this};var DiffTracker=function(){this.list=[]};function getFromVirtualRoute(e,t){var o,n,s=e;for(t=t.slice();t.length>0;){if(!s.childNodes)return!1;n=t.splice(0,1)[0],o=s,s=s.childNodes[n]}return{node:s,parentNode:o,nodeIndex:n}}function applyVirtualDiff(e,t,o){var n,s,i,a=getFromVirtualRoute(e,t[o._const.route]),l=a.node,r=a.parentNode,c=a.nodeIndex,u=[],d={diff:t,node:l};if(o.preVirtualDiffApply(d))return!0;switch(t[o._const.action]){case o._const.addAttribute:l.attributes||(l.attributes={}),l.attributes[t[o._const.name]]=t[o._const.value],"checked"===t[o._const.name]?l.checked=!0:"selected"===t[o._const.name]?l.selected=!0:"INPUT"===l.nodeName&&"value"===t[o._const.name]&&(l.value=t[o._const.value]);break;case o._const.modifyAttribute:l.attributes[t[o._const.name]]=t[o._const.newValue];break;case o._const.removeAttribute:delete l.attributes[t[o._const.name]],0===Object.keys(l.attributes).length&&delete l.attributes,"checked"===t[o._const.name]?l.checked=!1:"selected"===t[o._const.name]?delete l.selected:"INPUT"===l.nodeName&&"value"===t[o._const.name]&&delete l.value;break;case o._const.modifyTextElement:l.data=t[o._const.newValue];break;case o._const.modifyValue:l.value=t[o._const.newValue];break;case o._const.modifyComment:l.data=t[o._const.newValue];break;case o._const.modifyChecked:l.checked=t[o._const.newValue];break;case o._const.modifySelected:l.selected=t[o._const.newValue];break;case o._const.replaceElement:(n=cloneObj(t[o._const.newValue])).outerDone=!0,n.innerDone=!0,n.valueDone=!0,r.childNodes[c]=n;break;case o._const.relocateGroup:l.childNodes.splice(t[o._const.from],t.groupLength).reverse().forEach(function(e){return l.childNodes.splice(t[o._const.to],0,e)}),l.subsets&&l.subsets.forEach(function(e){if(t[o._const.from]<t[o._const.to]&&e.oldValue<=t[o._const.to]&&e.oldValue>t[o._const.from]){e.oldValue-=t.groupLength;var n=e.oldValue+e.length-t[o._const.to];n>0&&(u.push({oldValue:t[o._const.to]+t.groupLength,newValue:e.newValue+e.length-n,length:n}),e.length-=n)}else if(t[o._const.from]>t[o._const.to]&&e.oldValue>t[o._const.to]&&e.oldValue<t[o._const.from]){e.oldValue+=t.groupLength;var s=e.oldValue+e.length-t[o._const.to];s>0&&(u.push({oldValue:t[o._const.to]+t.groupLength,newValue:e.newValue+e.length-s,length:s}),e.length-=s)}else e.oldValue===t[o._const.from]&&(e.oldValue=t[o._const.to])});break;case o._const.removeElement:r.childNodes.splice(c,1),r.subsets&&r.subsets.forEach(function(e){e.oldValue>c?e.oldValue-=1:e.oldValue===c?e.delete=!0:e.oldValue<c&&e.oldValue+e.length>c&&(e.oldValue+e.length-1===c?e.length--:(u.push({newValue:e.newValue+c-e.oldValue,oldValue:c,length:e.length-c+e.oldValue-1}),e.length=c-e.oldValue))}),l=r;break;case o._const.addElement:s=t[o._const.route].slice(),i=s.splice(s.length-1,1)[0],l=getFromVirtualRoute(e,s).node,(n=cloneObj(t[o._const.element])).outerDone=!0,n.innerDone=!0,n.valueDone=!0,l.childNodes||(l.childNodes=[]),i>=l.childNodes.length?l.childNodes.push(n):l.childNodes.splice(i,0,n),l.subsets&&l.subsets.forEach(function(e){if(e.oldValue>=i)e.oldValue+=1;else if(e.oldValue<i&&e.oldValue+e.length>i){var t=e.oldValue+e.length-i;u.push({newValue:e.newValue+e.length-t,oldValue:i+1,length:t}),e.length-=t}});break;case o._const.removeTextElement:r.childNodes.splice(c,1),"TEXTAREA"===r.nodeName&&delete r.value,r.subsets&&r.subsets.forEach(function(e){e.oldValue>c?e.oldValue-=1:e.oldValue===c?e.delete=!0:e.oldValue<c&&e.oldValue+e.length>c&&(e.oldValue+e.length-1===c?e.length--:(u.push({newValue:e.newValue+c-e.oldValue,oldValue:c,length:e.length-c+e.oldValue-1}),e.length=c-e.oldValue))}),l=r;break;case o._const.addTextElement:s=t[o._const.route].slice(),i=s.splice(s.length-1,1)[0],(n={}).nodeName="#text",n.data=t[o._const.value],(l=getFromVirtualRoute(e,s).node).childNodes||(l.childNodes=[]),i>=l.childNodes.length?l.childNodes.push(n):l.childNodes.splice(i,0,n),"TEXTAREA"===l.nodeName&&(l.value=t[o._const.newValue]),l.subsets&&l.subsets.forEach(function(e){if(e.oldValue>=i&&(e.oldValue+=1),e.oldValue<i&&e.oldValue+e.length>i){var t=e.oldValue+e.length-i;u.push({newValue:e.newValue+e.length-t,oldValue:i+1,length:t}),e.length-=t}});break;default:console.log("unknown action")}l.subsets&&(l.subsets=l.subsets.filter(function(e){return!e.delete&&e.oldValue!==e.newValue}),u.length&&(l.subsets=l.subsets.concat(u))),d.newNode=n,o.postVirtualDiffApply(d)}function applyVirtual(e,t,o){return t.forEach(function(t){applyVirtualDiff(e,t,o)}),!0}function nodeToObj(e,t){void 0===t&&(t={});var o={};if(o.nodeName=e.nodeName,"#text"===o.nodeName||"#comment"===o.nodeName)o.data=e.data;else{if(e.attributes&&e.attributes.length>0)o.attributes={},Array.prototype.slice.call(e.attributes).forEach(function(e){return o.attributes[e.name]=e.value});if("TEXTAREA"===o.nodeName)o.value=e.value;else if(e.childNodes&&e.childNodes.length>0){o.childNodes=[],Array.prototype.slice.call(e.childNodes).forEach(function(e){return o.childNodes.push(nodeToObj(e,t))})}t.valueDiffing&&(void 0!==e.checked&&e.type&&["radio","checkbox"].includes(e.type.toLowerCase())?o.checked=e.checked:void 0!==e.value&&(o.value=e.value),void 0!==e.selected&&(o.selected=e.selected))}return o}DiffTracker.prototype.add=function(e){var t;(t=this.list).push.apply(t,e)},DiffTracker.prototype.forEach=function(e){this.list.forEach(function(t){return e(t)})};var tagRE=/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g,empty=Object.create?Object.create(null):{},attrRE=/\s([^'"\/\s><]+?)[\s\/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;function unescape(e){return e.replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&")}var lookup={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuItem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0};function parseTag(e){var t={nodeName:"",attributes:{}},o=e.match(/<\/?([^\s]+?)[\/\s>]/);o&&(t.nodeName=o[1].toUpperCase(),(lookup[o[1].toLowerCase()]||"/"===e.charAt(e.length-2))&&(t.voidElement=!0));for(var n=new RegExp(attrRE),s=null,i=!1;!i;)if(null===(s=n.exec(e)))i=!0;else if(s[0].trim())if(s[1]){var a=s[1].trim(),l=[a,""];a.indexOf("=")>-1&&(l=a.split("=")),t.attributes[l[0]]=l[1],n.lastIndex--}else s[2]&&(t.attributes[s[2]]=s[3].trim().substring(1,s[3].length-1));return t}function parse(e,t){void 0===t&&(t={components:empty});var o,n=[],s=-1,i=[],a={},l=!1;return e.replace(tagRE,function(r,c){if(l){if(r!=="</"+o.nodeName+">")return;l=!1}var u,d="/"!==r.charAt(1),f=c+r.length,h=e.charAt(f);if(d&&(s++,"tag"===(o=parseTag(r)).type&&t.components[o.nodeName]&&(o.type="component",l=!0),o.voidElement||l||!h||"<"===h||(o.childNodes||(o.childNodes=[]),o.childNodes.push({nodeName:"#text",data:unescape(e.slice(f,e.indexOf("<",f)))})),a[o.tagName]=o,0===s&&n.push(o),(u=i[s-1])&&(u.childNodes||(u.childNodes=[]),u.childNodes.push(o)),i[s]=o),(!d||o.voidElement)&&(s--,!l&&"<"!==h&&h)){u=-1===s?n:i[s].childNodes||[];var p=e.indexOf("<",f),m=unescape(e.slice(f,-1===p?void 0:p));u.push({nodeName:"#text",data:m})}}),n[0]}function cleanObj(e){return delete e.voidElement,e.childNodes&&e.childNodes.forEach(function(e){return cleanObj(e)}),e}function stringToObj(e){return cleanObj(parse(e))}var DiffFinder=function(e,t,o){this.options=o,this.t1=e instanceof HTMLElement?nodeToObj(e,this.options):"string"==typeof e?stringToObj(e,this.options):JSON.parse(JSON.stringify(e)),this.t2=t instanceof HTMLElement?nodeToObj(t,this.options):"string"==typeof t?stringToObj(t,this.options):JSON.parse(JSON.stringify(t)),this.diffcount=0,this.foundAll=!1,this.debug&&(this.t1Orig=nodeToObj(e,this.options),this.t2Orig=nodeToObj(t,this.options)),this.tracker=new DiffTracker};DiffFinder.prototype.init=function(){return this.findDiffs(this.t1,this.t2)},DiffFinder.prototype.findDiffs=function(e,t){var o;do{if(this.options.debug&&(this.diffcount+=1,this.diffcount>this.options.diffcap))throw window.diffError=[this.t1Orig,this.t2Orig],new Error("surpassed diffcap:"+JSON.stringify(this.t1Orig)+" -> "+JSON.stringify(this.t2Orig));0===(o=this.findNextDiff(e,t,[])).length&&(isEqual(e,t)||(this.foundAll?console.error("Could not find remaining diffs!"):(this.foundAll=!0,removeDone(e),o=this.findNextDiff(e,t,[])))),o.length>0&&(this.foundAll=!1,this.tracker.add(o),applyVirtual(e,o,this.options))}while(o.length>0);return this.tracker.list},DiffFinder.prototype.findNextDiff=function(e,t,o){var n,s;if(this.options.maxDepth&&o.length>this.options.maxDepth)return[];if(!e.outerDone){if(n=this.findOuterDiff(e,t,o),this.options.filterOuterDiff&&(s=this.options.filterOuterDiff(e,t,n))&&(n=s),n.length>0)return e.outerDone=!0,n;e.outerDone=!0}if(!e.innerDone){if((n=this.findInnerDiff(e,t,o)).length>0)return n;e.innerDone=!0}if(this.options.valueDiffing&&!e.valueDone){if((n=this.findValueDiff(e,t,o)).length>0)return e.valueDone=!0,n;e.valueDone=!0}return[]},DiffFinder.prototype.findOuterDiff=function(e,t,o){var n,s,i,a,l,r,c=[];if(e.nodeName!==t.nodeName){if(!o.length)throw new Error("Top level nodes have to be of the same kind.");return[(new Diff).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,cloneObj(e)).setValue(this.options._const.newValue,cloneObj(t)).setValue(this.options._const.route,o)]}if(o.length&&this.options.maxNodeDiffCount<Math.abs((e.childNodes||[]).length-(t.childNodes||[]).length))return[(new Diff).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,cloneObj(e)).setValue(this.options._const.newValue,cloneObj(t)).setValue(this.options._const.route,o)];if(e.data!==t.data)return"#text"===e.nodeName?[(new Diff).setValue(this.options._const.action,this.options._const.modifyTextElement).setValue(this.options._const.route,o).setValue(this.options._const.oldValue,e.data).setValue(this.options._const.newValue,t.data)]:[(new Diff).setValue(this.options._const.action,this.options._const.modifyComment).setValue(this.options._const.route,o).setValue(this.options._const.oldValue,e.data).setValue(this.options._const.newValue,t.data)];for(s=e.attributes?Object.keys(e.attributes).sort():[],i=t.attributes?Object.keys(t.attributes).sort():[],a=s.length,r=0;r<a;r++)n=s[r],-1===(l=i.indexOf(n))?c.push((new Diff).setValue(this.options._const.action,this.options._const.removeAttribute).setValue(this.options._const.route,o).setValue(this.options._const.name,n).setValue(this.options._const.value,e.attributes[n])):(i.splice(l,1),e.attributes[n]!==t.attributes[n]&&c.push((new Diff).setValue(this.options._const.action,this.options._const.modifyAttribute).setValue(this.options._const.route,o).setValue(this.options._const.name,n).setValue(this.options._const.oldValue,e.attributes[n]).setValue(this.options._const.newValue,t.attributes[n])));for(a=i.length,r=0;r<a;r++)n=i[r],c.push((new Diff).setValue(this.options._const.action,this.options._const.addAttribute).setValue(this.options._const.route,o).setValue(this.options._const.name,n).setValue(this.options._const.value,t.attributes[n]));return c},DiffFinder.prototype.findInnerDiff=function(e,t,o){var n=e.childNodes?e.childNodes.slice():[],s=t.childNodes?t.childNodes.slice():[],i=Math.max(n.length,s.length),a=Math.abs(n.length-s.length),l=[],r=0;if(!this.options.maxChildCount||i<this.options.maxChildCount){var c=e.subsets&&e.subsetsAge--?e.subsets:e.childNodes&&t.childNodes?markSubTrees(e,t):[];if(c.length>0&&(l=this.attemptGroupRelocation(e,t,c,o)).length>0)return l}for(var u=0;u<i;u+=1){var d=n[u],f=s[u];a&&(d&&!f?"#text"===d.nodeName?(l.push((new Diff).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,o.concat(r)).setValue(this.options._const.value,d.data)),r-=1):(l.push((new Diff).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.route,o.concat(r)).setValue(this.options._const.element,cloneObj(d))),r-=1):f&&!d&&("#text"===f.nodeName?l.push((new Diff).setValue(this.options._const.action,this.options._const.addTextElement).setValue(this.options._const.route,o.concat(r)).setValue(this.options._const.value,f.data)):l.push((new Diff).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.route,o.concat(r)).setValue(this.options._const.element,cloneObj(f))))),d&&f&&(!this.options.maxChildCount||i<this.options.maxChildCount?l=l.concat(this.findNextDiff(d,f,o.concat(r))):isEqual(d,f)||(n.length>s.length?(l=l.concat([(new Diff).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.element,cloneObj(d)).setValue(this.options._const.route,o.concat(r))]),n.splice(u,1),r-=1,a-=1):n.length<s.length?(l=l.concat([(new Diff).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.element,cloneObj(f)).setValue(this.options._const.route,o.concat(r))]),n.splice(u,0,{}),a-=1):l=l.concat([(new Diff).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,cloneObj(d)).setValue(this.options._const.newValue,cloneObj(f)).setValue(this.options._const.route,o.concat(r))]))),r+=1}return e.innerDone=!0,l},DiffFinder.prototype.attemptGroupRelocation=function(e,t,o,n){for(var s,i,a,l,r,c,u=getGapInformation(e,t,o),d=u.gaps1,f=u.gaps2,h=Math.min(d.length,f.length),p=[],m=0,g=0;m<h;g+=1,m+=1)if(!0===d[m])if("#text"===(l=e.childNodes[g]).nodeName){if("#text"===t.childNodes[m].nodeName&&l.data!==t.childNodes[m].data){for(c=g;e.childNodes.length>c+1&&"#text"===e.childNodes[c+1].nodeName;)if(c+=1,t.childNodes[m].data===e.childNodes[c].data){r=!0;break}if(!r)return p.push((new Diff).setValue(this.options._const.action,this.options._const.modifyTextElement).setValue(this.options._const.route,n.concat(m)).setValue(this.options._const.oldValue,l.data).setValue(this.options._const.newValue,t.childNodes[m].data)),p}p.push((new Diff).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,n.concat(m)).setValue(this.options._const.value,l.data)),d.splice(m,1),h=Math.min(d.length,f.length),m-=1}else p.push((new Diff).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.route,n.concat(m)).setValue(this.options._const.element,cloneObj(l))),d.splice(m,1),h=Math.min(d.length,f.length),m-=1;else if(!0===f[m])"#text"===(l=t.childNodes[m]).nodeName?(p.push((new Diff).setValue(this.options._const.action,this.options._const.addTextElement).setValue(this.options._const.route,n.concat(m)).setValue(this.options._const.value,l.data)),d.splice(m,0,!0),h=Math.min(d.length,f.length),g-=1):(p.push((new Diff).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.route,n.concat(m)).setValue(this.options._const.element,cloneObj(l))),d.splice(m,0,!0),h=Math.min(d.length,f.length),g-=1);else if(d[m]!==f[m]){if(p.length>0)return p;if(a=o[d[m]],(i=Math.min(a.newValue,e.childNodes.length-a.length))!==a.oldValue){s=!1;for(var _=0;_<a.length;_+=1)roughlyEqual(e.childNodes[i+_],e.childNodes[a.oldValue+_],[],!1,!0)||(s=!0);if(s)return[(new Diff).setValue(this.options._const.action,this.options._const.relocateGroup).setValue("groupLength",a.length).setValue(this.options._const.from,a.oldValue).setValue(this.options._const.to,i).setValue(this.options._const.route,n)]}}return p},DiffFinder.prototype.findValueDiff=function(e,t,o){var n=[];return e.selected!==t.selected&&n.push((new Diff).setValue(this.options._const.action,this.options._const.modifySelected).setValue(this.options._const.oldValue,e.selected).setValue(this.options._const.newValue,t.selected).setValue(this.options._const.route,o)),(e.value||t.value)&&e.value!==t.value&&"OPTION"!==e.nodeName&&n.push((new Diff).setValue(this.options._const.action,this.options._const.modifyValue).setValue(this.options._const.oldValue,e.value||"").setValue(this.options._const.newValue,t.value||"").setValue(this.options._const.route,o)),e.checked!==t.checked&&n.push((new Diff).setValue(this.options._const.action,this.options._const.modifyChecked).setValue(this.options._const.oldValue,e.checked).setValue(this.options._const.newValue,t.checked).setValue(this.options._const.route,o)),n};var DEFAULT_OPTIONS={debug:!1,diffcap:10,maxDepth:!1,maxChildCount:50,valueDiffing:!0,textDiff:function(e,t,o,n){e.data=n},preVirtualDiffApply:function(){},postVirtualDiffApply:function(){},preDiffApply:function(){},postDiffApply:function(){},filterOuterDiff:null,compress:!1,_const:!1,document:!(!window||!window.document)&&window.document},DiffDOM=function(e){var t=this;if(void 0===e&&(e={}),this.options=e,Object.entries(DEFAULT_OPTIONS).forEach(function(e){var o=e[0],n=e[1];Object.prototype.hasOwnProperty.call(t.options,o)||(t.options[o]=n)}),!this.options._const){var o=["addAttribute","modifyAttribute","removeAttribute","modifyTextElement","relocateGroup","removeElement","addElement","removeTextElement","addTextElement","replaceElement","modifyValue","modifyChecked","modifySelected","modifyComment","action","route","oldValue","newValue","element","group","from","to","name","value","data","attributes","nodeName","childNodes","checked","selected"];this.options._const={},this.options.compress?o.forEach(function(e,o){return t.options._const[e]=o}):o.forEach(function(e){return t.options._const[e]=e})}this.DiffFinder=DiffFinder};DiffDOM.prototype.apply=function(e,t){return applyDOM(e,t,this.options)},DiffDOM.prototype.undo=function(e,t){return undoDOM(e,t,this.options)},DiffDOM.prototype.diff=function(e,t){return new this.DiffFinder(e,t,this.options).init()};var TraceLogger=function(e){var t=this;void 0===e&&(e={}),this.pad="│ ",this.padding="",this.tick=1,this.messages=[];var o=function(e,o){var n=e[o];e[o]=function(){for(var s=[],i=arguments.length;i--;)s[i]=arguments[i];t.fin(o,Array.prototype.slice.call(s));var a=n.apply(e,s);return t.fout(o,a),a}};for(var n in e)"function"==typeof e[n]&&o(e,n);this.log("┌ TRACELOG START")};TraceLogger.prototype.fin=function(e,t){this.padding+=this.pad,this.log("├─> entering "+e,t)},TraceLogger.prototype.fout=function(e,t){this.log("│<──┘ generated return value",t),this.padding=this.padding.substring(0,this.padding.length-this.pad.length)},TraceLogger.prototype.format=function(e,t){return function(e){for(e=""+e;e.length<4;)e="0"+e;return e}(t)+"> "+this.padding+e},TraceLogger.prototype.log=function(){var e=Array.prototype.slice.call(arguments),t=function(e){return e?"string"==typeof e?e:e instanceof HTMLElement?e.outerHTML||"<empty>":e instanceof Array?"["+e.map(t).join(",")+"]":e.toString()||e.valueOf()||"<unknown>":"<falsey>"};e=e.map(t).join(", "),this.messages.push(this.format(e,this.tick++))},TraceLogger.prototype.toString=function(){for(var e="└───";e.length<=this.padding.length+this.pad.length;)e+="× ";var t=this.padding;return this.padding="",e=this.format(e,this.tick),this.padding=t,this.messages.join("\n")+"\n"+e},exports.DiffDOM=DiffDOM,exports.TraceLogger=TraceLogger,exports.nodeToObj=nodeToObj,exports.stringToObj=stringToObj; | ||
"use strict";function e(t,o,n){var s;return"#text"===t.nodeName?s=n.document.createTextNode(t.data):"#comment"===t.nodeName?s=n.document.createComment(t.data):(o?s=n.document.createElementNS("http://www.w3.org/2000/svg",t.nodeName):"svg"===t.nodeName.toLowerCase()?(s=n.document.createElementNS("http://www.w3.org/2000/svg","svg"),o=!0):s=n.document.createElement(t.nodeName),t.attributes&&Object.entries(t.attributes).forEach((function(e){var t=e[0],o=e[1];return s.setAttribute(t,o)})),t.childNodes&&t.childNodes.forEach((function(t){return s.appendChild(e(t,o,n))})),n.valueDiffing&&(t.value&&(s.value=t.value),t.checked&&(s.checked=t.checked),t.selected&&(s.selected=t.selected))),s}function t(e,t){for(t=t.slice();t.length>0;){if(!e.childNodes)return!1;var o=t.splice(0,1)[0];e=e.childNodes[o]}return e}function o(o,n,s){var i,a,l,c,r=t(o,n[s._const.route]),u={diff:n,node:r};if(s.preDiffApply(u))return!0;switch(n[s._const.action]){case s._const.addAttribute:if(!r||!r.setAttribute)return!1;r.setAttribute(n[s._const.name],n[s._const.value]);break;case s._const.modifyAttribute:if(!r||!r.setAttribute)return!1;r.setAttribute(n[s._const.name],n[s._const.newValue]),"INPUT"===r.nodeName&&"value"===n[s._const.name]&&(r.value=n[s._const.newValue]);break;case s._const.removeAttribute:if(!r||!r.removeAttribute)return!1;r.removeAttribute(n[s._const.name]);break;case s._const.modifyTextElement:if(!r||3!==r.nodeType)return!1;s.textDiff(r,r.data,n[s._const.oldValue],n[s._const.newValue]);break;case s._const.modifyValue:if(!r||void 0===r.value)return!1;r.value=n[s._const.newValue];break;case s._const.modifyComment:if(!r||void 0===r.data)return!1;s.textDiff(r,r.data,n[s._const.oldValue],n[s._const.newValue]);break;case s._const.modifyChecked:if(!r||void 0===r.checked)return!1;r.checked=n[s._const.newValue];break;case s._const.modifySelected:if(!r||void 0===r.selected)return!1;r.selected=n[s._const.newValue];break;case s._const.replaceElement:r.parentNode.replaceChild(e(n[s._const.newValue],"http://www.w3.org/2000/svg"===r.namespaceURI,s),r);break;case s._const.relocateGroup:Array.apply(void 0,new Array(n.groupLength)).map((function(){return r.removeChild(r.childNodes[n[s._const.from]])})).forEach((function(e,t){0===t&&(a=r.childNodes[n[s._const.to]]),r.insertBefore(e,a||null)}));break;case s._const.removeElement:r.parentNode.removeChild(r);break;case s._const.addElement:c=(l=n[s._const.route].slice()).splice(l.length-1,1)[0],(r=t(o,l)).insertBefore(e(n[s._const.element],"http://www.w3.org/2000/svg"===r.namespaceURI,s),r.childNodes[c]||null);break;case s._const.removeTextElement:if(!r||3!==r.nodeType)return!1;r.parentNode.removeChild(r);break;case s._const.addTextElement:if(c=(l=n[s._const.route].slice()).splice(l.length-1,1)[0],i=s.document.createTextNode(n[s._const.value]),!(r=t(o,l))||!r.childNodes)return!1;r.insertBefore(i,r.childNodes[c]||null);break;default:console.log("unknown action")}return u.newNode=i,s.postDiffApply(u),!0}function n(e,t,o){var n=e[t];e[t]=e[o],e[o]=n}function s(e,t,s){t.length||(t=[t]),(t=t.slice()).reverse(),t.forEach((function(t){!function(e,t,s){switch(t[s._const.action]){case s._const.addAttribute:t[s._const.action]=s._const.removeAttribute,o(e,t,s);break;case s._const.modifyAttribute:n(t,s._const.oldValue,s._const.newValue),o(e,t,s);break;case s._const.removeAttribute:t[s._const.action]=s._const.addAttribute,o(e,t,s);break;case s._const.modifyTextElement:case s._const.modifyValue:case s._const.modifyComment:case s._const.modifyChecked:case s._const.modifySelected:case s._const.replaceElement:n(t,s._const.oldValue,s._const.newValue),o(e,t,s);break;case s._const.relocateGroup:n(t,s._const.from,s._const.to),o(e,t,s);break;case s._const.removeElement:t[s._const.action]=s._const.addElement,o(e,t,s);break;case s._const.addElement:t[s._const.action]=s._const.removeElement,o(e,t,s);break;case s._const.removeTextElement:t[s._const.action]=s._const.addTextElement,o(e,t,s);break;case s._const.addTextElement:t[s._const.action]=s._const.removeTextElement,o(e,t,s);break;default:console.log("unknown action")}}(e,t,s)}))}Object.defineProperty(exports,"__esModule",{value:!0});var i=function(e){var t=this;void 0===e&&(e={}),Object.entries(e).forEach((function(e){var o=e[0],n=e[1];return t[o]=n}))};function a(e){var t=[];return t.push(e.nodeName),"#text"!==e.nodeName&&"#comment"!==e.nodeName&&e.attributes&&(e.attributes.class&&t.push(e.nodeName+"."+e.attributes.class.replace(/ /g,".")),e.attributes.id&&t.push(e.nodeName+"#"+e.attributes.id)),t}function l(e){var t={},o={};return e.forEach((function(e){a(e).forEach((function(e){var n=e in t;n||e in o?n&&(delete t[e],o[e]=!0):t[e]=!0}))})),t}function c(e,t){var o=l(e),n=l(t),s={};return Object.keys(o).forEach((function(e){n[e]&&(s[e]=!0)})),s}function r(e){return delete e.outerDone,delete e.innerDone,delete e.valueDone,!e.childNodes||e.childNodes.every(r)}function u(e,t){if(!["nodeName","value","checked","selected","data"].every((function(o){return e[o]===t[o]})))return!1;if(Boolean(e.attributes)!==Boolean(t.attributes))return!1;if(Boolean(e.childNodes)!==Boolean(t.childNodes))return!1;if(e.attributes){var o=Object.keys(e.attributes),n=Object.keys(t.attributes);if(o.length!==n.length)return!1;if(!o.every((function(o){return e.attributes[o]===t.attributes[o]})))return!1}if(e.childNodes){if(e.childNodes.length!==t.childNodes.length)return!1;if(!e.childNodes.every((function(e,o){return u(e,t.childNodes[o])})))return!1}return!0}function d(e,t,o,n,s){if(!e||!t)return!1;if(e.nodeName!==t.nodeName)return!1;if("#text"===e.nodeName)return!!s||e.data===t.data;if(e.nodeName in o)return!0;if(e.attributes&&t.attributes){if(e.attributes.id){if(e.attributes.id!==t.attributes.id)return!1;if(e.nodeName+"#"+e.attributes.id in o)return!0}if(e.attributes.class&&e.attributes.class===t.attributes.class)if(e.nodeName+"."+e.attributes.class.replace(/ /g,".")in o)return!0}if(n)return!0;var i=e.childNodes?e.childNodes.slice().reverse():[],a=t.childNodes?t.childNodes.slice().reverse():[];if(i.length!==a.length)return!1;if(s)return i.every((function(e,t){return e.nodeName===a[t].nodeName}));var l=c(i,a);return i.every((function(e,t){return d(e,a[t],l,!0,!0)}))}function h(e){return JSON.parse(JSON.stringify(e))}function f(e,t,o,n){var s=0,i=[],l=e.length,r=t.length,u=Array.apply(void 0,new Array(l+1)).map((function(){return[]})),h=c(e,t),f=l===r;f&&e.some((function(e,o){var n=a(e),s=a(t[o]);return n.length!==s.length?(f=!1,!0):(n.some((function(e,t){if(e!==s[t])return f=!1,!0})),!f||void 0)}));for(var p=0;p<l;p++)for(var m=e[p],_=0;_<r;_++){var V=t[_];o[p]||n[_]||!d(m,V,h,f)?u[p+1][_+1]=0:(u[p+1][_+1]=u[p][_]?u[p][_]+1:1,u[p+1][_+1]>=s&&(s=u[p+1][_+1],i=[p+1,_+1]))}return 0!==s&&{oldValue:i[0]-s,newValue:i[1]-s,length:s}}function p(e,t){return Array.apply(void 0,new Array(e)).map((function(){return t}))}i.prototype.toString=function(){return JSON.stringify(this)},i.prototype.setValue=function(e,t){return this[e]=t,this};var m=function(){this.list=[]};function _(e,t){var o,n,s=e;for(t=t.slice();t.length>0;){if(!s.childNodes)return!1;n=t.splice(0,1)[0],o=s,s=s.childNodes[n]}return{node:s,parentNode:o,nodeIndex:n}}function V(e,t,o){return t.forEach((function(t){!function(e,t,o){var n,s,i,a=_(e,t[o._const.route]),l=a.node,c=a.parentNode,r=a.nodeIndex,u=[],d={diff:t,node:l};if(o.preVirtualDiffApply(d))return!0;switch(t[o._const.action]){case o._const.addAttribute:l.attributes||(l.attributes={}),l.attributes[t[o._const.name]]=t[o._const.value],"checked"===t[o._const.name]?l.checked=!0:"selected"===t[o._const.name]?l.selected=!0:"INPUT"===l.nodeName&&"value"===t[o._const.name]&&(l.value=t[o._const.value]);break;case o._const.modifyAttribute:l.attributes[t[o._const.name]]=t[o._const.newValue];break;case o._const.removeAttribute:delete l.attributes[t[o._const.name]],0===Object.keys(l.attributes).length&&delete l.attributes,"checked"===t[o._const.name]?l.checked=!1:"selected"===t[o._const.name]?delete l.selected:"INPUT"===l.nodeName&&"value"===t[o._const.name]&&delete l.value;break;case o._const.modifyTextElement:l.data=t[o._const.newValue];break;case o._const.modifyValue:l.value=t[o._const.newValue];break;case o._const.modifyComment:l.data=t[o._const.newValue];break;case o._const.modifyChecked:l.checked=t[o._const.newValue];break;case o._const.modifySelected:l.selected=t[o._const.newValue];break;case o._const.replaceElement:(n=h(t[o._const.newValue])).outerDone=!0,n.innerDone=!0,n.valueDone=!0,c.childNodes[r]=n;break;case o._const.relocateGroup:l.childNodes.splice(t[o._const.from],t.groupLength).reverse().forEach((function(e){return l.childNodes.splice(t[o._const.to],0,e)})),l.subsets&&l.subsets.forEach((function(e){if(t[o._const.from]<t[o._const.to]&&e.oldValue<=t[o._const.to]&&e.oldValue>t[o._const.from]){e.oldValue-=t.groupLength;var n=e.oldValue+e.length-t[o._const.to];n>0&&(u.push({oldValue:t[o._const.to]+t.groupLength,newValue:e.newValue+e.length-n,length:n}),e.length-=n)}else if(t[o._const.from]>t[o._const.to]&&e.oldValue>t[o._const.to]&&e.oldValue<t[o._const.from]){e.oldValue+=t.groupLength;var s=e.oldValue+e.length-t[o._const.to];s>0&&(u.push({oldValue:t[o._const.to]+t.groupLength,newValue:e.newValue+e.length-s,length:s}),e.length-=s)}else e.oldValue===t[o._const.from]&&(e.oldValue=t[o._const.to])}));break;case o._const.removeElement:c.childNodes.splice(r,1),c.subsets&&c.subsets.forEach((function(e){e.oldValue>r?e.oldValue-=1:e.oldValue===r?e.delete=!0:e.oldValue<r&&e.oldValue+e.length>r&&(e.oldValue+e.length-1===r?e.length--:(u.push({newValue:e.newValue+r-e.oldValue,oldValue:r,length:e.length-r+e.oldValue-1}),e.length=r-e.oldValue))})),l=c;break;case o._const.addElement:s=t[o._const.route].slice(),i=s.splice(s.length-1,1)[0],l=_(e,s).node,(n=h(t[o._const.element])).outerDone=!0,n.innerDone=!0,n.valueDone=!0,l.childNodes||(l.childNodes=[]),i>=l.childNodes.length?l.childNodes.push(n):l.childNodes.splice(i,0,n),l.subsets&&l.subsets.forEach((function(e){if(e.oldValue>=i)e.oldValue+=1;else if(e.oldValue<i&&e.oldValue+e.length>i){var t=e.oldValue+e.length-i;u.push({newValue:e.newValue+e.length-t,oldValue:i+1,length:t}),e.length-=t}}));break;case o._const.removeTextElement:c.childNodes.splice(r,1),"TEXTAREA"===c.nodeName&&delete c.value,c.subsets&&c.subsets.forEach((function(e){e.oldValue>r?e.oldValue-=1:e.oldValue===r?e.delete=!0:e.oldValue<r&&e.oldValue+e.length>r&&(e.oldValue+e.length-1===r?e.length--:(u.push({newValue:e.newValue+r-e.oldValue,oldValue:r,length:e.length-r+e.oldValue-1}),e.length=r-e.oldValue))})),l=c;break;case o._const.addTextElement:s=t[o._const.route].slice(),i=s.splice(s.length-1,1)[0],(n={}).nodeName="#text",n.data=t[o._const.value],(l=_(e,s).node).childNodes||(l.childNodes=[]),i>=l.childNodes.length?l.childNodes.push(n):l.childNodes.splice(i,0,n),"TEXTAREA"===l.nodeName&&(l.value=t[o._const.newValue]),l.subsets&&l.subsets.forEach((function(e){if(e.oldValue>=i&&(e.oldValue+=1),e.oldValue<i&&e.oldValue+e.length>i){var t=e.oldValue+e.length-i;u.push({newValue:e.newValue+e.length-t,oldValue:i+1,length:t}),e.length-=t}}));break;default:console.log("unknown action")}l.subsets&&(l.subsets=l.subsets.filter((function(e){return!e.delete&&e.oldValue!==e.newValue})),u.length&&(l.subsets=l.subsets.concat(u))),d.newNode=n,o.postVirtualDiffApply(d)}(e,t,o)})),!0}function g(e,t){void 0===t&&(t={});var o={};if(o.nodeName=e.nodeName,"#text"===o.nodeName||"#comment"===o.nodeName)o.data=e.data;else{if(e.attributes&&e.attributes.length>0)o.attributes={},Array.prototype.slice.call(e.attributes).forEach((function(e){return o.attributes[e.name]=e.value}));if("TEXTAREA"===o.nodeName)o.value=e.value;else if(e.childNodes&&e.childNodes.length>0){o.childNodes=[],Array.prototype.slice.call(e.childNodes).forEach((function(e){return o.childNodes.push(g(e,t))}))}t.valueDiffing&&(void 0!==e.checked&&e.type&&["radio","checkbox"].includes(e.type.toLowerCase())?o.checked=e.checked:void 0!==e.value&&(o.value=e.value),void 0!==e.selected&&(o.selected=e.selected))}return o}m.prototype.add=function(e){var t;(t=this.list).push.apply(t,e)},m.prototype.forEach=function(e){this.list.forEach((function(t){return e(t)}))};var v=/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g,N=Object.create?Object.create(null):{},b=/\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;function w(e){return e.replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&")}var y={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuItem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0};function E(e){var t={nodeName:"",attributes:{}},o=e.match(/<\/?([^\s]+?)[/\s>]/);if(o&&(t.nodeName=o[1].toUpperCase(),(y[o[1].toLowerCase()]||"/"===e.charAt(e.length-2))&&(t.voidElement=!0),t.nodeName.startsWith("!--"))){var n=e.indexOf("--\x3e");return{type:"comment",data:-1!==n?e.slice(4,n):""}}for(var s=new RegExp(b),i=null,a=!1;!a;)if(null===(i=s.exec(e)))a=!0;else if(i[0].trim())if(i[1]){var l=i[1].trim(),c=[l,""];l.indexOf("=")>-1&&(c=l.split("=")),t.attributes[c[0]]=c[1],s.lastIndex--}else i[2]&&(t.attributes[i[2]]=i[3].trim().substring(1,i[3].length-1));return t}function k(e){return function e(t){return delete t.voidElement,t.childNodes&&t.childNodes.forEach((function(t){return e(t)})),t}(function(e,t){void 0===t&&(t={components:N});var o,n=[],s=-1,i=[],a=!1;return e.replace(v,(function(l,c){if(a){if(l!=="</"+o.nodeName+">")return;a=!1}var r,u="/"!==l.charAt(1),d=l.startsWith("\x3c!--"),h=c+l.length,f=e.charAt(h);if(d){var p=E(l);return s<0?(n.push(p),n):((r=i[s])&&(r.childNodes||(r.childNodes=[]),r.childNodes.push(p)),n)}if(u&&(o=E(l),s++,"tag"===o.type&&t.components[o.nodeName]&&(o.type="component",a=!0),o.voidElement||a||!f||"<"===f||(o.childNodes||(o.childNodes=[]),o.childNodes.push({nodeName:"#text",data:w(e.slice(h,e.indexOf("<",h)))})),0===s&&n.push(o),(r=i[s-1])&&(r.childNodes||(r.childNodes=[]),r.childNodes.push(o)),i[s]=o),(!u||o.voidElement)&&(s--,!a&&"<"!==f&&f)){r=-1===s?n:i[s].childNodes||[];var m=e.indexOf("<",h),_=w(e.slice(h,-1===m?void 0:m));r.push({nodeName:"#text",data:_})}})),n[0]}(e))}var x=function(e,t,o){this.options=o,this.t1=e instanceof HTMLElement?g(e,this.options):"string"==typeof e?k(e,this.options):JSON.parse(JSON.stringify(e)),this.t2=t instanceof HTMLElement?g(t,this.options):"string"==typeof t?k(t,this.options):JSON.parse(JSON.stringify(t)),this.diffcount=0,this.foundAll=!1,this.debug&&(this.t1Orig=g(e,this.options),this.t2Orig=g(t,this.options)),this.tracker=new m};x.prototype.init=function(){return this.findDiffs(this.t1,this.t2)},x.prototype.findDiffs=function(e,t){var o;do{if(this.options.debug&&(this.diffcount+=1,this.diffcount>this.options.diffcap))throw window.diffError=[this.t1Orig,this.t2Orig],new Error("surpassed diffcap:"+JSON.stringify(this.t1Orig)+" -> "+JSON.stringify(this.t2Orig));0===(o=this.findNextDiff(e,t,[])).length&&(u(e,t)||(this.foundAll?console.error("Could not find remaining diffs!"):(this.foundAll=!0,r(e),o=this.findNextDiff(e,t,[])))),o.length>0&&(this.foundAll=!1,this.tracker.add(o),V(e,o,this.options))}while(o.length>0);return this.tracker.list},x.prototype.findNextDiff=function(e,t,o){var n,s;if(this.options.maxDepth&&o.length>this.options.maxDepth)return[];if(!e.outerDone){if(n=this.findOuterDiff(e,t,o),this.options.filterOuterDiff&&(s=this.options.filterOuterDiff(e,t,n))&&(n=s),n.length>0)return e.outerDone=!0,n;e.outerDone=!0}if(!e.innerDone){if((n=this.findInnerDiff(e,t,o)).length>0)return n;e.innerDone=!0}if(this.options.valueDiffing&&!e.valueDone){if((n=this.findValueDiff(e,t,o)).length>0)return e.valueDone=!0,n;e.valueDone=!0}return[]},x.prototype.findOuterDiff=function(e,t,o){var n,s,a,l,c,r,u=[];if(e.nodeName!==t.nodeName){if(!o.length)throw new Error("Top level nodes have to be of the same kind.");return[(new i).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,h(e)).setValue(this.options._const.newValue,h(t)).setValue(this.options._const.route,o)]}if(o.length&&this.options.maxNodeDiffCount<Math.abs((e.childNodes||[]).length-(t.childNodes||[]).length))return[(new i).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,h(e)).setValue(this.options._const.newValue,h(t)).setValue(this.options._const.route,o)];if(e.data!==t.data)return"#text"===e.nodeName?[(new i).setValue(this.options._const.action,this.options._const.modifyTextElement).setValue(this.options._const.route,o).setValue(this.options._const.oldValue,e.data).setValue(this.options._const.newValue,t.data)]:[(new i).setValue(this.options._const.action,this.options._const.modifyComment).setValue(this.options._const.route,o).setValue(this.options._const.oldValue,e.data).setValue(this.options._const.newValue,t.data)];for(s=e.attributes?Object.keys(e.attributes).sort():[],a=t.attributes?Object.keys(t.attributes).sort():[],l=s.length,r=0;r<l;r++)n=s[r],-1===(c=a.indexOf(n))?u.push((new i).setValue(this.options._const.action,this.options._const.removeAttribute).setValue(this.options._const.route,o).setValue(this.options._const.name,n).setValue(this.options._const.value,e.attributes[n])):(a.splice(c,1),e.attributes[n]!==t.attributes[n]&&u.push((new i).setValue(this.options._const.action,this.options._const.modifyAttribute).setValue(this.options._const.route,o).setValue(this.options._const.name,n).setValue(this.options._const.oldValue,e.attributes[n]).setValue(this.options._const.newValue,t.attributes[n])));for(l=a.length,r=0;r<l;r++)n=a[r],u.push((new i).setValue(this.options._const.action,this.options._const.addAttribute).setValue(this.options._const.route,o).setValue(this.options._const.name,n).setValue(this.options._const.value,t.attributes[n]));return u},x.prototype.findInnerDiff=function(e,t,o){var n=e.childNodes?e.childNodes.slice():[],s=t.childNodes?t.childNodes.slice():[],a=Math.max(n.length,s.length),l=Math.abs(n.length-s.length),c=[],r=0;if(!this.options.maxChildCount||a<this.options.maxChildCount){var d=e.subsets&&e.subsetsAge--?e.subsets:e.childNodes&&t.childNodes?function(e,t){for(var o=e.childNodes?e.childNodes:[],n=t.childNodes?t.childNodes:[],s=p(o.length,!1),i=p(n.length,!1),a=[],l=!0,c=function(){return arguments[1]};l;){if(l=f(o,n,s,i))a.push(l),Array.apply(void 0,new Array(l.length)).map(c).forEach((function(e){return t=e,s[l.oldValue+t]=!0,void(i[l.newValue+t]=!0);var t}))}return e.subsets=a,e.subsetsAge=100,a}(e,t):[];if(d.length>0&&(c=this.attemptGroupRelocation(e,t,d,o)).length>0)return c}for(var m=0;m<a;m+=1){var _=n[m],V=s[m];l&&(_&&!V?"#text"===_.nodeName?(c.push((new i).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,o.concat(r)).setValue(this.options._const.value,_.data)),r-=1):(c.push((new i).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.route,o.concat(r)).setValue(this.options._const.element,h(_))),r-=1):V&&!_&&("#text"===V.nodeName?c.push((new i).setValue(this.options._const.action,this.options._const.addTextElement).setValue(this.options._const.route,o.concat(r)).setValue(this.options._const.value,V.data)):c.push((new i).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.route,o.concat(r)).setValue(this.options._const.element,h(V))))),_&&V&&(!this.options.maxChildCount||a<this.options.maxChildCount?c=c.concat(this.findNextDiff(_,V,o.concat(r))):u(_,V)||(n.length>s.length?(c=c.concat([(new i).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.element,h(_)).setValue(this.options._const.route,o.concat(r))]),n.splice(m,1),r-=1,l-=1):n.length<s.length?(c=c.concat([(new i).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.element,h(V)).setValue(this.options._const.route,o.concat(r))]),n.splice(m,0,{}),l-=1):c=c.concat([(new i).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,h(_)).setValue(this.options._const.newValue,h(V)).setValue(this.options._const.route,o.concat(r))]))),r+=1}return e.innerDone=!0,c},x.prototype.attemptGroupRelocation=function(e,t,o,n){for(var s,a,l,c,r,u,f=function(e,t,o){var n=e.childNodes?p(e.childNodes.length,!0):[],s=t.childNodes?p(t.childNodes.length,!0):[],i=0;return o.forEach((function(e){for(var t=e.oldValue+e.length,o=e.newValue+e.length,a=e.oldValue;a<t;a+=1)n[a]=i;for(var l=e.newValue;l<o;l+=1)s[l]=i;i+=1})),{gaps1:n,gaps2:s}}(e,t,o),m=f.gaps1,_=f.gaps2,V=Math.min(m.length,_.length),g=[],v=0,N=0;v<V;N+=1,v+=1)if(!0===m[v])if("#text"===(c=e.childNodes[N]).nodeName)if("#text"===t.childNodes[v].nodeName){if(c.data!==t.childNodes[v].data){for(u=N;e.childNodes.length>u+1&&"#text"===e.childNodes[u+1].nodeName;)if(u+=1,t.childNodes[v].data===e.childNodes[u].data){r=!0;break}if(!r)return g.push((new i).setValue(this.options._const.action,this.options._const.modifyTextElement).setValue(this.options._const.route,n.concat(v)).setValue(this.options._const.oldValue,c.data).setValue(this.options._const.newValue,t.childNodes[v].data)),g}}else g.push((new i).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,n.concat(v)).setValue(this.options._const.value,c.data)),m.splice(v,1),V=Math.min(m.length,_.length),v-=1;else g.push((new i).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.route,n.concat(v)).setValue(this.options._const.element,h(c))),m.splice(v,1),V=Math.min(m.length,_.length),v-=1;else if(!0===_[v])"#text"===(c=t.childNodes[v]).nodeName?(g.push((new i).setValue(this.options._const.action,this.options._const.addTextElement).setValue(this.options._const.route,n.concat(v)).setValue(this.options._const.value,c.data)),m.splice(v,0,!0),V=Math.min(m.length,_.length),N-=1):(g.push((new i).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.route,n.concat(v)).setValue(this.options._const.element,h(c))),m.splice(v,0,!0),V=Math.min(m.length,_.length),N-=1);else if(m[v]!==_[v]){if(g.length>0)return g;if(l=o[m[v]],(a=Math.min(l.newValue,e.childNodes.length-l.length))!==l.oldValue){s=!1;for(var b=0;b<l.length;b+=1)d(e.childNodes[a+b],e.childNodes[l.oldValue+b],[],!1,!0)||(s=!0);if(s)return[(new i).setValue(this.options._const.action,this.options._const.relocateGroup).setValue("groupLength",l.length).setValue(this.options._const.from,l.oldValue).setValue(this.options._const.to,a).setValue(this.options._const.route,n)]}}return g},x.prototype.findValueDiff=function(e,t,o){var n=[];return e.selected!==t.selected&&n.push((new i).setValue(this.options._const.action,this.options._const.modifySelected).setValue(this.options._const.oldValue,e.selected).setValue(this.options._const.newValue,t.selected).setValue(this.options._const.route,o)),(e.value||t.value)&&e.value!==t.value&&"OPTION"!==e.nodeName&&n.push((new i).setValue(this.options._const.action,this.options._const.modifyValue).setValue(this.options._const.oldValue,e.value||"").setValue(this.options._const.newValue,t.value||"").setValue(this.options._const.route,o)),e.checked!==t.checked&&n.push((new i).setValue(this.options._const.action,this.options._const.modifyChecked).setValue(this.options._const.oldValue,e.checked).setValue(this.options._const.newValue,t.checked).setValue(this.options._const.route,o)),n};var A={debug:!1,diffcap:10,maxDepth:!1,maxChildCount:50,valueDiffing:!0,textDiff:function(e,t,o,n){e.data=n},preVirtualDiffApply:function(){},postVirtualDiffApply:function(){},preDiffApply:function(){},postDiffApply:function(){},filterOuterDiff:null,compress:!1,_const:!1,document:!(!window||!window.document)&&window.document},D=function(e){var t=this;if(void 0===e&&(e={}),this.options=e,Object.entries(A).forEach((function(e){var o=e[0],n=e[1];Object.prototype.hasOwnProperty.call(t.options,o)||(t.options[o]=n)})),!this.options._const){var o=["addAttribute","modifyAttribute","removeAttribute","modifyTextElement","relocateGroup","removeElement","addElement","removeTextElement","addTextElement","replaceElement","modifyValue","modifyChecked","modifySelected","modifyComment","action","route","oldValue","newValue","element","group","from","to","name","value","data","attributes","nodeName","childNodes","checked","selected"];this.options._const={},this.options.compress?o.forEach((function(e,o){return t.options._const[e]=o})):o.forEach((function(e){return t.options._const[e]=e}))}this.DiffFinder=x};D.prototype.apply=function(e,t){return function(e,t,n){return t.every((function(t){return o(e,t,n)}))}(e,t,this.options)},D.prototype.undo=function(e,t){return s(e,t,this.options)},D.prototype.diff=function(e,t){return new this.DiffFinder(e,t,this.options).init()};var O=function(e){var t=this;void 0===e&&(e={}),this.pad="│ ",this.padding="",this.tick=1,this.messages=[];var o=function(e,o){var n=e[o];e[o]=function(){for(var s=[],i=arguments.length;i--;)s[i]=arguments[i];t.fin(o,Array.prototype.slice.call(s));var a=n.apply(e,s);return t.fout(o,a),a}};for(var n in e)"function"==typeof e[n]&&o(e,n);this.log("┌ TRACELOG START")};O.prototype.fin=function(e,t){this.padding+=this.pad,this.log("├─> entering "+e,t)},O.prototype.fout=function(e,t){this.log("│<──┘ generated return value",t),this.padding=this.padding.substring(0,this.padding.length-this.pad.length)},O.prototype.format=function(e,t){return function(e){for(e=""+e;e.length<4;)e="0"+e;return e}(t)+"> "+this.padding+e},O.prototype.log=function(){var e=Array.prototype.slice.call(arguments),t=function(e){return e?"string"==typeof e?e:e instanceof HTMLElement?e.outerHTML||"<empty>":e instanceof Array?"["+e.map(t).join(",")+"]":e.toString()||e.valueOf()||"<unknown>":"<falsey>"};e=e.map(t).join(", "),this.messages.push(this.format(e,this.tick++))},O.prototype.toString=function(){for(var e="└───";e.length<=this.padding.length+this.pad.length;)e+="× ";var t=this.padding;return this.padding="",e=this.format(e,this.tick),this.padding=t,this.messages.join("\n")+"\n"+e},exports.DiffDOM=D,exports.TraceLogger=O,exports.nodeToObj=g,exports.stringToObj=k; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "diff-dom", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"description": "A diff for DOM elements, as client-side JavaScript code. Gets all modifications, insertions and removals between two DOM fragments.", | ||
@@ -32,11 +32,10 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.5.5", | ||
"eslint": "^6.2.1", | ||
"@babel/preset-env": "^7.11.5", | ||
"eslint": "^6.8.0", | ||
"jest": "^24.9.0", | ||
"rollup": "^1.32.1", | ||
"rollup-plugin-buble": "^0.19.8", | ||
"rollup-plugin-terser": "^5.1.1", | ||
"rollup": "^1.19.4", | ||
"slimdom": "^2.3.1", | ||
"updates": "^8.5.2" | ||
"rollup-plugin-terser": "^5.3.1", | ||
"updates": "^8.5.3" | ||
} | ||
} |
@@ -9,4 +9,6 @@ export function objToNode(objNode, insideSvg, options) { | ||
} else { | ||
if (objNode.nodeName.toLowerCase() === 'svg' || insideSvg) { | ||
node = options.document.createElementNS('http://www.w3.org/2000/svg', objNode.nodeName.toLowerCase()) | ||
if (insideSvg) { | ||
node = options.document.createElementNS('http://www.w3.org/2000/svg', objNode.nodeName) | ||
} else if (objNode.nodeName.toLowerCase() === 'svg') { | ||
node = options.document.createElementNS('http://www.w3.org/2000/svg', 'svg') | ||
insideSvg = true | ||
@@ -13,0 +15,0 @@ } else { |
@@ -345,29 +345,32 @@ import {Diff, DiffTracker, cloneObj, getGapInformation, isEqual, markSubTrees, removeDone, roughlyEqual} from "./helpers" | ||
if (node.nodeName === '#text') { | ||
if (t2.childNodes[index2].nodeName === '#text' && node.data !== t2.childNodes[index2].data) { | ||
testI = index1 | ||
while (t1.childNodes.length > testI + 1 && t1.childNodes[testI + 1].nodeName === '#text') { | ||
testI += 1 | ||
if (t2.childNodes[index2].data === t1.childNodes[testI].data) { | ||
similarNode = true | ||
break | ||
if (t2.childNodes[index2].nodeName === '#text') { | ||
if (node.data !== t2.childNodes[index2].data) { | ||
testI = index1 | ||
while (t1.childNodes.length > testI + 1 && t1.childNodes[testI + 1].nodeName === '#text') { | ||
testI += 1 | ||
if (t2.childNodes[index2].data === t1.childNodes[testI].data) { | ||
similarNode = true | ||
break | ||
} | ||
} | ||
if (!similarNode) { | ||
diffs.push(new Diff() | ||
.setValue(this.options._const.action, this.options._const.modifyTextElement) | ||
.setValue(this.options._const.route, route.concat(index2)) | ||
.setValue(this.options._const.oldValue, node.data) | ||
.setValue(this.options._const.newValue, t2.childNodes[index2].data) | ||
) | ||
return diffs | ||
} | ||
} | ||
if (!similarNode) { | ||
diffs.push(new Diff() | ||
.setValue(this.options._const.action, this.options._const.modifyTextElement) | ||
.setValue(this.options._const.route, route.concat(index2)) | ||
.setValue(this.options._const.oldValue, node.data) | ||
.setValue(this.options._const.newValue, t2.childNodes[index2].data) | ||
) | ||
return diffs | ||
} | ||
} else { | ||
diffs.push(new Diff() | ||
.setValue(this.options._const.action, this.options._const.removeTextElement) | ||
.setValue(this.options._const.route, route.concat(index2)) | ||
.setValue(this.options._const.value, node.data) | ||
) | ||
gaps1.splice(index2, 1) | ||
shortest = Math.min(gaps1.length, gaps2.length) | ||
index2 -= 1 | ||
} | ||
diffs.push(new Diff() | ||
.setValue(this.options._const.action, this.options._const.removeTextElement) | ||
.setValue(this.options._const.route, route.concat(index2)) | ||
.setValue(this.options._const.value, node.data) | ||
) | ||
gaps1.splice(index2, 1) | ||
shortest = Math.min(gaps1.length, gaps2.length) | ||
index2 -= 1 | ||
} else { | ||
@@ -374,0 +377,0 @@ diffs.push(new Diff() |
@@ -46,4 +46,14 @@ // from html-parse-stringify (MIT) | ||
res.nodeName = tagMatch[1].toUpperCase() | ||
if (lookup[tagMatch[1].toLowerCase()] || tag.charAt(tag.length - 2) === '/') res.voidElement = true | ||
if (lookup[tagMatch[1].toLowerCase()] || tag.charAt(tag.length - 2) === '/') { | ||
res.voidElement = true | ||
} | ||
// handle comment tag | ||
if (res.nodeName.startsWith('!--')) { | ||
const endIndex = tag.indexOf('-->') | ||
return { | ||
type: 'comment', | ||
data: endIndex !== -1 ? tag.slice(4, endIndex) : '' | ||
} | ||
} | ||
} | ||
@@ -83,3 +93,2 @@ | ||
const arr = [] | ||
const byTag = {} | ||
let inComponent = false | ||
@@ -96,2 +105,3 @@ | ||
const isOpen = tag.charAt(1) !== '/' | ||
const isComment = tag.startsWith('<!--') | ||
const start = index + tag.length | ||
@@ -101,6 +111,24 @@ const nextChar = html.charAt(start) | ||
if (isComment) { | ||
const comment = parseTag(tag) | ||
// if we're at root, push new base node | ||
if (level < 0) { | ||
result.push(comment) | ||
return result | ||
} | ||
parent = arr[level] | ||
if (parent) { | ||
if (!parent.childNodes) { | ||
parent.childNodes = [] | ||
} | ||
parent.childNodes.push(comment) | ||
} | ||
return result | ||
} | ||
if (isOpen) { | ||
current = parseTag(tag) | ||
level++ | ||
current = parseTag(tag) | ||
if (current.type === 'tag' && options.components[current.nodeName]) { | ||
@@ -121,4 +149,2 @@ current.type = 'component' | ||
byTag[current.tagName] = current | ||
// if we're at root, push new base node | ||
@@ -125,0 +151,0 @@ if (level === 0) { |
@@ -18,4 +18,4 @@ export class Diff { | ||
const output = [] | ||
output.push(el.nodeName) | ||
if (el.nodeName !== '#text' && el.nodeName !== '#comment') { | ||
output.push(el.nodeName) | ||
if (el.attributes) { | ||
@@ -22,0 +22,0 @@ if (el.attributes['class']) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
407515
7
2065