diff --git a/browser/diffDOM.js b/browser/diffDOM.js index 263d7ba..9fac206 100644 --- a/browser/diffDOM.js +++ b/browser/diffDOM.js @@ -1,2 +1,2 @@ -var diffDOM=function(e){"use strict";function t(e,n,o){var s;return"#text"===e.nodeName?s=o.document.createTextNode(e.data):"#comment"===e.nodeName?s=o.document.createComment(e.data):(n?s=o.document.createElementNS("http://www.w3.org/2000/svg",e.nodeName):"svg"===e.nodeName.toLowerCase()?(s=o.document.createElementNS("http://www.w3.org/2000/svg","svg"),n=!0):s=o.document.createElement(e.nodeName),e.attributes&&Object.entries(e.attributes).forEach((function(e){var t=e[0],n=e[1];return s.setAttribute(t,n)})),e.childNodes&&e.childNodes.forEach((function(e){return s.appendChild(t(e,n,o))})),o.valueDiffing&&(e.value&&(s instanceof HTMLButtonElement||s instanceof HTMLDataElement||s instanceof HTMLInputElement||s instanceof HTMLLIElement||s instanceof HTMLMeterElement||s instanceof HTMLOptionElement||s instanceof HTMLProgressElement||s instanceof HTMLParamElement)&&(s.value=e.value),e.checked&&s instanceof HTMLInputElement&&(s.checked=e.checked),e.selected&&s instanceof HTMLOptionElement&&(s.selected=e.selected))),s}var n=function(e,t){for(t=t.slice();t.length>0;){var n=t.splice(0,1)[0];e=e.childNodes[n]}return e};function o(e,o,s){var i,a,c,l=o[s._const.action],r=o[s._const.route];[s._const.addElement,s._const.addTextElement].includes(l)||(i=n(e,r));var u={diff:o,node:i};if(s.preDiffApply(u))return!0;switch(l){case s._const.addAttribute:if(!(i&&i instanceof Element))return!1;i.setAttribute(o[s._const.name],o[s._const.value]);break;case s._const.modifyAttribute:if(!(i&&i instanceof Element))return!1;i.setAttribute(o[s._const.name],o[s._const.newValue]),i instanceof HTMLInputElement&&"value"===o[s._const.name]&&(i.value=o[s._const.newValue]);break;case s._const.removeAttribute:if(!(i&&i instanceof Element))return!1;i.removeAttribute(o[s._const.name]);break;case s._const.modifyTextElement:if(!(i&&i instanceof Text))return!1;s.textDiff(i,i.data,o[s._const.oldValue],o[s._const.newValue]),i.parentNode instanceof HTMLTextAreaElement&&(i.parentNode.value=o[s._const.newValue]);break;case s._const.modifyValue:if(!i||void 0===i.value)return!1;i.value=o[s._const.newValue];break;case s._const.modifyComment:if(!(i&&i instanceof Comment))return!1;s.textDiff(i,i.data,o[s._const.oldValue],o[s._const.newValue]);break;case s._const.modifyChecked:if(!i||void 0===i.checked)return!1;i.checked=o[s._const.newValue];break;case s._const.modifySelected:if(!i||void 0===i.selected)return!1;i.selected=o[s._const.newValue];break;case s._const.replaceElement:var d="svg"===o[s._const.newValue].nodeName.toLowerCase()||"http://www.w3.org/2000/svg"===i.parentNode.namespaceURI;i.parentNode.replaceChild(t(o[s._const.newValue],d,s),i);break;case s._const.relocateGroup:Array.apply(void 0,new Array(o[s._const.groupLength])).map((function(){return i.removeChild(i.childNodes[o[s._const.from]])})).forEach((function(e,t){0===t&&(c=i.childNodes[o[s._const.to]]),i.insertBefore(e,c||null)}));break;case s._const.removeElement:i.parentNode.removeChild(i);break;case s._const.addElement:var h=(p=r.slice()).splice(p.length-1,1)[0];if(!((i=n(e,p))instanceof Element))return!1;i.insertBefore(t(o[s._const.element],"http://www.w3.org/2000/svg"===i.namespaceURI,s),i.childNodes[h]||null);break;case s._const.removeTextElement:if(!i||3!==i.nodeType)return!1;var f=i.parentNode;f.removeChild(i),f instanceof HTMLTextAreaElement&&(f.value="");break;case s._const.addTextElement:var p;h=(p=r.slice()).splice(p.length-1,1)[0];if(a=s.document.createTextNode(o[s._const.value]),!(i=n(e,p)).childNodes)return!1;i.insertBefore(a,i.childNodes[h]||null),i.parentNode instanceof HTMLTextAreaElement&&(i.parentNode.value=o[s._const.value]);break;default:console.log("unknown action")}return s.postDiffApply({diff:u.diff,node:u.node,newNode:a}),!0}function s(e,t,n){var o=e[t];e[t]=e[n],e[n]=o}function i(e,t,n){(t=t.slice()).reverse(),t.forEach((function(t){!function(e,t,n){switch(t[n._const.action]){case n._const.addAttribute:t[n._const.action]=n._const.removeAttribute,o(e,t,n);break;case n._const.modifyAttribute:s(t,n._const.oldValue,n._const.newValue),o(e,t,n);break;case n._const.removeAttribute:t[n._const.action]=n._const.addAttribute,o(e,t,n);break;case n._const.modifyTextElement:case n._const.modifyValue:case n._const.modifyComment:case n._const.modifyChecked:case n._const.modifySelected:case n._const.replaceElement:s(t,n._const.oldValue,n._const.newValue),o(e,t,n);break;case n._const.relocateGroup:s(t,n._const.from,n._const.to),o(e,t,n);break;case n._const.removeElement:t[n._const.action]=n._const.addElement,o(e,t,n);break;case n._const.addElement:t[n._const.action]=n._const.removeElement,o(e,t,n);break;case n._const.removeTextElement:t[n._const.action]=n._const.addTextElement,o(e,t,n);break;case n._const.addTextElement:t[n._const.action]=n._const.removeTextElement,o(e,t,n);break;default:console.log("unknown action")}}(e,t,n)}))}var a=function(){return a=Object.assign||function(e){for(var t,n=arguments,o=1,s=arguments.length;o=s&&(s=u[p+1][_+1],i=[p+1,_+1]))}return 0!==s&&{oldValue:i[0]-s,newValue:i[1]-s,length:s}}(n,o,s,i);e?(a.push(e),Array.apply(void 0,new Array(e.length)).map(l).forEach((function(t){return function(e,t,n,o){e[n.oldValue+o]=!0,t[n.newValue+o]=!0}(s,i,e,t)}))):u=!0};!u;)d();return e.subsets=a,e.subsetsAge=100,a},_=function(){function e(){this.list=[]}return e.prototype.add=function(e){var t;(t=this.list).push.apply(t,e)},e.prototype.forEach=function(e){this.list.forEach((function(t){return e(t)}))},e}(),v=function(){function e(e){void 0===e&&(e={});var t=this;Object.entries(e).forEach((function(e){var n=e[0],o=e[1];return t[n]=o}))}return e.prototype.toString=function(){return JSON.stringify(this)},e.prototype.setValue=function(e,t){return this[e]=t,this},e}();function V(e,t){var n,o,s=e;for(t=t.slice();t.length>0;)o=t.splice(0,1)[0],n=s,s=s.childNodes?s.childNodes[o]:void 0;return{node:s,parentNode:n,nodeIndex:o}}function g(e,t,n){return t.forEach((function(t){!function(e,t,n){var o,s,i,a;if(![n._const.addElement,n._const.addTextElement].includes(t[n._const.action])){var c=V(e,t[n._const.route]);s=c.node,i=c.parentNode,a=c.nodeIndex}var l,r,u=[],h={diff:t,node:s};if(n.preVirtualDiffApply(h))return!0;switch(t[n._const.action]){case n._const.addAttribute:s.attributes||(s.attributes={}),s.attributes[t[n._const.name]]=t[n._const.value],"checked"===t[n._const.name]?s.checked=!0:"selected"===t[n._const.name]?s.selected=!0:"INPUT"===s.nodeName&&"value"===t[n._const.name]&&(s.value=t[n._const.value]);break;case n._const.modifyAttribute:s.attributes[t[n._const.name]]=t[n._const.newValue];break;case n._const.removeAttribute:delete s.attributes[t[n._const.name]],0===Object.keys(s.attributes).length&&delete s.attributes,"checked"===t[n._const.name]?s.checked=!1:"selected"===t[n._const.name]?delete s.selected:"INPUT"===s.nodeName&&"value"===t[n._const.name]&&delete s.value;break;case n._const.modifyTextElement:s.data=t[n._const.newValue],"TEXTAREA"===i.nodeName&&(i.value=t[n._const.newValue]);break;case n._const.modifyValue:s.value=t[n._const.newValue];break;case n._const.modifyComment:s.data=t[n._const.newValue];break;case n._const.modifyChecked:s.checked=t[n._const.newValue];break;case n._const.modifySelected:s.selected=t[n._const.newValue];break;case n._const.replaceElement:l=d(t[n._const.newValue]),i.childNodes[a]=l;break;case n._const.relocateGroup:s.childNodes.splice(t[n._const.from],t[n._const.groupLength]).reverse().forEach((function(e){return s.childNodes.splice(t[n._const.to],0,e)})),s.subsets&&s.subsets.forEach((function(e){if(t[n._const.from]t[n._const.from])e.oldValue-=t[n._const.groupLength],(o=e.oldValue+e.length-t[n._const.to])>0&&(u.push({oldValue:t[n._const.to]+t[n._const.groupLength],newValue:e.newValue+e.length-o,length:o}),e.length-=o);else if(t[n._const.from]>t[n._const.to]&&e.oldValue>t[n._const.to]&&e.oldValue0&&(u.push({oldValue:t[n._const.to]+t[n._const.groupLength],newValue:e.newValue+e.length-o,length:o}),e.length-=o)}else e.oldValue===t[n._const.from]&&(e.oldValue=t[n._const.to])}));break;case n._const.removeElement:i.childNodes.splice(a,1),i.subsets&&i.subsets.forEach((function(e){e.oldValue>a?e.oldValue-=1:e.oldValue===a?e.delete=!0:e.oldValuea&&(e.oldValue+e.length-1===a?e.length--:(u.push({newValue:e.newValue+a-e.oldValue,oldValue:a,length:e.length-a+e.oldValue-1}),e.length=a-e.oldValue))})),s=i;break;case n._const.addElement:var f=(r=t[n._const.route].slice()).splice(r.length-1,1)[0];s=null===(o=V(e,r))||void 0===o?void 0:o.node,l=d(t[n._const.element]),s.childNodes||(s.childNodes=[]),f>=s.childNodes.length?s.childNodes.push(l):s.childNodes.splice(f,0,l),s.subsets&&s.subsets.forEach((function(e){if(e.oldValue>=f)e.oldValue+=1;else if(e.oldValuef){var t=e.oldValue+e.length-f;u.push({newValue:e.newValue+e.length-t,oldValue:f+1,length:t}),e.length-=t}}));break;case n._const.removeTextElement:i.childNodes.splice(a,1),"TEXTAREA"===i.nodeName&&delete i.value,i.subsets&&i.subsets.forEach((function(e){e.oldValue>a?e.oldValue-=1:e.oldValue===a?e.delete=!0:e.oldValuea&&(e.oldValue+e.length-1===a?e.length--:(u.push({newValue:e.newValue+a-e.oldValue,oldValue:a,length:e.length-a+e.oldValue-1}),e.length=a-e.oldValue))})),s=i;break;case n._const.addTextElement:var p=(r=t[n._const.route].slice()).splice(r.length-1,1)[0];l={nodeName:"#text",data:t[n._const.value]},(s=V(e,r).node).childNodes||(s.childNodes=[]),p>=s.childNodes.length?s.childNodes.push(l):s.childNodes.splice(p,0,l),"TEXTAREA"===s.nodeName&&(s.value=t[n._const.newValue]),s.subsets&&s.subsets.forEach((function(e){if(e.oldValue>=p&&(e.oldValue+=1),e.oldValuep){var t=e.oldValue+e.length-p;u.push({newValue:e.newValue+e.length-t,oldValue:p+1,length:t}),e.length-=t}}));break;default:console.log("unknown action")}s.subsets&&(s.subsets=s.subsets.filter((function(e){return!e.delete&&e.oldValue!==e.newValue})),u.length&&(s.subsets=s.subsets.concat(u))),n.postVirtualDiffApply({node:h.node,diff:h.diff,newNode:l})}(e,t,n)})),!0}function b(e,t){void 0===t&&(t={valueDiffing:!0});var n={nodeName:e.nodeName};if(e instanceof Text||e instanceof Comment)n.data=e.data;else{if(e.attributes&&e.attributes.length>0)n.attributes={},Array.prototype.slice.call(e.attributes).forEach((function(e){return n.attributes[e.name]=e.value}));if(e.childNodes&&e.childNodes.length>0)n.childNodes=[],Array.prototype.slice.call(e.childNodes).forEach((function(e){return n.childNodes.push(b(e,t))}));t.valueDiffing&&(e instanceof HTMLTextAreaElement&&(n.value=e.value),e instanceof HTMLInputElement&&["radio","checkbox"].includes(e.type.toLowerCase())&&void 0!==e.checked?n.checked=e.checked:(e instanceof HTMLButtonElement||e instanceof HTMLDataElement||e instanceof HTMLInputElement||e instanceof HTMLLIElement||e instanceof HTMLMeterElement||e instanceof HTMLOptionElement||e instanceof HTMLProgressElement||e instanceof HTMLParamElement)&&(n.value=e.value),e instanceof HTMLOptionElement&&(n.selected=e.selected))}return n}var N=/<\s*\/*[a-zA-Z:_][a-zA-Z0-9:_\-.]*\s*(?:"[^"]*"['"]*|'[^']*'['"]*|[^'"/>])*\/*\s*>|/g,y=/\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;function w(e){return e.replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&")}var E={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},k=function(e,t){var n={nodeName:"",attributes:{}},o=!1,s=e.match(/<\/?([^\s]+?)[/\s>]/);if(s&&(n.nodeName=t||"svg"===s[1]?s[1]:s[1].toUpperCase(),(E[s[1]]||"/"===e.charAt(e.length-2))&&(o=!0),n.nodeName.startsWith("!--"))){var i=e.indexOf("--\x3e");return{type:"comment",node:{nodeName:"#comment",data:-1!==i?e.slice(4,i):""},voidElement:o}}for(var a=new RegExp(y),c=null,l=!1;!l;)if(null===(c=a.exec(e)))l=!0;else if(c[0].trim())if(c[1]){var r=c[1].trim(),u=[r,""];r.indexOf("=")>-1&&(u=r.split("=")),n.attributes[u[0]]=u[1],a.lastIndex--}else c[2]&&(n.attributes[c[2]]=c[3].trim().substring(1,c[3].length-1));return{type:"tag",node:n,voidElement:o}},x=function(e,t){void 0===t&&(t={valueDiffing:!0,caseSensitive:!1});var n,o=[],s=-1,i=[],a=!1;if(0!==e.indexOf("<")){var c=e.indexOf("<");o.push({nodeName:"#text",data:-1===c?e:e.substring(0,c)})}return e.replace(N,(function(c,l){var r="/"!==c.charAt(1),u=c.startsWith("\x3c!--"),d=l+c.length,h=e.charAt(d);if(u){var f=k(c,t.caseSensitive).node;if(s<0)return o.push(f),"";var p=i[s];return p&&f.nodeName&&(p.node.childNodes||(p.node.childNodes=[]),p.node.childNodes.push(f)),""}if(r){if("svg"===(n=k(c,t.caseSensitive||a)).node.nodeName&&(a=!0),s++,!n.voidElement&&h&&"<"!==h){n.node.childNodes||(n.node.childNodes=[]);var m=w(e.slice(d,e.indexOf("<",d)));n.node.childNodes.push({nodeName:"#text",data:m}),t.valueDiffing&&"TEXTAREA"===n.node.nodeName&&(n.node.value=m)}0===s&&n.node.nodeName&&o.push(n.node);var _=i[s-1];_&&n.node.nodeName&&(_.node.childNodes||(_.node.childNodes=[]),_.node.childNodes.push(n.node)),i[s]=n}if((!r||n.voidElement)&&(s>-1&&(n.voidElement||t.caseSensitive&&n.node.nodeName===c.slice(2,-1)||!t.caseSensitive&&n.node.nodeName.toUpperCase()===c.slice(2,-1).toUpperCase())&&--s>-1&&("svg"===n.node.nodeName&&(a=!1),n=i[s]),"<"!==h&&h)){var v=-1===s?o:i[s].node.childNodes||[],V=e.indexOf("<",d);m=w(e.slice(d,-1===V?void 0:V));v.push({nodeName:"#text",data:m})}return""})),o[0]},T=function(){function e(e,t,n){this.options=n,this.t1="undefined"!=typeof Element&&e instanceof Element?b(e,this.options):"string"==typeof e?x(e,this.options):JSON.parse(JSON.stringify(e)),this.t2="undefined"!=typeof Element&&t instanceof Element?b(t,this.options):"string"==typeof t?x(t,this.options):JSON.parse(JSON.stringify(t)),this.diffcount=0,this.foundAll=!1,this.debug&&(this.t1Orig="undefined"!=typeof Element&&e instanceof Element?b(e,this.options):"string"==typeof e?x(e,this.options):JSON.parse(JSON.stringify(e)),this.t2Orig="undefined"!=typeof Element&&t instanceof Element?b(t,this.options):"string"==typeof t?x(t,this.options):JSON.parse(JSON.stringify(t))),this.tracker=new _}return e.prototype.init=function(){return this.findDiffs(this.t1,this.t2)},e.prototype.findDiffs=function(e,t){var n;do{if(this.options.debug&&(this.diffcount+=1,this.diffcount>this.options.diffcap))throw new Error("surpassed diffcap:".concat(JSON.stringify(this.t1Orig)," -> ").concat(JSON.stringify(this.t2Orig)));0===(n=this.findNextDiff(e,t,[])).length&&(h(e,t)||(this.foundAll?console.error("Could not find remaining diffs!"):(this.foundAll=!0,u(e),n=this.findNextDiff(e,t,[])))),n.length>0&&(this.foundAll=!1,this.tracker.add(n),g(e,n,this.options))}while(n.length>0);return this.tracker.list},e.prototype.findNextDiff=function(e,t,n){var o,s;if(this.options.maxDepth&&n.length>this.options.maxDepth)return[];if(!e.outerDone){if(o=this.findOuterDiff(e,t,n),this.options.filterOuterDiff&&(s=this.options.filterOuterDiff(e,t,o))&&(o=s),o.length>0)return e.outerDone=!0,o;e.outerDone=!0}if(Object.prototype.hasOwnProperty.call(e,"data"))return[];if(!e.innerDone){if((o=this.findInnerDiff(e,t,n)).length>0)return o;e.innerDone=!0}if(this.options.valueDiffing&&!e.valueDone){if((o=this.findValueDiff(e,t,n)).length>0)return e.valueDone=!0,o;e.valueDone=!0}return[]},e.prototype.findOuterDiff=function(e,t,n){var o,s,i,a,c,l,r=[];if(e.nodeName!==t.nodeName){if(!n.length)throw new Error("Top level nodes have to be of the same kind.");return[(new v).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,d(e)).setValue(this.options._const.newValue,d(t)).setValue(this.options._const.route,n)]}if(n.length&&this.options.diffcap0&&(c=this.attemptGroupRelocation(e,t,u,n,r)).length>0)return c}for(var f=0;fs.length?("#text"===p.nodeName?c.push((new v).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,n.concat(l)).setValue(this.options._const.value,p.data)):c.push((new v).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.element,d(p)).setValue(this.options._const.route,n.concat(l))),o.splice(f,1),f-=1,l-=1,a-=1):o.lengthw+1&&"#text"===_[w+1].nodeName;)if(w+=1,V[N].data===_[w].data){r=!0;break}r||b.push((new v).setValue(this.options._const.action,this.options._const.modifyTextElement).setValue(this.options._const.route,o.concat(y)).setValue(this.options._const.oldValue,l.data).setValue(this.options._const.newValue,V[N].data))}}else b.push((new v).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,o.concat(y)).setValue(this.options._const.value,l.data)),h.splice(y,1),_.splice(y,1),g=Math.min(h.length,m.length),y-=1,N-=1;else!0===m[N]?b.push((new v).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,d(l)).setValue(this.options._const.newValue,d(V[N])).setValue(this.options._const.route,o.concat(y))):(b.push((new v).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.route,o.concat(y)).setValue(this.options._const.element,d(l))),h.splice(y,1),_.splice(y,1),g=Math.min(h.length,m.length),y-=1,N-=1);else if(!0===m[N])"#text"===(l=V[N]).nodeName?(b.push((new v).setValue(this.options._const.action,this.options._const.addTextElement).setValue(this.options._const.route,o.concat(y)).setValue(this.options._const.value,l.data)),h.splice(y,0,!0),_.splice(y,0,{nodeName:"#text",data:l.data}),g=Math.min(h.length,m.length)):(b.push((new v).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.route,o.concat(y)).setValue(this.options._const.element,d(l))),h.splice(y,0,!0),_.splice(y,0,d(l)),g=Math.min(h.length,m.length));else if(h[y]!==m[N]){if(b.length>0)return b;if(c=n[h[y]],(a=Math.min(c.newValue,_.length-c.length))!==c.oldValue){i=!1;for(var E=0;E entering ".concat(e),t)},e.prototype.fout=function(e,t){this.log("│<──┘ generated return value",t),this.padding=this.padding.substring(0,this.padding.length-this.pad.length)},e.prototype.format=function(e,t){return"".concat(function(e){for(var t="".concat(e);t.length<4;)t="0".concat(e);return t}(t),"> ").concat(this.padding).concat(e)},e.prototype.log=function(){for(var e=arguments,t=[],n=0;n":e instanceof Array?"[".concat(e.map(o).join(","),"]"):e.toString()||e.valueOf()||"":""},s=t.map(o).join(", ");this.messages.push(this.format(s,this.tick++))},e.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,"".concat(this.messages.join("\n"),"\n").concat(e)},e}();return e.DiffDOM=A,e.TraceLogger=D,e.nodeToObj=b,e.stringToObj=x,e}({}); +var diffDOM=function(e){"use strict";function t(e,n,o){var s;return"#text"===e.nodeName?s=o.document.createTextNode(e.data):"#comment"===e.nodeName?s=o.document.createComment(e.data):(n?s=o.document.createElementNS("http://www.w3.org/2000/svg",e.nodeName):"svg"===e.nodeName.toLowerCase()?(s=o.document.createElementNS("http://www.w3.org/2000/svg","svg"),n=!0):s=o.document.createElement(e.nodeName),e.attributes&&Object.entries(e.attributes).forEach((function(e){var t=e[0],n=e[1];return s.setAttribute(t,n)})),e.childNodes&&e.childNodes.forEach((function(e){return s.appendChild(t(e,n,o))})),o.valueDiffing&&(e.value&&(s instanceof HTMLButtonElement||s instanceof HTMLDataElement||s instanceof HTMLInputElement||s instanceof HTMLLIElement||s instanceof HTMLMeterElement||s instanceof HTMLOptionElement||s instanceof HTMLProgressElement||s instanceof HTMLParamElement)&&(s.value=e.value),e.checked&&s instanceof HTMLInputElement&&(s.checked=e.checked),e.selected&&s instanceof HTMLOptionElement&&(s.selected=e.selected))),s}var n=function(e,t){for(t=t.slice();t.length>0;){var n=t.splice(0,1)[0];e=e.childNodes[n]}return e};function o(e,o,s){var i,a,c,l=o[s._const.action],r=o[s._const.route];[s._const.addElement,s._const.addTextElement].includes(l)||(i=n(e,r));var u={diff:o,node:i};if(s.preDiffApply(u))return!0;switch(l){case s._const.addAttribute:if(!(i&&i instanceof Element))return!1;i.setAttribute(o[s._const.name],o[s._const.value]);break;case s._const.modifyAttribute:if(!(i&&i instanceof Element))return!1;i.setAttribute(o[s._const.name],o[s._const.newValue]),i instanceof HTMLInputElement&&"value"===o[s._const.name]&&(i.value=o[s._const.newValue]);break;case s._const.removeAttribute:if(!(i&&i instanceof Element))return!1;i.removeAttribute(o[s._const.name]);break;case s._const.modifyTextElement:if(!(i&&i instanceof Text))return!1;s.textDiff(i,i.data,o[s._const.oldValue],o[s._const.newValue]),i.parentNode instanceof HTMLTextAreaElement&&(i.parentNode.value=o[s._const.newValue]);break;case s._const.modifyValue:if(!i||void 0===i.value)return!1;i.value=o[s._const.newValue];break;case s._const.modifyComment:if(!(i&&i instanceof Comment))return!1;s.textDiff(i,i.data,o[s._const.oldValue],o[s._const.newValue]);break;case s._const.modifyChecked:if(!i||void 0===i.checked)return!1;i.checked=o[s._const.newValue];break;case s._const.modifySelected:if(!i||void 0===i.selected)return!1;i.selected=o[s._const.newValue];break;case s._const.replaceElement:var d="svg"===o[s._const.newValue].nodeName.toLowerCase()||"http://www.w3.org/2000/svg"===i.parentNode.namespaceURI;i.parentNode.replaceChild(t(o[s._const.newValue],d,s),i);break;case s._const.relocateGroup:Array.apply(void 0,new Array(o[s._const.groupLength])).map((function(){return i.removeChild(i.childNodes[o[s._const.from]])})).forEach((function(e,t){0===t&&(c=i.childNodes[o[s._const.to]]),i.insertBefore(e,c||null)}));break;case s._const.removeElement:i.parentNode.removeChild(i);break;case s._const.addElement:var h=(p=r.slice()).splice(p.length-1,1)[0];if(!((i=n(e,p))instanceof Element))return!1;i.insertBefore(t(o[s._const.element],"http://www.w3.org/2000/svg"===i.namespaceURI,s),i.childNodes[h]||null);break;case s._const.removeTextElement:if(!i||3!==i.nodeType)return!1;var f=i.parentNode;f.removeChild(i),f instanceof HTMLTextAreaElement&&(f.value="");break;case s._const.addTextElement:var p;h=(p=r.slice()).splice(p.length-1,1)[0];if(a=s.document.createTextNode(o[s._const.value]),!(i=n(e,p)).childNodes)return!1;i.insertBefore(a,i.childNodes[h]||null),i.parentNode instanceof HTMLTextAreaElement&&(i.parentNode.value=o[s._const.value]);break;default:console.log("unknown action")}return s.postDiffApply({diff:u.diff,node:u.node,newNode:a}),!0}function s(e,t,n){var o=e[t];e[t]=e[n],e[n]=o}function i(e,t,n){(t=t.slice()).reverse(),t.forEach((function(t){!function(e,t,n){switch(t[n._const.action]){case n._const.addAttribute:t[n._const.action]=n._const.removeAttribute,o(e,t,n);break;case n._const.modifyAttribute:s(t,n._const.oldValue,n._const.newValue),o(e,t,n);break;case n._const.removeAttribute:t[n._const.action]=n._const.addAttribute,o(e,t,n);break;case n._const.modifyTextElement:case n._const.modifyValue:case n._const.modifyComment:case n._const.modifyChecked:case n._const.modifySelected:case n._const.replaceElement:s(t,n._const.oldValue,n._const.newValue),o(e,t,n);break;case n._const.relocateGroup:s(t,n._const.from,n._const.to),o(e,t,n);break;case n._const.removeElement:t[n._const.action]=n._const.addElement,o(e,t,n);break;case n._const.addElement:t[n._const.action]=n._const.removeElement,o(e,t,n);break;case n._const.removeTextElement:t[n._const.action]=n._const.addTextElement,o(e,t,n);break;case n._const.addTextElement:t[n._const.action]=n._const.removeTextElement,o(e,t,n);break;default:console.log("unknown action")}}(e,t,n)}))}var a=function(){return a=Object.assign||function(e){for(var t,n=arguments,o=1,s=arguments.length;o=s&&(s=u[p+1][_+1],i=[p+1,_+1]))}return 0!==s&&{oldValue:i[0]-s,newValue:i[1]-s,length:s}}(n,o,s,i);e?(a.push(e),Array.apply(void 0,new Array(e.length)).map(l).forEach((function(t){return function(e,t,n,o){e[n.oldValue+o]=!0,t[n.newValue+o]=!0}(s,i,e,t)}))):u=!0};!u;)d();return e.subsets=a,e.subsetsAge=100,a},_=function(){function e(){this.list=[]}return e.prototype.add=function(e){var t;(t=this.list).push.apply(t,e)},e.prototype.forEach=function(e){this.list.forEach((function(t){return e(t)}))},e}(),v=function(){function e(e){void 0===e&&(e={});var t=this;Object.entries(e).forEach((function(e){var n=e[0],o=e[1];return t[n]=o}))}return e.prototype.toString=function(){return JSON.stringify(this)},e.prototype.setValue=function(e,t){return this[e]=t,this},e}(),V=function(e,t){var n;return null!=e&&("undefined"!=typeof window?"function"==typeof window[t]&&!!(e instanceof window[t]||(null===(n=e.ownerDocument)||void 0===n?void 0:n.defaultView)&&e instanceof e.ownerDocument.defaultView[t]):"undefined"!=typeof global&&e instanceof global[t])};function g(e,t){var n,o,s=e;for(t=t.slice();t.length>0;)o=t.splice(0,1)[0],n=s,s=s.childNodes?s.childNodes[o]:void 0;return{node:s,parentNode:n,nodeIndex:o}}function b(e,t,n){return t.forEach((function(t){!function(e,t,n){var o,s,i,a;if(![n._const.addElement,n._const.addTextElement].includes(t[n._const.action])){var c=g(e,t[n._const.route]);s=c.node,i=c.parentNode,a=c.nodeIndex}var l,r,u=[],h={diff:t,node:s};if(n.preVirtualDiffApply(h))return!0;switch(t[n._const.action]){case n._const.addAttribute:s.attributes||(s.attributes={}),s.attributes[t[n._const.name]]=t[n._const.value],"checked"===t[n._const.name]?s.checked=!0:"selected"===t[n._const.name]?s.selected=!0:"INPUT"===s.nodeName&&"value"===t[n._const.name]&&(s.value=t[n._const.value]);break;case n._const.modifyAttribute:s.attributes[t[n._const.name]]=t[n._const.newValue];break;case n._const.removeAttribute:delete s.attributes[t[n._const.name]],0===Object.keys(s.attributes).length&&delete s.attributes,"checked"===t[n._const.name]?s.checked=!1:"selected"===t[n._const.name]?delete s.selected:"INPUT"===s.nodeName&&"value"===t[n._const.name]&&delete s.value;break;case n._const.modifyTextElement:s.data=t[n._const.newValue],"TEXTAREA"===i.nodeName&&(i.value=t[n._const.newValue]);break;case n._const.modifyValue:s.value=t[n._const.newValue];break;case n._const.modifyComment:s.data=t[n._const.newValue];break;case n._const.modifyChecked:s.checked=t[n._const.newValue];break;case n._const.modifySelected:s.selected=t[n._const.newValue];break;case n._const.replaceElement:l=d(t[n._const.newValue]),i.childNodes[a]=l;break;case n._const.relocateGroup:s.childNodes.splice(t[n._const.from],t[n._const.groupLength]).reverse().forEach((function(e){return s.childNodes.splice(t[n._const.to],0,e)})),s.subsets&&s.subsets.forEach((function(e){if(t[n._const.from]t[n._const.from])e.oldValue-=t[n._const.groupLength],(o=e.oldValue+e.length-t[n._const.to])>0&&(u.push({oldValue:t[n._const.to]+t[n._const.groupLength],newValue:e.newValue+e.length-o,length:o}),e.length-=o);else if(t[n._const.from]>t[n._const.to]&&e.oldValue>t[n._const.to]&&e.oldValue0&&(u.push({oldValue:t[n._const.to]+t[n._const.groupLength],newValue:e.newValue+e.length-o,length:o}),e.length-=o)}else e.oldValue===t[n._const.from]&&(e.oldValue=t[n._const.to])}));break;case n._const.removeElement:i.childNodes.splice(a,1),i.subsets&&i.subsets.forEach((function(e){e.oldValue>a?e.oldValue-=1:e.oldValue===a?e.delete=!0:e.oldValuea&&(e.oldValue+e.length-1===a?e.length--:(u.push({newValue:e.newValue+a-e.oldValue,oldValue:a,length:e.length-a+e.oldValue-1}),e.length=a-e.oldValue))})),s=i;break;case n._const.addElement:var f=(r=t[n._const.route].slice()).splice(r.length-1,1)[0];s=null===(o=g(e,r))||void 0===o?void 0:o.node,l=d(t[n._const.element]),s.childNodes||(s.childNodes=[]),f>=s.childNodes.length?s.childNodes.push(l):s.childNodes.splice(f,0,l),s.subsets&&s.subsets.forEach((function(e){if(e.oldValue>=f)e.oldValue+=1;else if(e.oldValuef){var t=e.oldValue+e.length-f;u.push({newValue:e.newValue+e.length-t,oldValue:f+1,length:t}),e.length-=t}}));break;case n._const.removeTextElement:i.childNodes.splice(a,1),"TEXTAREA"===i.nodeName&&delete i.value,i.subsets&&i.subsets.forEach((function(e){e.oldValue>a?e.oldValue-=1:e.oldValue===a?e.delete=!0:e.oldValuea&&(e.oldValue+e.length-1===a?e.length--:(u.push({newValue:e.newValue+a-e.oldValue,oldValue:a,length:e.length-a+e.oldValue-1}),e.length=a-e.oldValue))})),s=i;break;case n._const.addTextElement:var p=(r=t[n._const.route].slice()).splice(r.length-1,1)[0];l={nodeName:"#text",data:t[n._const.value]},(s=g(e,r).node).childNodes||(s.childNodes=[]),p>=s.childNodes.length?s.childNodes.push(l):s.childNodes.splice(p,0,l),"TEXTAREA"===s.nodeName&&(s.value=t[n._const.newValue]),s.subsets&&s.subsets.forEach((function(e){if(e.oldValue>=p&&(e.oldValue+=1),e.oldValuep){var t=e.oldValue+e.length-p;u.push({newValue:e.newValue+e.length-t,oldValue:p+1,length:t}),e.length-=t}}));break;default:console.log("unknown action")}s.subsets&&(s.subsets=s.subsets.filter((function(e){return!e.delete&&e.oldValue!==e.newValue})),u.length&&(s.subsets=s.subsets.concat(u))),n.postVirtualDiffApply({node:h.node,diff:h.diff,newNode:l})}(e,t,n)})),!0}function N(e,t){void 0===t&&(t={valueDiffing:!0});var n={nodeName:e.nodeName};if(V(e,"Text")||V(e,"Comment"))n.data=e.data;else{if(e.attributes&&e.attributes.length>0)n.attributes={},Array.prototype.slice.call(e.attributes).forEach((function(e){return n.attributes[e.name]=e.value}));if(e.childNodes&&e.childNodes.length>0)n.childNodes=[],Array.prototype.slice.call(e.childNodes).forEach((function(e){return n.childNodes.push(N(e,t))}));t.valueDiffing&&(V(e,"HTMLTextAreaElement")&&(n.value=e.value),V(e,"HTMLInputElement")&&["radio","checkbox"].includes(e.type.toLowerCase())&&void 0!==e.checked?n.checked=e.checked:(V(e,"HTMLButtonElement")||V(e,"HTMLDataElement")||V(e,"HTMLInputElement")||V(e,"HTMLLIElement")||V(e,"HTMLMeterElement")||V(e,"HTMLOptionElement")||V(e,"HTMLProgressElement")||V(e,"HTMLParamElement"))&&(n.value=e.value),V(e,"HTMLOptionElement")&&(n.selected=e.selected))}return n}var w=/<\s*\/*[a-zA-Z:_][a-zA-Z0-9:_\-.]*\s*(?:"[^"]*"['"]*|'[^']*'['"]*|[^'"/>])*\/*\s*>|/g,y=/\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;function E(e){return e.replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&")}var k={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},x=function(e,t){var n={nodeName:"",attributes:{}},o=!1,s=e.match(/<\/?([^\s]+?)[/\s>]/);if(s&&(n.nodeName=t||"svg"===s[1]?s[1]:s[1].toUpperCase(),(k[s[1]]||"/"===e.charAt(e.length-2))&&(o=!0),n.nodeName.startsWith("!--"))){var i=e.indexOf("--\x3e");return{type:"comment",node:{nodeName:"#comment",data:-1!==i?e.slice(4,i):""},voidElement:o}}for(var a=new RegExp(y),c=null,l=!1;!l;)if(null===(c=a.exec(e)))l=!0;else if(c[0].trim())if(c[1]){var r=c[1].trim(),u=[r,""];r.indexOf("=")>-1&&(u=r.split("=")),n.attributes[u[0]]=u[1],a.lastIndex--}else c[2]&&(n.attributes[c[2]]=c[3].trim().substring(1,c[3].length-1));return{type:"tag",node:n,voidElement:o}},T=function(e,t){void 0===t&&(t={valueDiffing:!0,caseSensitive:!1});var n,o=[],s=-1,i=[],a=!1;if(0!==e.indexOf("<")){var c=e.indexOf("<");o.push({nodeName:"#text",data:-1===c?e:e.substring(0,c)})}return e.replace(w,(function(c,l){var r="/"!==c.charAt(1),u=c.startsWith("\x3c!--"),d=l+c.length,h=e.charAt(d);if(u){var f=x(c,t.caseSensitive).node;if(s<0)return o.push(f),"";var p=i[s];return p&&f.nodeName&&(p.node.childNodes||(p.node.childNodes=[]),p.node.childNodes.push(f)),""}if(r){if("svg"===(n=x(c,t.caseSensitive||a)).node.nodeName&&(a=!0),s++,!n.voidElement&&h&&"<"!==h){n.node.childNodes||(n.node.childNodes=[]);var m=E(e.slice(d,e.indexOf("<",d)));n.node.childNodes.push({nodeName:"#text",data:m}),t.valueDiffing&&"TEXTAREA"===n.node.nodeName&&(n.node.value=m)}0===s&&n.node.nodeName&&o.push(n.node);var _=i[s-1];_&&n.node.nodeName&&(_.node.childNodes||(_.node.childNodes=[]),_.node.childNodes.push(n.node)),i[s]=n}if((!r||n.voidElement)&&(s>-1&&(n.voidElement||t.caseSensitive&&n.node.nodeName===c.slice(2,-1)||!t.caseSensitive&&n.node.nodeName.toUpperCase()===c.slice(2,-1).toUpperCase())&&--s>-1&&("svg"===n.node.nodeName&&(a=!1),n=i[s]),"<"!==h&&h)){var v=-1===s?o:i[s].node.childNodes||[],V=e.indexOf("<",d);m=E(e.slice(d,-1===V?void 0:V));v.push({nodeName:"#text",data:m})}return""})),o[0]},O=function(){function e(e,t,n){this.options=n,this.t1="undefined"!=typeof Element&&V(e,"Element")?N(e,this.options):"string"==typeof e?T(e,this.options):JSON.parse(JSON.stringify(e)),this.t2="undefined"!=typeof Element&&V(t,"Element")?N(t,this.options):"string"==typeof t?T(t,this.options):JSON.parse(JSON.stringify(t)),this.diffcount=0,this.foundAll=!1,this.debug&&(this.t1Orig="undefined"!=typeof Element&&V(e,"Element")?N(e,this.options):"string"==typeof e?T(e,this.options):JSON.parse(JSON.stringify(e)),this.t2Orig="undefined"!=typeof Element&&V(t,"Element")?N(t,this.options):"string"==typeof t?T(t,this.options):JSON.parse(JSON.stringify(t))),this.tracker=new _}return e.prototype.init=function(){return this.findDiffs(this.t1,this.t2)},e.prototype.findDiffs=function(e,t){var n;do{if(this.options.debug&&(this.diffcount+=1,this.diffcount>this.options.diffcap))throw new Error("surpassed diffcap:".concat(JSON.stringify(this.t1Orig)," -> ").concat(JSON.stringify(this.t2Orig)));0===(n=this.findNextDiff(e,t,[])).length&&(h(e,t)||(this.foundAll?console.error("Could not find remaining diffs!"):(this.foundAll=!0,u(e),n=this.findNextDiff(e,t,[])))),n.length>0&&(this.foundAll=!1,this.tracker.add(n),b(e,n,this.options))}while(n.length>0);return this.tracker.list},e.prototype.findNextDiff=function(e,t,n){var o,s;if(this.options.maxDepth&&n.length>this.options.maxDepth)return[];if(!e.outerDone){if(o=this.findOuterDiff(e,t,n),this.options.filterOuterDiff&&(s=this.options.filterOuterDiff(e,t,o))&&(o=s),o.length>0)return e.outerDone=!0,o;e.outerDone=!0}if(Object.prototype.hasOwnProperty.call(e,"data"))return[];if(!e.innerDone){if((o=this.findInnerDiff(e,t,n)).length>0)return o;e.innerDone=!0}if(this.options.valueDiffing&&!e.valueDone){if((o=this.findValueDiff(e,t,n)).length>0)return e.valueDone=!0,o;e.valueDone=!0}return[]},e.prototype.findOuterDiff=function(e,t,n){var o,s,i,a,c,l,r=[];if(e.nodeName!==t.nodeName){if(!n.length)throw new Error("Top level nodes have to be of the same kind.");return[(new v).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,d(e)).setValue(this.options._const.newValue,d(t)).setValue(this.options._const.route,n)]}if(n.length&&this.options.diffcap0&&(c=this.attemptGroupRelocation(e,t,u,n,r)).length>0)return c}for(var f=0;fs.length?("#text"===p.nodeName?c.push((new v).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,n.concat(l)).setValue(this.options._const.value,p.data)):c.push((new v).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.element,d(p)).setValue(this.options._const.route,n.concat(l))),o.splice(f,1),f-=1,l-=1,a-=1):o.lengthy+1&&"#text"===_[y+1].nodeName;)if(y+=1,V[N].data===_[y].data){r=!0;break}r||b.push((new v).setValue(this.options._const.action,this.options._const.modifyTextElement).setValue(this.options._const.route,o.concat(w)).setValue(this.options._const.oldValue,l.data).setValue(this.options._const.newValue,V[N].data))}}else b.push((new v).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,o.concat(w)).setValue(this.options._const.value,l.data)),h.splice(w,1),_.splice(w,1),g=Math.min(h.length,m.length),w-=1,N-=1;else!0===m[N]?b.push((new v).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,d(l)).setValue(this.options._const.newValue,d(V[N])).setValue(this.options._const.route,o.concat(w))):(b.push((new v).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.route,o.concat(w)).setValue(this.options._const.element,d(l))),h.splice(w,1),_.splice(w,1),g=Math.min(h.length,m.length),w-=1,N-=1);else if(!0===m[N])"#text"===(l=V[N]).nodeName?(b.push((new v).setValue(this.options._const.action,this.options._const.addTextElement).setValue(this.options._const.route,o.concat(w)).setValue(this.options._const.value,l.data)),h.splice(w,0,!0),_.splice(w,0,{nodeName:"#text",data:l.data}),g=Math.min(h.length,m.length)):(b.push((new v).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.route,o.concat(w)).setValue(this.options._const.element,d(l))),h.splice(w,0,!0),_.splice(w,0,d(l)),g=Math.min(h.length,m.length));else if(h[w]!==m[N]){if(b.length>0)return b;if(c=n[h[w]],(a=Math.min(c.newValue,_.length-c.length))!==c.oldValue){i=!1;for(var E=0;E entering ".concat(e),t)},e.prototype.fout=function(e,t){this.log("│<──┘ generated return value",t),this.padding=this.padding.substring(0,this.padding.length-this.pad.length)},e.prototype.format=function(e,t){return"".concat(function(e){for(var t="".concat(e);t.length<4;)t="0".concat(e);return t}(t),"> ").concat(this.padding).concat(e)},e.prototype.log=function(){for(var e=arguments,t=[],n=0;n":e instanceof Array?"[".concat(e.map(o).join(","),"]"):e.toString()||e.valueOf()||"":""},s=t.map(o).join(", ");this.messages.push(this.format(s,this.tick++))},e.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,"".concat(this.messages.join("\n"),"\n").concat(e)},e}();return e.DiffDOM=D,e.TraceLogger=L,e.nodeToObj=N,e.stringToObj=T,e}({}); //# sourceMappingURL=diffDOM.js.map diff --git a/browser/diffDOM.js.map b/browser/diffDOM.js.map index 28f95ab..5dff292 100644 --- a/browser/diffDOM.js.map +++ b/browser/diffDOM.js.map @@ -1 +1 @@ -{"version":3,"file":"diffDOM.js","sources":["../src/diffDOM/dom/fromVirtual.ts","../src/diffDOM/dom/apply.ts","../src/diffDOM/dom/undo.ts","../node_modules/tslib/tslib.es6.js","../src/diffDOM/virtual/helpers.ts","../src/diffDOM/helpers.ts","../src/diffDOM/virtual/apply.ts","../src/diffDOM/virtual/fromDOM.ts","../src/diffDOM/virtual/fromString.ts","../src/diffDOM/virtual/diff.ts","../src/diffDOM/index.ts","../src/TraceLogger.ts"],"sourcesContent":["import { DiffDOMOptions, elementNodeType, textNodeType } from \"../types\"\n\nexport function objToNode(\n objNode: elementNodeType,\n insideSvg: boolean,\n options: DiffDOMOptions\n) {\n let node: Element | Text | Comment\n if (objNode.nodeName === \"#text\") {\n node = options.document.createTextNode((objNode as textNodeType).data)\n } else if (objNode.nodeName === \"#comment\") {\n node = options.document.createComment((objNode as textNodeType).data)\n } else {\n if (insideSvg) {\n node = options.document.createElementNS(\n \"http://www.w3.org/2000/svg\",\n objNode.nodeName\n )\n } else if (objNode.nodeName.toLowerCase() === \"svg\") {\n node = options.document.createElementNS(\n \"http://www.w3.org/2000/svg\",\n \"svg\"\n )\n insideSvg = true\n } else {\n node = options.document.createElement(objNode.nodeName)\n }\n if (objNode.attributes) {\n Object.entries(objNode.attributes).forEach(([key, value]) =>\n (node as Element).setAttribute(key, value)\n )\n }\n if (objNode.childNodes) {\n node = node as Element\n objNode.childNodes.forEach(\n (childNode: elementNodeType | textNodeType) =>\n node.appendChild(objToNode(childNode, insideSvg, options))\n )\n }\n if (options.valueDiffing) {\n if (\n objNode.value &&\n (node instanceof HTMLButtonElement ||\n node instanceof HTMLDataElement ||\n node instanceof HTMLInputElement ||\n node instanceof HTMLLIElement ||\n node instanceof HTMLMeterElement ||\n node instanceof HTMLOptionElement ||\n node instanceof HTMLProgressElement ||\n node instanceof HTMLParamElement)\n ) {\n node.value = objNode.value\n }\n if (objNode.checked && node instanceof HTMLInputElement) {\n node.checked = objNode.checked\n }\n if (objNode.selected && node instanceof HTMLOptionElement) {\n node.selected = objNode.selected\n }\n }\n }\n return node\n}\n","import { DiffDOMOptions, diffType, nodeType } from \"../types\"\nimport { Diff } from \"../helpers\"\n\nimport { objToNode } from \"./fromVirtual\"\n\n// ===== Apply a diff =====\n\nconst getFromRoute = (\n node: Element,\n route: number[]\n): Element | Text | false => {\n route = route.slice()\n while (route.length > 0) {\n const c = route.splice(0, 1)[0]\n node = node.childNodes[c] as Element\n }\n return node\n}\n\nexport function applyDiff(\n tree: Element,\n diff: diffType,\n options: DiffDOMOptions // {preDiffApply, postDiffApply, textDiff, valueDiffing, _const}\n) {\n const action = diff[options._const.action] as string | number\n const route = diff[options._const.route] as number[]\n let node\n\n if (\n ![options._const.addElement, options._const.addTextElement].includes(\n action\n )\n ) {\n // For adding nodes, we calculate the route later on. It's different because it includes the position of the newly added item.\n node = getFromRoute(tree, route)\n }\n\n let newNode\n let reference: Element\n let nodeArray\n\n // pre-diff hook\n const info = {\n diff,\n node,\n }\n\n if (options.preDiffApply(info)) {\n return true\n }\n\n switch (action) {\n case options._const.addAttribute:\n if (!node || !(node instanceof Element)) {\n return false\n }\n node.setAttribute(\n diff[options._const.name] as string,\n diff[options._const.value] as string\n )\n break\n case options._const.modifyAttribute:\n if (!node || !(node instanceof Element)) {\n return false\n }\n node.setAttribute(\n diff[options._const.name] as string,\n diff[options._const.newValue] as string\n )\n if (\n node instanceof HTMLInputElement &&\n diff[options._const.name] === \"value\"\n ) {\n node.value = diff[options._const.newValue] as string\n }\n break\n case options._const.removeAttribute:\n if (!node || !(node instanceof Element)) {\n return false\n }\n node.removeAttribute(diff[options._const.name] as string)\n break\n case options._const.modifyTextElement:\n if (!node || !(node instanceof Text)) {\n return false\n }\n options.textDiff(\n node,\n node.data,\n diff[options._const.oldValue] as string,\n diff[options._const.newValue] as string\n )\n if (node.parentNode instanceof HTMLTextAreaElement) {\n node.parentNode.value = diff[options._const.newValue] as string\n }\n break\n case options._const.modifyValue:\n if (!node || typeof node.value === \"undefined\") {\n return false\n }\n node.value = diff[options._const.newValue]\n break\n case options._const.modifyComment:\n if (!node || !(node instanceof Comment)) {\n return false\n }\n options.textDiff(\n node,\n node.data,\n diff[options._const.oldValue] as string,\n diff[options._const.newValue] as string\n )\n break\n case options._const.modifyChecked:\n if (!node || typeof node.checked === \"undefined\") {\n return false\n }\n node.checked = diff[options._const.newValue]\n break\n case options._const.modifySelected:\n if (!node || typeof node.selected === \"undefined\") {\n return false\n }\n node.selected = diff[options._const.newValue]\n break\n case options._const.replaceElement: {\n const insideSvg =\n (\n diff[options._const.newValue] as nodeType\n ).nodeName.toLowerCase() === \"svg\" ||\n node.parentNode.namespaceURI === \"http://www.w3.org/2000/svg\"\n node.parentNode.replaceChild(\n objToNode(\n diff[options._const.newValue] as nodeType,\n insideSvg,\n options\n ),\n node\n )\n break\n }\n case options._const.relocateGroup:\n nodeArray = Array(\n ...new Array(diff[options._const.groupLength])\n ).map(() =>\n node.removeChild(\n node.childNodes[diff[options._const.from] as number]\n )\n )\n nodeArray.forEach((childNode, index) => {\n if (index === 0) {\n reference =\n node.childNodes[diff[options._const.to] as number]\n }\n node.insertBefore(childNode, reference || null)\n })\n break\n case options._const.removeElement:\n node.parentNode.removeChild(node)\n break\n case options._const.addElement: {\n const parentRoute = route.slice()\n const c: number = parentRoute.splice(parentRoute.length - 1, 1)[0]\n node = getFromRoute(tree, parentRoute)\n if (!(node instanceof Element)) {\n return false\n }\n node.insertBefore(\n objToNode(\n diff[options._const.element] as nodeType,\n node.namespaceURI === \"http://www.w3.org/2000/svg\",\n options\n ),\n node.childNodes[c] || null\n )\n break\n }\n case options._const.removeTextElement: {\n if (!node || node.nodeType !== 3) {\n return false\n }\n const parentNode = node.parentNode\n parentNode.removeChild(node)\n if (parentNode instanceof HTMLTextAreaElement) {\n parentNode.value = \"\"\n }\n break\n }\n case options._const.addTextElement: {\n const parentRoute = route.slice()\n const c: number = parentRoute.splice(parentRoute.length - 1, 1)[0]\n newNode = options.document.createTextNode(\n diff[options._const.value] as string\n )\n node = getFromRoute(tree, parentRoute)\n if (!node.childNodes) {\n return false\n }\n node.insertBefore(newNode, node.childNodes[c] || null)\n if (node.parentNode instanceof HTMLTextAreaElement) {\n node.parentNode.value = diff[options._const.value] as string\n }\n break\n }\n default:\n console.log(\"unknown action\")\n }\n\n // if a new node was created, we might be interested in its\n // post diff hook\n options.postDiffApply({\n diff: info.diff,\n node: info.node,\n newNode,\n })\n\n return true\n}\n\nexport function applyDOM(\n tree: Element,\n diffs: (Diff | diffType)[],\n options: DiffDOMOptions\n) {\n return diffs.every((diff: Diff | diffType) =>\n applyDiff(tree, diff as diffType, options)\n )\n}\n","import { DiffDOMOptions, diffType } from \"../types\"\nimport { Diff } from \"../helpers\"\nimport { applyDiff } from \"./apply\"\n\n// ===== Undo a diff =====\n\nfunction swap(obj: object, p1: string | number, p2: string | number) {\n const tmp = obj[p1]\n obj[p1] = obj[p2]\n obj[p2] = tmp\n}\n\nfunction undoDiff(\n tree: Element,\n diff: diffType,\n options: DiffDOMOptions // {preDiffApply, postDiffApply, textDiff, valueDiffing, _const}\n) {\n switch (diff[options._const.action]) {\n case options._const.addAttribute:\n diff[options._const.action] = options._const.removeAttribute\n applyDiff(tree, diff, options)\n break\n case options._const.modifyAttribute:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.removeAttribute:\n diff[options._const.action] = options._const.addAttribute\n applyDiff(tree, diff, options)\n break\n case options._const.modifyTextElement:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.modifyValue:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.modifyComment:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.modifyChecked:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.modifySelected:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.replaceElement:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.relocateGroup:\n swap(diff, options._const.from, options._const.to)\n applyDiff(tree, diff, options)\n break\n case options._const.removeElement:\n diff[options._const.action] = options._const.addElement\n applyDiff(tree, diff, options)\n break\n case options._const.addElement:\n diff[options._const.action] = options._const.removeElement\n applyDiff(tree, diff, options)\n break\n case options._const.removeTextElement:\n diff[options._const.action] = options._const.addTextElement\n applyDiff(tree, diff, options)\n break\n case options._const.addTextElement:\n diff[options._const.action] = options._const.removeTextElement\n applyDiff(tree, diff, options)\n break\n default:\n console.log(\"unknown action\")\n }\n}\n\nexport function undoDOM(\n tree: Element,\n diffs: (diffType | Diff)[],\n options: DiffDOMOptions\n) {\n diffs = diffs.slice()\n diffs.reverse()\n diffs.forEach((diff: diffType | Diff) => {\n undoDiff(tree, diff as diffType, options)\n })\n}\n","/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\r\n function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\r\n var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\r\n var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\r\n var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\r\n var _, done = false;\r\n for (var i = decorators.length - 1; i >= 0; i--) {\r\n var context = {};\r\n for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\r\n for (var p in contextIn.access) context.access[p] = contextIn.access[p];\r\n context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\r\n var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\r\n if (kind === \"accessor\") {\r\n if (result === void 0) continue;\r\n if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\r\n if (_ = accept(result.get)) descriptor.get = _;\r\n if (_ = accept(result.set)) descriptor.set = _;\r\n if (_ = accept(result.init)) initializers.push(_);\r\n }\r\n else if (_ = accept(result)) {\r\n if (kind === \"field\") initializers.push(_);\r\n else descriptor[key] = _;\r\n }\r\n }\r\n if (target) Object.defineProperty(target, contextIn.name, descriptor);\r\n done = true;\r\n};\r\n\r\nexport function __runInitializers(thisArg, initializers, value) {\r\n var useValue = arguments.length > 2;\r\n for (var i = 0; i < initializers.length; i++) {\r\n value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);\r\n }\r\n return useValue ? value : void 0;\r\n};\r\n\r\nexport function __propKey(x) {\r\n return typeof x === \"symbol\" ? x : \"\".concat(x);\r\n};\r\n\r\nexport function __setFunctionName(f, name, prefix) {\r\n if (typeof name === \"symbol\") name = name.description ? \"[\".concat(name.description, \"]\") : \"\";\r\n return Object.defineProperty(f, \"name\", { configurable: true, value: prefix ? \"\".concat(prefix, \" \", name) : name });\r\n};\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n","import {\n diffNodeType,\n elementDiffNodeType,\n elementNodeType,\n nodeType,\n subsetType,\n textDiffNodeType,\n textNodeType,\n} from \"../types\"\nimport { Diff } from \"../helpers\"\nconst elementDescriptors = (el: diffNodeType) => {\n const output = []\n output.push(el.nodeName)\n if (el.nodeName !== \"#text\" && el.nodeName !== \"#comment\") {\n el = el as elementDiffNodeType\n if (el.attributes) {\n if (el.attributes[\"class\"]) {\n output.push(\n `${el.nodeName}.${el.attributes[\"class\"].replace(\n / /g,\n \".\"\n )}`\n )\n }\n if (el.attributes.id) {\n output.push(`${el.nodeName}#${el.attributes.id}`)\n }\n }\n }\n return output\n}\n\nconst findUniqueDescriptors = (li: diffNodeType[]) => {\n const uniqueDescriptors = {}\n const duplicateDescriptors = {}\n\n li.forEach((node: nodeType) => {\n elementDescriptors(node).forEach((descriptor) => {\n const inUnique = descriptor in uniqueDescriptors\n const inDupes = descriptor in duplicateDescriptors\n if (!inUnique && !inDupes) {\n uniqueDescriptors[descriptor] = true\n } else if (inUnique) {\n delete uniqueDescriptors[descriptor]\n duplicateDescriptors[descriptor] = true\n }\n })\n })\n\n return uniqueDescriptors\n}\n\nexport const uniqueInBoth = (l1: diffNodeType[], l2: diffNodeType[]) => {\n const l1Unique = findUniqueDescriptors(l1)\n const l2Unique = findUniqueDescriptors(l2)\n const inBoth = {}\n\n Object.keys(l1Unique).forEach((key) => {\n if (l2Unique[key]) {\n inBoth[key] = true\n }\n })\n\n return inBoth\n}\n\nexport const removeDone = (tree: elementDiffNodeType) => {\n delete tree.outerDone\n delete tree.innerDone\n delete tree.valueDone\n if (tree.childNodes) {\n return tree.childNodes.every(removeDone)\n } else {\n return true\n }\n}\n\nexport const cleanNode = (diffNode: diffNodeType) => {\n if (Object.prototype.hasOwnProperty.call(diffNode, \"data\")) {\n const textNode: textNodeType = {\n nodeName: diffNode.nodeName === \"#text\" ? \"#text\" : \"#comment\",\n data: (diffNode as textDiffNodeType).data,\n }\n return textNode\n } else {\n const elementNode: elementNodeType = {\n nodeName: diffNode.nodeName,\n }\n diffNode = diffNode as elementDiffNodeType\n if (Object.prototype.hasOwnProperty.call(diffNode, \"attributes\")) {\n elementNode.attributes = { ...diffNode.attributes }\n }\n if (Object.prototype.hasOwnProperty.call(diffNode, \"checked\")) {\n elementNode.checked = diffNode.checked\n }\n if (Object.prototype.hasOwnProperty.call(diffNode, \"value\")) {\n elementNode.value = diffNode.value\n }\n if (Object.prototype.hasOwnProperty.call(diffNode, \"selected\")) {\n elementNode.selected = diffNode.selected\n }\n if (Object.prototype.hasOwnProperty.call(diffNode, \"childNodes\")) {\n elementNode.childNodes = diffNode.childNodes.map((diffChildNode) =>\n cleanNode(diffChildNode)\n )\n }\n return elementNode\n }\n}\n\nexport const isEqual = (e1: diffNodeType, e2: diffNodeType) => {\n if (\n ![\"nodeName\", \"value\", \"checked\", \"selected\", \"data\"].every(\n (element) => {\n if (e1[element] !== e2[element]) {\n return false\n }\n return true\n }\n )\n ) {\n return false\n }\n if (Object.prototype.hasOwnProperty.call(e1, \"data\")) {\n // Comment or Text\n return true\n }\n e1 = e1 as elementDiffNodeType\n e2 = e2 as elementDiffNodeType\n if (Boolean(e1.attributes) !== Boolean(e2.attributes)) {\n return false\n }\n\n if (Boolean(e1.childNodes) !== Boolean(e2.childNodes)) {\n return false\n }\n if (e1.attributes) {\n const e1Attributes = Object.keys(e1.attributes)\n const e2Attributes = Object.keys(e2.attributes)\n\n if (e1Attributes.length !== e2Attributes.length) {\n return false\n }\n if (\n !e1Attributes.every((attribute) => {\n if (\n (e1 as elementDiffNodeType).attributes[attribute] !==\n (e2 as elementDiffNodeType).attributes[attribute]\n ) {\n return false\n }\n return true\n })\n ) {\n return false\n }\n }\n if (e1.childNodes) {\n if (e1.childNodes.length !== e2.childNodes.length) {\n return false\n }\n if (\n !e1.childNodes.every((childNode: nodeType, index: number) =>\n isEqual(childNode, e2.childNodes[index])\n )\n ) {\n return false\n }\n }\n\n return true\n}\n\nexport const roughlyEqual = (\n e1: diffNodeType,\n e2: diffNodeType,\n uniqueDescriptors: { [key: string]: boolean },\n sameSiblings: boolean,\n preventRecursion = false\n) => {\n if (!e1 || !e2) {\n return false\n }\n\n if (e1.nodeName !== e2.nodeName) {\n return false\n }\n\n if ([\"#text\", \"#comment\"].includes(e1.nodeName)) {\n // Note that we initially don't care what the text content of a node is,\n // the mere fact that it's the same tag and \"has text\" means it's roughly\n // equal, and then we can find out the true text difference later.\n return preventRecursion\n ? true\n : (e1 as textDiffNodeType).data === (e2 as textDiffNodeType).data\n }\n\n e1 = e1 as elementDiffNodeType\n e2 = e2 as elementDiffNodeType\n\n if (e1.nodeName in uniqueDescriptors) {\n return true\n }\n\n if (e1.attributes && e2.attributes) {\n if (e1.attributes.id) {\n if (e1.attributes.id !== e2.attributes.id) {\n return false\n } else {\n const idDescriptor = `${e1.nodeName}#${e1.attributes.id}`\n if (idDescriptor in uniqueDescriptors) {\n return true\n }\n }\n }\n if (\n e1.attributes[\"class\"] &&\n e1.attributes[\"class\"] === e2.attributes[\"class\"]\n ) {\n const classDescriptor = `${e1.nodeName}.${e1.attributes[\n \"class\"\n ].replace(/ /g, \".\")}`\n if (classDescriptor in uniqueDescriptors) {\n return true\n }\n }\n }\n\n if (sameSiblings) {\n return true\n }\n\n const nodeList1 = e1.childNodes ? e1.childNodes.slice().reverse() : []\n const nodeList2 = e2.childNodes ? e2.childNodes.slice().reverse() : []\n\n if (nodeList1.length !== nodeList2.length) {\n return false\n }\n\n if (preventRecursion) {\n return nodeList1.every(\n (element: nodeType, index: number) =>\n element.nodeName === nodeList2[index].nodeName\n )\n } else {\n // note: we only allow one level of recursion at any depth. If 'preventRecursion'\n // was not set, we must explicitly force it to true for child iterations.\n const childUniqueDescriptors = uniqueInBoth(nodeList1, nodeList2)\n return nodeList1.every((element: nodeType, index: number) =>\n roughlyEqual(\n element,\n nodeList2[index],\n childUniqueDescriptors,\n true,\n true\n )\n )\n }\n}\n\n/**\n * based on https://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring#JavaScript\n */\nconst findCommonSubsets = (\n c1: diffNodeType[],\n c2: diffNodeType[],\n marked1: boolean[],\n marked2: boolean[]\n) => {\n let lcsSize = 0\n let index: number[] = []\n const c1Length = c1.length\n const c2Length = c2.length\n\n const // set up the matching table\n matches = Array(...new Array(c1Length + 1)).map(() => [])\n\n const uniqueDescriptors = uniqueInBoth(c1, c2)\n\n let // If all of the elements are the same tag, id and class, then we can\n // consider them roughly the same even if they have a different number of\n // children. This will reduce removing and re-adding similar elements.\n subsetsSame = c1Length === c2Length\n\n if (subsetsSame) {\n c1.some((element: nodeType, i: number) => {\n const c1Desc = elementDescriptors(element)\n const c2Desc = elementDescriptors(c2[i])\n if (c1Desc.length !== c2Desc.length) {\n subsetsSame = false\n return true\n }\n c1Desc.some((description, i) => {\n if (description !== c2Desc[i]) {\n subsetsSame = false\n return true\n }\n })\n if (!subsetsSame) {\n return true\n }\n })\n }\n\n // fill the matches with distance values\n for (let c1Index = 0; c1Index < c1Length; c1Index++) {\n const c1Element = c1[c1Index]\n for (let c2Index = 0; c2Index < c2Length; c2Index++) {\n const c2Element = c2[c2Index]\n if (\n !marked1[c1Index] &&\n !marked2[c2Index] &&\n roughlyEqual(\n c1Element,\n c2Element,\n uniqueDescriptors,\n subsetsSame\n )\n ) {\n matches[c1Index + 1][c2Index + 1] = matches[c1Index][c2Index]\n ? matches[c1Index][c2Index] + 1\n : 1\n if (matches[c1Index + 1][c2Index + 1] >= lcsSize) {\n lcsSize = matches[c1Index + 1][c2Index + 1]\n index = [c1Index + 1, c2Index + 1]\n }\n } else {\n matches[c1Index + 1][c2Index + 1] = 0\n }\n }\n }\n\n if (lcsSize === 0) {\n return false\n }\n\n return {\n oldValue: index[0] - lcsSize,\n newValue: index[1] - lcsSize,\n length: lcsSize,\n }\n}\n\nconst makeBooleanArray = (n: number, v: boolean) =>\n Array(...new Array(n)).map(() => v)\n\n/**\n * Generate arrays that indicate which node belongs to which subset,\n * or whether it's actually an orphan node, existing in only one\n * of the two trees, rather than somewhere in both.\n *\n * So if t1 =
, t2 =
.\n * The longest subset is \"
\" (length 2), so it will group 0.\n * The second longest is \"\" (length 1), so it will be group 1.\n * gaps1 will therefore be [1,0,0] and gaps2 [0,0,1].\n *\n * If an element is not part of any group, it will stay being 'true', which\n * is the initial value. For example:\n * t1 =


, t2 =
\n *\n * The \"

\" and \"\" do only show up in one of the two and will\n * therefore be marked by \"true\". The remaining parts are parts of the\n * groups 0 and 1:\n * gaps1 = [1, true, 0, 0], gaps2 = [true, 0, 0, 1]\n *\n */\nexport const getGapInformation = (\n t1: elementDiffNodeType,\n t2: elementDiffNodeType,\n stable: subsetType[]\n) => {\n const gaps1: (true | number)[] = t1.childNodes\n ? (makeBooleanArray(t1.childNodes.length, true) as true[])\n : []\n const gaps2: (true | number)[] = t2.childNodes\n ? (makeBooleanArray(t2.childNodes.length, true) as true[])\n : []\n let group = 0\n\n // give elements from the same subset the same group number\n stable.forEach((subset: subsetType) => {\n const endOld = subset.oldValue + subset.length\n const endNew = subset.newValue + subset.length\n\n for (let j = subset.oldValue; j < endOld; j += 1) {\n gaps1[j] = group\n }\n for (let j = subset.newValue; j < endNew; j += 1) {\n gaps2[j] = group\n }\n group += 1\n })\n\n return {\n gaps1,\n gaps2,\n }\n}\n\n/**\n * Find all matching subsets, based on immediate child differences only.\n */\nconst markBoth = (marked1, marked2, subset: subsetType, i: number) => {\n marked1[subset.oldValue + i] = true\n marked2[subset.newValue + i] = true\n}\n\nexport const markSubTrees = (\n oldTree: elementDiffNodeType,\n newTree: elementDiffNodeType\n) => {\n // note: the child lists are views, and so update as we update old/newTree\n const oldChildren = oldTree.childNodes ? oldTree.childNodes : []\n\n const newChildren = newTree.childNodes ? newTree.childNodes : []\n const marked1 = makeBooleanArray(oldChildren.length, false)\n const marked2 = makeBooleanArray(newChildren.length, false)\n const subsets = []\n\n const returnIndex = function () {\n return arguments[1]\n }\n\n let foundAllSubsets = false\n\n while (!foundAllSubsets) {\n const subset = findCommonSubsets(\n oldChildren,\n newChildren,\n marked1,\n marked2\n )\n if (subset) {\n subsets.push(subset)\n const subsetArray = Array(...new Array(subset.length)).map(\n returnIndex\n )\n subsetArray.forEach((item) =>\n markBoth(marked1, marked2, subset, item)\n )\n } else {\n foundAllSubsets = true\n }\n }\n\n oldTree.subsets = subsets\n oldTree.subsetsAge = 100\n return subsets\n}\n\nexport class DiffTracker {\n list: Diff[]\n constructor() {\n this.list = []\n }\n\n add(diffs: Diff[]) {\n this.list.push(...diffs)\n }\n forEach(fn: (Diff) => void) {\n this.list.forEach((li: Diff) => fn(li))\n }\n}\n\n//export const elementHasValue = (element: Element) : boolean => element instanceof HTMLButtonElement || element instanceof HTMLDataElement || element instanceof HTMLInputElement || element instanceof HTMLLIElement || element instanceof HTMLMeterElement || element instanceof HTMLOptionElement || element instanceof HTMLProgressElement || element instanceof HTMLParamElement\n","import { elementNodeType } from \"./types\"\n\nexport class Diff {\n constructor(options = {}) {\n Object.entries(options).forEach(([key, value]) => (this[key] = value))\n }\n\n toString() {\n return JSON.stringify(this)\n }\n\n setValue(\n aKey: string | number,\n aValue:\n | string\n | number\n | boolean\n | number[]\n | { [key: string]: string | { [key: string]: string } }\n | elementNodeType\n ) {\n this[aKey] = aValue\n return this\n }\n}\n","import { DiffDOMOptions, elementNodeType, nodeType, subsetType } from \"../types\"\nimport { Diff } from \"../helpers\"\nimport { cleanNode } from \"./helpers\"\n// ===== Apply a virtual diff =====\n\nfunction getFromVirtualRoute(tree: elementNodeType, route: number[]) {\n let node = tree\n let parentNode\n let nodeIndex\n\n route = route.slice()\n while (route.length > 0) {\n nodeIndex = route.splice(0, 1)[0]\n parentNode = node\n node = node.childNodes ? node.childNodes[nodeIndex] : undefined\n }\n return {\n node,\n parentNode,\n nodeIndex,\n }\n}\n\nfunction applyVirtualDiff(\n tree: elementNodeType,\n diff: Diff,\n options: DiffDOMOptions // {preVirtualDiffApply, postVirtualDiffApply, _const}\n) {\n let node, parentNode, nodeIndex\n\n if (\n ![options._const.addElement, options._const.addTextElement].includes(\n diff[options._const.action]\n )\n ) {\n // For adding nodes, we calculate the route later on. It's different because it includes the position of the newly added item.\n const routeInfo = getFromVirtualRoute(tree, diff[options._const.route])\n node = routeInfo.node\n parentNode = routeInfo.parentNode\n nodeIndex = routeInfo.nodeIndex\n }\n\n const newSubsets: subsetType[] = []\n\n // pre-diff hook\n const info = {\n diff,\n node,\n }\n\n if (options.preVirtualDiffApply(info)) {\n return true\n }\n\n let newNode\n let nodeArray\n let route\n\n switch (diff[options._const.action]) {\n case options._const.addAttribute:\n if (!node.attributes) {\n node.attributes = {}\n }\n\n node.attributes[diff[options._const.name]] =\n diff[options._const.value]\n\n if (diff[options._const.name] === \"checked\") {\n node.checked = true\n } else if (diff[options._const.name] === \"selected\") {\n node.selected = true\n } else if (\n node.nodeName === \"INPUT\" &&\n diff[options._const.name] === \"value\"\n ) {\n node.value = diff[options._const.value]\n }\n\n break\n case options._const.modifyAttribute:\n node.attributes[diff[options._const.name]] =\n diff[options._const.newValue]\n break\n case options._const.removeAttribute:\n delete node.attributes[diff[options._const.name]]\n\n if (Object.keys(node.attributes).length === 0) {\n delete node.attributes\n }\n\n if (diff[options._const.name] === \"checked\") {\n node.checked = false\n } else if (diff[options._const.name] === \"selected\") {\n delete node.selected\n } else if (\n node.nodeName === \"INPUT\" &&\n diff[options._const.name] === \"value\"\n ) {\n delete node.value\n }\n\n break\n case options._const.modifyTextElement:\n node.data = diff[options._const.newValue]\n if (parentNode.nodeName === \"TEXTAREA\") {\n parentNode.value = diff[options._const.newValue]\n }\n break\n case options._const.modifyValue:\n node.value = diff[options._const.newValue]\n break\n case options._const.modifyComment:\n node.data = diff[options._const.newValue]\n break\n case options._const.modifyChecked:\n node.checked = diff[options._const.newValue]\n break\n case options._const.modifySelected:\n node.selected = diff[options._const.newValue]\n break\n case options._const.replaceElement:\n newNode = cleanNode(diff[options._const.newValue])\n parentNode.childNodes[nodeIndex] = newNode\n break\n case options._const.relocateGroup:\n nodeArray = node.childNodes\n .splice(\n diff[options._const.from],\n diff[options._const.groupLength]\n )\n .reverse()\n nodeArray.forEach((movedNode: nodeType) =>\n node.childNodes.splice(diff[options._const.to], 0, movedNode)\n )\n if (node.subsets) {\n node.subsets.forEach((map: subsetType) => {\n if (\n diff[options._const.from] < diff[options._const.to] &&\n map.oldValue <= diff[options._const.to] &&\n map.oldValue > diff[options._const.from]\n ) {\n map.oldValue -= diff[options._const.groupLength]\n const splitLength =\n map.oldValue + map.length - diff[options._const.to]\n if (splitLength > 0) {\n // new insertion splits map.\n newSubsets.push({\n oldValue:\n diff[options._const.to] +\n diff[options._const.groupLength],\n newValue:\n map.newValue + map.length - splitLength,\n length: splitLength,\n })\n map.length -= splitLength\n }\n } else if (\n diff[options._const.from] > diff[options._const.to] &&\n map.oldValue > diff[options._const.to] &&\n map.oldValue < diff[options._const.from]\n ) {\n map.oldValue += diff[options._const.groupLength]\n const splitLength =\n map.oldValue + map.length - diff[options._const.to]\n if (splitLength > 0) {\n // new insertion splits map.\n newSubsets.push({\n oldValue:\n diff[options._const.to] +\n diff[options._const.groupLength],\n newValue:\n map.newValue + map.length - splitLength,\n length: splitLength,\n })\n map.length -= splitLength\n }\n } else if (map.oldValue === diff[options._const.from]) {\n map.oldValue = diff[options._const.to]\n }\n })\n }\n\n break\n case options._const.removeElement:\n parentNode.childNodes.splice(nodeIndex, 1)\n if (parentNode.subsets) {\n parentNode.subsets.forEach((map: subsetType) => {\n if (map.oldValue > nodeIndex) {\n map.oldValue -= 1\n } else if (map.oldValue === nodeIndex) {\n map.delete = true\n } else if (\n map.oldValue < nodeIndex &&\n map.oldValue + map.length > nodeIndex\n ) {\n if (map.oldValue + map.length - 1 === nodeIndex) {\n map.length--\n } else {\n newSubsets.push({\n newValue:\n map.newValue + nodeIndex - map.oldValue,\n oldValue: nodeIndex,\n length:\n map.length - nodeIndex + map.oldValue - 1,\n })\n map.length = nodeIndex - map.oldValue\n }\n }\n })\n }\n node = parentNode\n break\n case options._const.addElement: {\n route = diff[options._const.route].slice()\n const c: number = route.splice(route.length - 1, 1)[0]\n node = getFromVirtualRoute(tree, route)?.node\n newNode = cleanNode(diff[options._const.element])\n\n if (!node.childNodes) {\n node.childNodes = []\n }\n\n if (c >= node.childNodes.length) {\n node.childNodes.push(newNode)\n } else {\n node.childNodes.splice(c, 0, newNode)\n }\n if (node.subsets) {\n node.subsets.forEach((map: subsetType) => {\n if (map.oldValue >= c) {\n map.oldValue += 1\n } else if (\n map.oldValue < c &&\n map.oldValue + map.length > c\n ) {\n const splitLength = map.oldValue + map.length - c\n newSubsets.push({\n newValue: map.newValue + map.length - splitLength,\n oldValue: c + 1,\n length: splitLength,\n })\n map.length -= splitLength\n }\n })\n }\n break\n }\n case options._const.removeTextElement:\n parentNode.childNodes.splice(nodeIndex, 1)\n if (parentNode.nodeName === \"TEXTAREA\") {\n delete parentNode.value\n }\n if (parentNode.subsets) {\n parentNode.subsets.forEach((map: subsetType) => {\n if (map.oldValue > nodeIndex) {\n map.oldValue -= 1\n } else if (map.oldValue === nodeIndex) {\n map.delete = true\n } else if (\n map.oldValue < nodeIndex &&\n map.oldValue + map.length > nodeIndex\n ) {\n if (map.oldValue + map.length - 1 === nodeIndex) {\n map.length--\n } else {\n newSubsets.push({\n newValue:\n map.newValue + nodeIndex - map.oldValue,\n oldValue: nodeIndex,\n length:\n map.length - nodeIndex + map.oldValue - 1,\n })\n map.length = nodeIndex - map.oldValue\n }\n }\n })\n }\n node = parentNode\n break\n case options._const.addTextElement: {\n route = diff[options._const.route].slice()\n const c: number = route.splice(route.length - 1, 1)[0]\n newNode = {\n nodeName: \"#text\",\n data: diff[options._const.value],\n }\n node = getFromVirtualRoute(tree, route).node\n if (!node.childNodes) {\n node.childNodes = []\n }\n\n if (c >= node.childNodes.length) {\n node.childNodes.push(newNode)\n } else {\n node.childNodes.splice(c, 0, newNode)\n }\n if (node.nodeName === \"TEXTAREA\") {\n node.value = diff[options._const.newValue]\n }\n if (node.subsets) {\n node.subsets.forEach((map: subsetType) => {\n if (map.oldValue >= c) {\n map.oldValue += 1\n }\n if (map.oldValue < c && map.oldValue + map.length > c) {\n const splitLength = map.oldValue + map.length - c\n newSubsets.push({\n newValue: map.newValue + map.length - splitLength,\n oldValue: c + 1,\n length: splitLength,\n })\n map.length -= splitLength\n }\n })\n }\n break\n }\n default:\n console.log(\"unknown action\")\n }\n\n if (node.subsets) {\n node.subsets = node.subsets.filter(\n (map: subsetType) => !map.delete && map.oldValue !== map.newValue\n )\n if (newSubsets.length) {\n node.subsets = node.subsets.concat(newSubsets)\n }\n }\n\n options.postVirtualDiffApply({\n node: info.node,\n diff: info.diff,\n newNode,\n })\n\n return\n}\n\nexport function applyVirtual(\n tree: elementNodeType,\n diffs: Diff[],\n options: DiffDOMOptions\n) {\n diffs.forEach((diff: Diff) => {\n applyVirtualDiff(tree, diff, options)\n })\n return true\n}\n","import { DiffDOMOptionsPartial, elementNodeType, textNodeType } from \"../types\"\n\nexport function nodeToObj(\n aNode: Element,\n options: DiffDOMOptionsPartial = { valueDiffing: true }\n) {\n const objNode: elementNodeType | textNodeType = {\n nodeName: aNode.nodeName,\n }\n if (aNode instanceof Text || aNode instanceof Comment) {\n ;(objNode as unknown as textNodeType).data = aNode.data\n } else {\n if (aNode.attributes && aNode.attributes.length > 0) {\n objNode.attributes = {}\n const nodeArray = Array.prototype.slice.call(aNode.attributes)\n nodeArray.forEach(\n (attribute) =>\n (objNode.attributes[attribute.name] = attribute.value)\n )\n }\n if (aNode.childNodes && aNode.childNodes.length > 0) {\n objNode.childNodes = []\n const nodeArray = Array.prototype.slice.call(aNode.childNodes)\n nodeArray.forEach((childNode) =>\n objNode.childNodes.push(nodeToObj(childNode, options))\n )\n }\n if (options.valueDiffing) {\n if (aNode instanceof HTMLTextAreaElement) {\n objNode.value = aNode.value\n }\n if (\n aNode instanceof HTMLInputElement &&\n [\"radio\", \"checkbox\"].includes(aNode.type.toLowerCase()) &&\n aNode.checked !== undefined\n ) {\n objNode.checked = aNode.checked\n } else if (\n aNode instanceof HTMLButtonElement ||\n aNode instanceof HTMLDataElement ||\n aNode instanceof HTMLInputElement ||\n aNode instanceof HTMLLIElement ||\n aNode instanceof HTMLMeterElement ||\n aNode instanceof HTMLOptionElement ||\n aNode instanceof HTMLProgressElement ||\n aNode instanceof HTMLParamElement\n ) {\n objNode.value = aNode.value\n }\n if (aNode instanceof HTMLOptionElement) {\n objNode.selected = aNode.selected\n }\n }\n }\n return objNode\n}\n","import { DiffDOMOptionsPartial, nodeType } from \"../types\"\n\n// from html-parse-stringify (MIT)\n\nconst tagRE =\n /<\\s*\\/*[a-zA-Z:_][a-zA-Z0-9:_\\-.]*\\s*(?:\"[^\"]*\"['\"]*|'[^']*'['\"]*|[^'\"/>])*\\/*\\s*>|/g\n\nconst attrRE = /\\s([^'\"/\\s><]+?)[\\s/>]|([^\\s=]+)=\\s?(\".*?\"|'.*?')/g\n\nfunction unescape(string: string) {\n return string\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/&/g, \"&\")\n}\n\n// create optimized lookup object for\n// void elements as listed here:\n// https://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements\nconst lookup = {\n area: true,\n base: true,\n br: true,\n col: true,\n embed: true,\n hr: true,\n img: true,\n input: true,\n keygen: true,\n link: true,\n menuItem: true,\n meta: true,\n param: true,\n source: true,\n track: true,\n wbr: true,\n}\n\nconst parseTag = (tag: string, caseSensitive: boolean) => {\n const res = {\n nodeName: \"\",\n attributes: {},\n }\n let voidElement = false\n let type = \"tag\"\n\n let tagMatch = tag.match(/<\\/?([^\\s]+?)[/\\s>]/)\n if (tagMatch) {\n res.nodeName =\n caseSensitive || tagMatch[1] === \"svg\"\n ? tagMatch[1]\n : tagMatch[1].toUpperCase()\n if (lookup[tagMatch[1]] || tag.charAt(tag.length - 2) === \"/\") {\n voidElement = true\n }\n\n // handle comment tag\n if (res.nodeName.startsWith(\"!--\")) {\n const endIndex = tag.indexOf(\"-->\")\n return {\n type: \"comment\",\n node: {\n nodeName: \"#comment\",\n data: endIndex !== -1 ? tag.slice(4, endIndex) : \"\",\n },\n voidElement,\n }\n }\n }\n\n let reg = new RegExp(attrRE)\n let result = null\n let done = false\n while (!done) {\n result = reg.exec(tag)\n\n if (result === null) {\n done = true\n } else if (result[0].trim()) {\n if (result[1]) {\n let attr = result[1].trim()\n let arr = [attr, \"\"]\n\n if (attr.indexOf(\"=\") > -1) arr = attr.split(\"=\")\n res.attributes[arr[0]] = arr[1]\n reg.lastIndex--\n } else if (result[2])\n res.attributes[result[2]] = result[3]\n .trim()\n .substring(1, result[3].length - 1)\n }\n }\n\n return {\n type,\n node: res,\n voidElement,\n }\n}\n\nexport const stringToObj = (\n html: string,\n options: DiffDOMOptionsPartial = {\n valueDiffing: true,\n caseSensitive: false,\n }\n) => {\n const result: nodeType[] = []\n let current: { type: string; node: nodeType; voidElement: boolean }\n let level = -1\n const arr: { type: string; node: nodeType; voidElement: boolean }[] = []\n let inComponent = false,\n insideSvg = false\n\n // handle text at top level\n if (html.indexOf(\"<\") !== 0) {\n const end = html.indexOf(\"<\")\n result.push({\n nodeName: \"#text\",\n data: end === -1 ? html : html.substring(0, end),\n })\n }\n\n html.replace(tagRE, (tag: string, index: number) => {\n if (inComponent) {\n if (tag !== ``) {\n return \"\"\n } else {\n inComponent = false\n }\n }\n const isOpen = tag.charAt(1) !== \"/\"\n const isComment = tag.startsWith(\"/g\n\nconst attrRE = /\\s([^'\"/\\s><]+?)[\\s/>]|([^\\s=]+)=\\s?(\".*?\"|'.*?')/g\n\nfunction unescape(string: string) {\n return string\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/&/g, \"&\")\n}\n\n// create optimized lookup object for\n// void elements as listed here:\n// https://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements\nconst lookup = {\n area: true,\n base: true,\n br: true,\n col: true,\n embed: true,\n hr: true,\n img: true,\n input: true,\n keygen: true,\n link: true,\n menuItem: true,\n meta: true,\n param: true,\n source: true,\n track: true,\n wbr: true,\n}\n\nconst parseTag = (tag: string, caseSensitive: boolean) => {\n const res = {\n nodeName: \"\",\n attributes: {},\n }\n let voidElement = false\n let type = \"tag\"\n\n let tagMatch = tag.match(/<\\/?([^\\s]+?)[/\\s>]/)\n if (tagMatch) {\n res.nodeName =\n caseSensitive || tagMatch[1] === \"svg\"\n ? tagMatch[1]\n : tagMatch[1].toUpperCase()\n if (lookup[tagMatch[1]] || tag.charAt(tag.length - 2) === \"/\") {\n voidElement = true\n }\n\n // handle comment tag\n if (res.nodeName.startsWith(\"!--\")) {\n const endIndex = tag.indexOf(\"-->\")\n return {\n type: \"comment\",\n node: {\n nodeName: \"#comment\",\n data: endIndex !== -1 ? tag.slice(4, endIndex) : \"\",\n },\n voidElement,\n }\n }\n }\n\n let reg = new RegExp(attrRE)\n let result = null\n let done = false\n while (!done) {\n result = reg.exec(tag)\n\n if (result === null) {\n done = true\n } else if (result[0].trim()) {\n if (result[1]) {\n let attr = result[1].trim()\n let arr = [attr, \"\"]\n\n if (attr.indexOf(\"=\") > -1) arr = attr.split(\"=\")\n res.attributes[arr[0]] = arr[1]\n reg.lastIndex--\n } else if (result[2])\n res.attributes[result[2]] = result[3]\n .trim()\n .substring(1, result[3].length - 1)\n }\n }\n\n return {\n type,\n node: res,\n voidElement,\n }\n}\n\nexport const stringToObj = (\n html: string,\n options: DiffDOMOptionsPartial = {\n valueDiffing: true,\n caseSensitive: false,\n }\n) => {\n const result: nodeType[] = []\n let current: { type: string; node: nodeType; voidElement: boolean }\n let level = -1\n const arr: { type: string; node: nodeType; voidElement: boolean }[] = []\n let inComponent = false,\n insideSvg = false\n\n // handle text at top level\n if (html.indexOf(\"<\") !== 0) {\n const end = html.indexOf(\"<\")\n result.push({\n nodeName: \"#text\",\n data: end === -1 ? html : html.substring(0, end),\n })\n }\n\n html.replace(tagRE, (tag: string, index: number) => {\n if (inComponent) {\n if (tag !== ``) {\n return \"\"\n } else {\n inComponent = false\n }\n }\n const isOpen = tag.charAt(1) !== \"/\"\n const isComment = tag.startsWith(\"