pragma-views2
Version:
1 lines • 1.75 kB
JavaScript
export function createInflatorFunction(e,t,n){const i=[],r=[];let o=[];const l=[];parseElement(e,"element",(e=>i.push(e)),(e=>o.push(e)),(e=>l.push(e)),(e=>r.push(e)));let s=["return Promise.resolve().then(() => {"];for(let e of i){const t=`item.${e.binding}`,n=t.includes("?")?t:getBindingPathExpression(t),i=`${e.path}.innerText = ${n} ? ${t} : "";`;s.push(i)}for(let e of l){const t=`${e.path}.setAttribute("${e.name}", item.${e.binding});`;s.push(t)}null!=t&&(s=s.concat(t)),null!=n&&(o=o.concat(n));for(let e of r)s.push(`if((item.${e.condition}) == false) {\n ${e.path}.setAttribute("aria-hidden", true)\n }`),o.push(`${e.path}.removeAttribute("aria-hidden");`);s.push("});");return{inflateFn:new Function("element","item",s.join("")),defaultFn:new Function("element",`return Promise.resolve().then(() => {${o.join("")}})`)}}function parseElement(e,t,n,i,r,o){if(-1!==["use"].indexOf(e.nodeName.toLowerCase()))return;if(null!=e.attributes){const n=e.getAttribute("condition");if(null!=n)o({element:e,path:t,condition:n});else for(const n of e.attributes)n.value.startsWith("#")&&(r({element:e,path:t,binding:n.value.slice(1),name:n.name}),i(`${t}.setAttribute("${n.name}", '${n.value}');`))}0==e.children.length&&(e.innerText||"").startsWith("#")&&(n({element:e,path:t,binding:e.innerText.slice(1)}),i(`${t}.innerHTML = '${e.innerText}';`));const l=Array.from(e.children);for(let e=0;e<l.length;e++)parseElement(l[e],`${t}.children[${e}]`,n,i,r,o)}export function getBindingPathExpression(e){if(null==e)return null;const t=e.split("."),n=[];let i="";for(let e=0;e<t.length;e++){n.push(t[e]);const r=n.join(".");i+=`${e>0?` && ${r}`:r} != null`}return i}const inflationUtils={createInflatorFunction};export{inflationUtils};