@asimojs/lml
Version:
LML - List Markup Language
2 lines (1 loc) • 6.32 kB
JavaScript
(function(A,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(A=typeof globalThis<"u"?globalThis:A||self,h(A.lml={}))})(this,function(A){"use strict";function h(s){if(Array.isArray(s)){if(s.length===0)return"fragment";const c=s[0];if(typeof c=="string"&&c.length>1){const p=c[0];if(p===T)return"element";if(p===P)return"component";if(p===L||p===$)return"invalid"}return"fragment"}else if(typeof s=="string")return"text";return"invalid"}const T="#",P="*",L="@",$="!",M="!",U=".",X=/^(\#|\*|\!|\@)(\w+\:)?([\w\-]+)(\+[\w\-]+)?(\.[\.\w\-]+)*(\!.+)?$/;function w(s,c){if(s==null)return"";const p=h(s);if(p==="text")return i(s);if(p==="invalid")return e(`Invalid LML node: ${JSON.stringify(s)}`);const u=p==="fragment",y=s,d=y.length;if(d===0)return"";if(!u){const r=y[0].match(X);if(r){const o=r[1],n=r[2],f=r[3],g=r[4],E=r[5],b=r[6],z=n?n.slice(0,-1):"",x=g?g.slice(1):"";let v;if(E){const m=E.split(U);v=[];for(const R of m)R!=""&&v.push(R)}let S=1,a;const k=y[S];if(k&&typeof k=="object"&&!Array.isArray(k)){a=k,S++,(x||v||b)&&(x&&(a.type=x),v&&(a.class&&typeof a.class=="string"?a.class=v.join(" ")+" "+a.class:a.class=v.join(" ")),b&&(a.keyValue=a.key=b.slice(1)),y[0]=`${o}${n||""}${f}`);const m=a.key;m&&m!==a.keyValue&&(a.keyValue=a.key)}else{if(b){const m=b.slice(1);a={key:m,keyValue:m}}(v||x)&&(a=a||{},x&&(a.type=x),v&&(a.class=v.join(" ")))}let O;if(d>S){O=[];for(let m=S;m<d;m++)O.push(w(y[m],c))}if(o===T||o===P||o===L){let m=o===T?"element":"component";o===L&&(m="decorator");const R={type:m,tagName:f,ns:z};return c.format(R,a,O)}else e(`Unsupported node prefix: ${o}`);return""}}const l=[];for(const t of y)if(typeof t=="string"||Array.isArray(t)){const r=w(t,c);if(r)if(Array.isArray(r))for(let o of r)l.push(o);else l.push(r)}else e(`Invalid LML node: ${JSON.stringify(t)}`);return l.length>0?l:"";function i(t){return c.format({type:"text",value:t})}function e(t){return c.error?(t.length>100&&(t=t.slice(0,100)+"..."),c.error(t)):console.error("[LML Scan Error] "+t),""}}const j={allowedElements:new Set(["address","article","aside","footer","header","h1","h2","h3","h4","h5","h6","hgroup","main","nav","section","blockquote","dd","div","dl","dt","figcaption","figure","hr","li","main","ol","p","pre","ul","a","abbr","b","bdi","bdo","br","cite","code","data","dfn","em","i","kbd","mark","q","rb","rp","rt","rtc","ruby","s","samp","small","span","strong","sub","sup","time","u","var","wbr","caption","col","colgroup","table","tbody","td","tfoot","th","thead","tr","img"]),forbiddenElementAttributes:new Set(["style","srcset"]),forbidEventHandlers:!0,urlAttributes:new Set(["href","src","cite","action","profile","longdesc","usemap","formaction","icon","poster","background","codebase","data","classid","manifest"]),allowedUrlPrefixes:["/","./","http://","https://","mailto://","tel://","data:image/"]};function _(s,c,p,u,y){u=u||(i=>console.error("[lm2JSX Error] "+i));const d=y||j;let l=null;if(d.allowedUrlPrefixes.length){const i=`^(${d.allowedUrlPrefixes.join("|")})`;l=new RegExp(i,"i")}return w(s,{format:(i,e,t)=>{const r=i.type;if(r==="text")return i.value;if(r==="element"||r==="component"){if(r==="element"){if(!d.allowedElements.has(i.tagName))return u(`Unauthorized element: ${i.tagName}`),"";if(e){for(const n of Object.getOwnPropertyNames(e))if(d.forbiddenElementAttributes.has(n))u(`Unauthorized element attribute: ${n}`),delete e[n];else if(d.forbidEventHandlers&&n.match(/^on/i))u(`Unauthorized event handler: ${n}`),delete e[n];else if(l&&d.urlAttributes.has(n)){const f=e[n];(typeof f!="string"||!f.match(l))&&(u(`Unauthorized URL: ${n}="${f}"`),delete e[n])}}}e&&e.class&&!e.className&&(e.className=e.class);let o=i.tagName;if(r==="component"){const n=p&&p(o,i.ns||"")||null;if(!n)return u("Invalid component: "+(i.ns?i.ns+":":"")+o),"";o=n}return t?c(o,e||null,...t):c(o,e||null)}return u("Unsupported node type: "+r),""},error:u})}function I(s,c){let p=h(s)==="fragment"?s:[s];const u=new Set;for(const l of c)l.node&&u.add(l.node);const y=new Map,d=u.size;if(d>0){let l=0;N(s,(i,e,t,r)=>(u.has(i)&&(l++,y.set(i,{node:e,parent:t,parentRef:r})),l<d),p,0)}for(const l of c){const i=l.action;let e=p,t=null,r="",o="";if(l.node){let n=y.get(l.node);if(n){if(t=n.parent,r=n.parentRef,e=n.node,o=l.path||"",o===""){const f=h(e);f!=="invalid"&&f!=="text"&&(i==="append"||i==="prepend")&&(o="children")}}else continue}if(o==="children"){if(e.length>1){let n=1;if(typeof e[n]!="string"&&!Array.isArray(e[n])&&n++,i==="delete")e.splice(n,e.length-n);else{const f=l.content,g=h(f);if(g!=="invalid"){const E=g==="fragment";i==="insertBefore"||i==="prepend"?E?e.splice.apply(e,[n,0,...f]):e.splice(n,0,f):i==="insertAfter"||i==="append"?E?e.push.apply(e,f):e.push(f):i==="replace"&&(E?e.splice.apply(e,[n,e.length-n,...f]):e.splice(n,e.length-n,f))}}}continue}else if(o){const n=o.split("/");if(Array.isArray(e)&&e.length>1)e=e[1];else continue;for(const f of n)t=e,r=f,e=t[r]}if(e)if(i==="delete")Array.isArray(t)?typeof r=="number"&&t.splice(r,1):t?t[r]=[]:p=[];else{const n=l.content,f=h(n);if(f!=="invalid"){const g=f==="fragment";if(i==="insertBefore"||i==="insertAfter"||i==="replace")if(Array.isArray(t)){if(typeof r=="number"){let E=r,b=0;i==="insertAfter"?E++:i==="replace"&&b++,g?t.splice.apply(t,[E,b,...n]):t.splice(E,b,n)}}else t?i==="insertBefore"?g?t[r]=[...n,e]:t[r]=[n,e]:i==="insertAfter"?g?t[r]=[e,...n]:t[r]=[e,n]:i==="replace"&&(t[r]=n):i==="replace"&&(p=g?n:[n]);else(i==="prepend"||i==="append")&&h(e)==="fragment"&&(i==="append"?g?e.push.apply(e,n):e.push(n):i==="prepend"&&(g?e.splice.apply(e,[0,0,...n]):e.splice(0,0,n)))}}}return p.length===1?p[0]:p}function N(s,c,p,u){if(!s)return!0;const y=h(s);if(Array.isArray(s)&&s.length){if(y==="component"||y==="element"){let l="";const e=s[0].indexOf(M);if(e>-1)l=s[0].slice(e+1);else{const t=s[1];t&&typeof t=="object"&&!Array.isArray(t)&&(l=t.key)}if(l&&c(l,s,p,u)===!1)return!1;for(let t=1;s.length>t;t++)if(!N(s[t],c,s,t))return!1}else for(let l=0;s.length>l;l++)if(!N(s[l],c,s,l))return!1}else if(typeof s=="object"){for(const d of Object.getOwnPropertyNames(s))if(!N(s[d],c,s,d))return!1}return!0}A.defaultSanitizationRules=j,A.lml2jsx=_,A.nodeType=h,A.processJSX=w,A.updateLML=I,Object.defineProperty(A,Symbol.toStringTag,{value:"Module"})});