forked from WebReflection/uhtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.js
1 lines (1 loc) · 6.58 KB
/
async.js
1
self.uhtml=function(e){"use strict";const{isArray:t}=Array,n=(e,r)=>{const s=[];for(const{length:o}=e;r<o;r++)s.push(t(e[r])?n(e[r],0):e[r]);return Promise.all(s)};var r=e=>{function t(t,n){return e.apply(this,[t].concat(n))}return function(e){return n(arguments,1).then(t.bind(this,e))}},s=e=>({get:t=>e.get(t),set:(t,n)=>(e.set(t,n),n)});const o=/([^\s\\>"'=]+)\s*=\s*(['"]?)$/,l=/^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i,i=/<[a-z][^>]+$/i,a=/>[^<>]*$/,c=/<([a-z]+[a-z0-9:._-]*)([^>]*?)(\/>)/gi,u=/\s+$/,d=(e,t)=>0<t--&&(i.test(e[t])||!a.test(e[t])&&d(e,t)),p=(e,t,n)=>l.test(t)?e:`<${t}${n.replace(u,"")}></${t}>`;const{isArray:f}=Array,{indexOf:h,slice:m}=[],g=(e,t)=>111===e.nodeType?1/t<0?t?(({firstChild:e,lastChild:t})=>{const n=document.createRange();return n.setStartAfter(e),n.setEndAfter(t),n.deleteContents(),e})(e):e.lastChild:t?e.valueOf():e.firstChild:e;const v=(e,t)=>{let n,r=t.slice(2);return!(t in e)&&t.toLowerCase()in e&&(r=r.toLowerCase()),t=>{const s=f(t)?t:[t,!1];n!==s[0]&&(n&&e.removeEventListener(r,n,s[1]),(n=s[0])&&e.addEventListener(r,n,s[1]))}},y=({childNodes:e},t)=>e[t],w=(e,t,n)=>((e,t,n,r,s)=>{const o=n.length;let l=t.length,i=o,a=0,c=0,u=null;for(;a<l||c<i;)if(l===a){const t=i<o?c?r(n[c-1],-0).nextSibling:r(n[i-c],0):s;for(;c<i;)e.insertBefore(r(n[c++],1),t)}else if(i===c)for(;a<l;)u&&u.has(t[a])||e.removeChild(r(t[a],-1)),a++;else if(t[a]===n[c])a++,c++;else if(t[l-1]===n[i-1])l--,i--;else if(t[a]===n[i-1]&&n[c]===t[l-1]){const s=r(t[--l],-1).nextSibling;e.insertBefore(r(n[c++],1),r(t[a++],-1).nextSibling),e.insertBefore(r(n[--i],1),s),t[l]=n[i]}else{if(!u){u=new Map;let e=c;for(;e<i;)u.set(n[e],e++)}if(u.has(t[a])){const s=u.get(t[a]);if(c<s&&s<i){let o=a,d=1;for(;++o<l&&o<i&&u.get(t[o])===s+d;)d++;if(d>s-c){const o=r(t[a],0);for(;c<s;)e.insertBefore(r(n[c++],1),o)}else e.replaceChild(r(n[c++],1),r(t[a++],-1))}else a++}else e.removeChild(r(t[a++],-1))}return n})(e.parentNode,t,n,g,e),b=(e,t)=>{switch(t[0]){case"?":return((e,t,n)=>r=>{n!==!!r&&((n=!!r)?e.setAttribute(t,""):e.removeAttribute(t))})(e,t.slice(1),!1);case".":return((e,t)=>"dataset"===t?(({dataset:e})=>t=>{for(const n in t){const r=t[n];null==r?delete e[n]:e[n]=r}})(e):n=>{e[t]=n})(e,t.slice(1));case"@":return v(e,"on"+t.slice(1));case"o":if("n"===t[1])return v(e,t)}switch(t){case"ref":return(e=>{let t;return n=>{t!==n&&(t=n,"function"==typeof n?n(e):n.current=e)}})(e);case"aria":return(e=>t=>{for(const n in t){const r="role"===n?n:`aria-${n}`,s=t[n];null==s?e.removeAttribute(r):e.setAttribute(r,s)}})(e)}return((e,t)=>{let n,r=!0;const s=document.createAttributeNS(null,t);return t=>{n!==t&&(n=t,null==n?r||(e.removeAttributeNode(s),r=!0):(s.value=t,r&&(e.setAttributeNodeNS(s),r=!1)))}})(e,t)};function N(e){const{type:t,path:n}=e,r=n.reduceRight(y,this);return"node"===t?(e=>{let t,n,r=[];const s=o=>{switch(typeof o){case"string":case"number":case"boolean":t!==o&&(t=o,n||(n=document.createTextNode("")),n.data=o,r=w(e,r,[n]));break;case"object":case"undefined":if(null==o){t!=o&&(t=o,r=w(e,r,[]));break}if(f(o)){t=o,0===o.length?r=w(e,r,[]):"object"==typeof o[0]?r=w(e,r,o):s(String(o));break}t!==o&&"ELEMENT_NODE"in o&&(t=o,r=w(e,r,11===o.nodeType?m.call(o.childNodes):[o]));break;case"function":s(o(e))}};return s})(r):"attr"===t?b(r,e.name):(e=>{let t;return n=>{t!=n&&(t=n,e.textContent=null==n?"":n)}})(r)}const $=e=>document.createElementNS("http://www.w3.org/1999/xhtml",e),x=(e,t)=>("svg"===t?k:C)(e),C=e=>{const t=$("template");return t.innerHTML=e,t.content},k=e=>{const{content:t}=$("template"),n=$("div");n.innerHTML='<svg xmlns="http://www.w3.org/2000/svg">'+e+"</svg>";const{childNodes:r}=n.firstChild;let{length:s}=r;for(;s--;)t.appendChild(r[0]);return t},A=1!=document.importNode.length,E=A?(e,t,n)=>document.importNode(x(e,t),!0):x,T=A?e=>document.createTreeWalker(e,129,null,!1):e=>document.createTreeWalker(e,129),M=e=>{const t=[];let{parentNode:n}=e;for(;n;)t.push(h.call(n.childNodes,e)),n=(e=n).parentNode;return t},O="isµ",L=s(new WeakMap),S=/^(?:plaintext|script|style|textarea|title|xmp)$/i,W=(e,t)=>{const n=((e,t,n)=>{const r=[],{length:s}=e;for(let n=1;n<s;n++){const s=e[n-1];r.push(o.test(s)&&d(e,n)?s.replace(o,((e,r,s)=>`${t}${n-1}=${s||'"'}${r}${s?"":'"'}`)):`${s}\x3c!--${t}${n-1}--\x3e`)}r.push(e[s-1]);const l=r.join("").trim();return n?l:l.replace(c,p)})(t,O,"svg"===e),r=E(n,e),s=T(r),l=[],i=t.length-1;let a=0,u=`isµ${a}`;for(;a<i;){const e=s.nextNode();if(!e)throw`bad template: ${n}`;if(8===e.nodeType)e.data===u&&(l.push({type:"node",path:M(e)}),u="isµ"+ ++a);else{for(;e.hasAttribute(u);)l.push({type:"attr",path:M(e),name:e.getAttribute(u)}),e.removeAttribute(u),u="isµ"+ ++a;S.test(e.tagName)&&e.textContent.trim()===`\x3c!--${u}--\x3e`&&(e.textContent="",l.push({type:"text",path:M(e)}),u="isµ"+ ++a)}}return{content:r,nodes:l}},j=(e,t)=>{const{content:n,nodes:r}=L.get(t)||L.set(t,W(e,t)),s=document.importNode(n,!0);return{content:s,updates:r.map(N,s)}},B=(e,{type:t,template:n,values:r})=>{const{length:s}=r;P(e,r,s);let{entry:o}=e;o&&o.template===n&&o.type===t||(e.entry=o=((e,t)=>{const{content:n,updates:r}=j(e,t);return{type:e,template:t,content:n,updates:r,wire:null}})(t,n));const{content:l,updates:i,wire:a}=o;for(let e=0;e<s;e++)i[e](r[e]);return a||(o.wire=(e=>{const{childNodes:t}=e,{length:n}=t;if(n<2)return n?t[0]:e;const r=m.call(t,0);return{ELEMENT_NODE:1,nodeType:111,firstChild:r[0],lastChild:r[n-1],valueOf(){if(t.length!==n){let t=0;for(;t<n;)e.appendChild(r[t++])}return e}}})(l))},P=({stack:e},t,n)=>{for(let r=0;r<n;r++){const n=t[r];n instanceof z?t[r]=B(e[r]||(e[r]={stack:[],entry:null,wire:null}),n):f(n)?P(e[r]||(e[r]={stack:[],entry:null,wire:null}),n,n.length):e[r]=null}n<e.length&&e.splice(n)};function z(e,t,n){this.type=e,this.template=t,this.values=n}const{create:H,defineProperties:_}=Object,D=e=>{const t=s(new WeakMap);return _(((t,...n)=>new z(e,t,n)),{for:{value(n,r){const s=t.get(n)||t.set(n,H(null));return s[r]||(s[r]=(t=>(n,...r)=>B(t,{type:e,template:n,values:r}))({stack:[],entry:null,wire:null}))}},node:{value:(t,...n)=>B({stack:[],entry:null,wire:null},{type:e,template:t,values:n}).valueOf()}})},R=s(new WeakMap),q=D("html"),F=D("svg"),{defineProperties:G}=Object,I=e=>{const t=s(new WeakMap);return G(r(e),{for:{value(n,s){const o=e.for(n,s);return t.get(o)||t.set(o,r(o))}},node:{value:r(e.node)}})},J=I(q),K=I(F);return e.Hole=z,e.html=J,e.render=(e,t)=>{const n="function"==typeof t?t():t;return Promise.resolve(n).then((t=>((e,t)=>{const n="function"==typeof t?t():t,r=R.get(e)||R.set(e,{stack:[],entry:null,wire:null}),s=n instanceof z?B(r,n):n;return s!==r.wire&&(r.wire=s,e.textContent="",e.appendChild(s.valueOf())),e})(e,t)))},e.svg=K,e}({});