0x1
Version:
2 lines (1 loc) • 1.89 kB
JavaScript
function O(q,j={}){let{children:b=[],className:z,id:B,...G}=j,A=document.createElement(q);if(B)A.id=B;if(z)A.className=z;if(Object.entries(G).forEach(([D,F])=>{if(D.startsWith("on")&&typeof F==="function"){let I=D.substring(2).toLowerCase();A.addEventListener(I,F)}else if(D==="style"&&typeof F==="object")Object.entries(F).forEach(([I,J])=>{A.style[I]=J});else if(F!==void 0&&F!==null)A.setAttribute(D,String(F))}),Array.isArray(b))b.forEach((D)=>{if(typeof D==="string")A.appendChild(document.createTextNode(D));else if(D instanceof Node)A.appendChild(D)});else if(b){if(typeof b==="string")A.appendChild(document.createTextNode(b));else if(b instanceof Node)A.appendChild(b)}return A}function Q(q){return q}function R(q,j){let b;if(typeof q==="function")b=q({});else b=q;if(j.innerHTML="",typeof b==="string")j.innerHTML=b;else if(b instanceof Node)j.appendChild(b);else console.error("Invalid component content type")}function S(q,j,b){let z=j(b),B;if(z===null)B=document.createDocumentFragment();else if(typeof z==="string"){let G=document.createElement("span");G.textContent=z,B=G}else B=z;if(q.parentNode)q.parentNode.replaceChild(B,q);return B}function U(q){let j=document.createDocumentFragment(),{children:b}=q;if(Array.isArray(b))b.forEach((z)=>{if(typeof z==="string")j.appendChild(document.createTextNode(z));else if(z instanceof Node)j.appendChild(z)});else if(b){if(typeof b==="string")j.appendChild(document.createTextNode(b));else if(b instanceof Node)j.appendChild(b)}return j}function P(q){let j=document.createElement("template");return j.innerHTML=q.trim(),j.content.firstElementChild}function W(q,j,b={}){return O(q,{...b,children:[j]})}function X(q,...j){let b=q.reduce((z,B,G)=>{let A=j[G]||"";return z+B+A},"");return P(b)}export{S as updateComponent,W as textElement,X as template,R as mount,P as fromHTML,O as createElement,Q as createComponent,U as DOMFragment};