diffhtml-middleware-verify-state
Version:
Verifies render state middleware, useful for sanity checking
1 lines • 2.73 kB
JavaScript
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(e=e||self).verifyState=o()}(this,function(){"use strict";var e="undefined"!=typeof global?global:window,o=globalThis[Symbol.for("diffHTML")];const{NodeCache:d,decodeEntities:h,Pool:n}=o.Internals,i=o["createTree"],u=(e,o)=>e instanceof Node&&e.attributes?e.attributes[o].value||e[o]:e.attributes[o],f=o=>e=>{if(o.debug)throw new Error(e);console.warn(e)};e.document&&document.createElement("div");const m=t=>(t.childNodes.forEach((e,o)=>{if(11===e.nodeType)return t.childNodes.splice.apply(t.childNodes,[o,1,...e.childNodes]),void e.childNodes.forEach(e=>m(e));m(e)}),t),p=(o,e)=>{const t=d.get(e);t&&(t.nodeName.toLowerCase()!==e.nodeName?o(`[Mismatched DOM Node] ${e.nodeName} has an invalid DOM Node association with `+t.nodeName):n.memory.protected.has(e)||o(`[Unprotected DOM Node] ${e.nodeName} was not protected in memory`)),e.childNodes.forEach(e=>p(o,e))},E=(o,t,e,d,n)=>{const i=f(o);let a=Object.keys(n.attributes||{}).sort().filter(Boolean),s=Object.keys(d.attributes||{}).sort().filter(Boolean);var l=n instanceof Node,r=l?"ON DOM NODE":"OLD";l&&(d=m(d));const c=h(n.nodeValue||"").replace(/\r?\n|\r/g,""),N=h(d.nodeValue||"").replace(/\r?\n|\r/g,"");if(n.nodeName.toLowerCase()!==d.nodeName.toLowerCase()&&11!==d.nodeType?i(`[Mismatched nodeName] ${r}: ${n.nodeName} NEW TREE: `+d.nodeName):n.nodeValue&&d.nodeValue&&c!==N?i(`[Mismatched nodeValue] ${r}: ${c} NEW TREE: `+N):n.nodeType!==d.nodeType&&11!==d.nodeType?i(`[Mismatched nodeType] ${r}: ${n.nodeType} NEW TREE: `+d.nodeType):n.childNodes.length!==d.childNodes.length&&i(`[Mismatched childNodes length] ${r}: ${n.childNodes.length} NEW TREE: `+d.childNodes.length),l&&oldTree.attributes&&(a=[...n.attributes].map(e=>String(e.name)).sort()),11!==d.nodeType){for(let e=0;e<a.length;e++){const c=u(n,a[e])||"",N=u(d,s[e])||"";a[e].toLowerCase()!==s[e].toLowerCase()?s[e]?a[e]?i(`[Unexpected attribute] ${r}: ${a[e]}="${c}" IN NEW TREE: ${s[e]}="${N}"`):i(`[Unexpected attribute] IN NEW TREE: ${s[e]}="${N}"`):i(`[Unexpected attribute] ${r}: ${a[e]}="${c}"`):l||c===N||i(`[Unexpected attribute] ${r}: ${a[e]}="${c}" IN NEW TREE: ${s[e]}="${N}"`)}for(let e=0;e<n.childNodes.length;e++)n.childNodes[e]&&d.childNodes[e]&&E(o,t,n.childNodes[e],d.childNodes[e],n.childNodes[e])}p(i,n)};function a(o){let t=[];for(let e=0;e<o.childNodes.length;e++){const d=o.childNodes[e];3===d.nodeType&&!d.nodeValue.trim()||t.push(d)}return 1===t.length?t[0]:1<t.length?i(o.childNodes):i(o)}return(i={})=>function(){return e=>{const{mount:o,state:t}=e,d=a(e.oldTree||t.oldTree),n=a(e.newTree);d&&n&&E(i,e,d,n,d),e.onceEnded(()=>{E(i,e,o,n,d)})}}});