UNPKG

uhtml

Version:

A minimalistic library to create fast and reactive Web pages

2 lines (1 loc) 2.6 kB
const{isArray:t}=Array,{assign:e,freeze:s}=Object,r=1,n=2,i=3,c=8,o=10,a=11,h=42,p=new Set(["plaintext","script","style","textarea","title","xmp"]),l=new Set(["area","base","br","col","embed","hr","img","input","keygen","link","menuitem","meta","param","source","track","wbr"]),u=s({}),d=s([]),m=(t,e)=>(t.children===d&&(t.children=[]),t.children.push(e),e.parent=t,e),x=(t,e,s)=>{t.props===u&&(t.props={}),t.props[e]=s},$=(t,e,s)=>{t!==e&&s.push(t)},g=(t,e)=>{t.children=e.map(w,t)},S=(s,r,n)=>{switch(r.length){case n:g(s,r[n-1]);case n-1:{const i=r[n-2];t(i)?g(s,i):s.props=e({},i)}}return s};function w(t){const e=f(t);return e.parent=this,e}const f=t=>{switch(t[0]){case 8:return new b(t[1]);case 10:return new O(t[1]);case 3:return new J(t[1]);case 42:return S(new N,t,3);case 1:return S(new j(t[1],!!t[2]),t,5);case 11:{const e=new k;return 1<t.length&&(e.children=t[1].map(w,e)),e}}};class y{constructor(t){this.type=t,this.parent=null}toJSON(){return[this.type,this.data]}}class b extends y{constructor(t){super(8),this.data=t}toString(){return`\x3c!--${this.data}--\x3e`}}class O extends y{constructor(t){super(10),this.data=t}toString(){return`<!${this.data}>`}}class J extends y{constructor(t){super(3),this.data=t}toString(){return this.data}}class N extends y{constructor(){super(42),this.name="template",this.props=u,this.children=d}toJSON(){const t=[42];return $(this.props,u,t),$(this.children,d,t),t}toString(){let t="";for(const e in this.props){const s=this.props[e];null!=s&&("boolean"==typeof s?s&&(t+=` ${e}`):t+=` ${e}="${s}"`)}return`<template${t}>${this.children.join("")}</template>`}}class j extends y{constructor(t,e=!1){super(1),this.name=t,this.xml=e,this.props=u,this.children=d}toJSON(){const t=[1,this.name,+this.xml];return $(this.props,u,t),$(this.children,d,t),t}toString(){const{xml:t,name:e,props:s,children:r}=this,{length:n}=r;let i=`<${e}`;for(const e in s){const r=s[e];null!=r&&("boolean"==typeof r?r&&(i+=t?` ${e}=""`:` ${e}`):i+=` ${e}="${r}"`)}if(n){i+=">";for(let s=!t&&p.has(e),c=0;c<n;c++)i+=s?r[c].data:r[c];i+=`</${e}>`}else i+=t?" />":l.has(e)?">":`></${e}>`;return i}}class k extends y{constructor(){super(11),this.name="#fragment",this.children=d}toJSON(){const t=[11];return $(this.children,d,t),t}toString(){return this.children.join("")}}export{n as ATTRIBUTE,c as COMMENT,h as COMPONENT,b as Comment,N as Component,o as DOCUMENT_TYPE,O as DocumentType,r as ELEMENT,j as Element,a as FRAGMENT,k as Fragment,y as Node,i as TEXT,p as TEXT_ELEMENTS,J as Text,l as VOID_ELEMENTS,m as append,d as children,f as fromJSON,x as prop,u as props};