-
Notifications
You must be signed in to change notification settings - Fork 1
/
BadDOM.min.js
1 lines (1 loc) · 1.43 KB
/
BadDOM.min.js
1
window.vDiff=((e,t)=>{const i={settings:{original:e},replace(i,n=e){const o=(new DOMParser).parseFromString(t,"text/html").body.childNodes[0];o.nodeName===e.nodeName?this.iterate(e,o):e.parentElement.replaceChild(o,e)},iterate(t,i,n){(t||i)&&(this.checkAdditions(t,i,n),t&&i&&t.nodeName!==i.nodeName?this.checkNodeName(t,i):t&&i&&t.nodeName===i.nodeName&&(this.checkTextContent(t,i),3!==t.nodeType&&8!==e.nodeType&&this.checkAttributes(t,i))),t&&i&&(t.childNodes&&i.childNodes?this.settings.lengthDifferentiator=[...e.childNodes,...i.childNodes]:this.settings.lengthDifferentiator=null,(this.settings.lengthDifferentiator||[]).forEach((e,n)=>{this.settings.lengthDifferentiator&&this.iterate(t.childNodes[n],i.childNodes[n],t,i)}))},checkNodeName(e,t){const i=t.cloneNode(!0);e.parentElement.replaceChild(i,e)},checkAttributes(e,t){const i=e.attributes||[],n=Object.keys(i).map(e=>i[e]),o=t.attributes||[],s=Object.keys(o).map(e=>o[e]);n.forEach(i=>null!==t.getAttribute(i.name)?e.setAttribute(i.name,t.getAttribute(i.name)):e.removeAttribute(i.name)),s.forEach(i=>e.getAttribute(i.name)!==t.getAttribute(i.name)&&e.setAttribute(i.name,t.getAttribute(i.name)))},checkTextContent(e,t){e.nodeValue!==t.nodeValue&&(e.textContent=t.textContent)},checkAdditions(e,t,i=this.settings.original){if(t&&void 0===e){const e=t.cloneNode(!0);3!==i.nodeType&&8!==i.nodeType&&i.appendChild(e)}else e&&void 0===t&&e.parentElement.removeChild(e)}};Object.create(i).replace(e,t)});