UNPKG

teddy

Version:

🧸 Teddy is the most readable and easy to learn templating language there is!

2 lines • 23.8 kB
var e={175:()=>{},507:()=>{}},t={};function n(s){var r=t[s];if(void 0!==r)return r.exports;var o=t[s]={exports:{}};return e[s](o,o.exports,n),o.exports}var s=n(175),r=n(507);function o(e){const t=function(e){const t=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),n=document.createElement("body"),s=[n],r=[],o=/<\/?([a-zA-Z0-9]+)([^>]*)>|<!--([\s\S]*?)-->/g,i=/([a-zA-Z0-9-:._]+)(?:=(["'])(.*?)\2|([^>\s]+))?/g;let a,c=0;for(;null!==(a=o.exec(e));){if(!s[s.length-1])throw new Error("Error parsing your template. There may be a coding mistake in your HTML. Look for extra closing </tags> and other common mistakes.");const n=e.slice(c,a.index);if(n.trim()){const e=document.createTextNode(n);s[s.length-1].appendChild(e)}if(a[0].startsWith("\x3c!--")){const e=document.createComment(a[3]);s[s.length-1].appendChild(e)}else{const[e,n,o]=a,c=n.toLowerCase();if(e.startsWith("</"))if(t.has(c)){const e=document.createElement(n);s[s.length-1].appendChild(e)}else r.length>0&&r[r.length-1]===c&&(r.pop(),s.pop());else{const a=document.createElement(n);let l;const d=new Map;for(;null!==(l=i.exec(o));){const e=l[1],t=l[3]||l[4]||"";if(d.has(e)){let n,s=1;do{n=`${e}-teddyduplicate${s}`,s++}while(d.has(n));d.set(n,t)}else d.set(e,t)}for(const[e,t]of d)try{switch(c){case"img":case"video":case"audio":case"iframe":case"script":"src"===e?a.setAttribute("data-teddy-defer-attr-src",t):a.setAttribute(e,t||"");break;case"link":"href"===e?a.setAttribute("data-teddy-defer-attr-href",t):a.setAttribute(e,t||"");break;default:a.setAttribute(e,t||"")}}catch(e){console.warn("Error parsing an element attribute. You might have a typo in your HTML. A common cause is two spaces between element attributes.")}s[s.length-1].appendChild(a),t.has(c)||e.endsWith("/>")||(s.push(a),r.push(c))}}c=o.lastIndex}if(c<e.length){const t=e.slice(c);if(t.trim()){const e=document.createTextNode(t);s[s.length-1].appendChild(e)}}return n}(e),n=function(e){if("string"==typeof e){return t.querySelectorAll(e)}const n=e;return{children:function(){return n.childNodes},find:function(e){return n.querySelectorAll(e)},html:function(){return a(n)},toString:function(){return c(n)},attr:function(e,t){return n.setAttribute(e,t)},removeAttr:function(e){return n.removeAttribute(e)},replaceWith:function(e){if("object"==typeof e){let t="";for(const n of e)n.nodeType===window.Node.COMMENT_NODE?t+="\x3c!--"+n.textContent+"--\x3e":t+=n.outerHTML||n.textContent;e=t}const t=document.createElement("div");t.innerHTML=e,n.replaceWith(...t.childNodes)},remove:function(){return n.remove()}}};return n.html=function(){return a(t)},n.toString=function(){return c(t)},n}o.isCheerioPolyfill=!0;const i=Object.entries({"&amp;amp;":"&amp;","&amp;lt;":"&lt;","&amp;gt;":"&gt;","&amp;quot;":"&quot;","&amp;#39;":"&#39;","&amp;#x2F;":"&#x2F;"});function a(e){let t="";for(const n of e.childNodes)if(n.nodeType===window.Node.ELEMENT_NODE){let e=n.outerHTML;for(const[t,n]of i)e=e.replace(new RegExp(t,"g"),n);t+=e}else if(n.nodeType===window.Node.TEXT_NODE){let e=n.textContent;for(const[t,n]of i)e=e.replace(new RegExp(t,"g"),n);t+=e}else if(n.nodeType===window.Node.COMMENT_NODE){let e=n.textContent;for(const[t,n]of i)e=e.replace(new RegExp(t,"g"),n);t+=`\x3c!--${e}--\x3e`}return t}function c(e){let t="";e.nodeType===window.Node.ELEMENT_NODE?t=e.outerHTML:e.nodeType===window.Node.TEXT_NODE?t=e.textContent:e.nodeType===window.Node.COMMENT_NODE&&(t=`\x3c!--${e.textContent}--\x3e`);for(const[e,n]of i)t=t.replace(new RegExp(e,"g"),n);return t}const l=o.isCheerioPolyfill,d={};B();let u={};const f={},p={};function h(e){if("string"!=typeof e)return d.verbosity>1&&console.warn("teddy.loadTemplate attempted to load a template which is not a string."),"";const t=e;let n=!1;if(!u[e]&&-1===e.indexOf("<")&&s&&s.readFileSync){n=!0,".html"!==e.slice(-5)&&(e+=".html");try{e=s.readFileSync(e,"utf8")}catch(t){try{e=s.readFileSync(d.templateRoot+e,"utf8")}catch(t){try{e=s.readFileSync(d.templateRoot+"/"+e,"utf8")}catch(e){n=!1}}}}else u[e]?(e=u[e],n=!0):(".html"!==e.slice(-5)&&(e+=".html"),u[e]&&(e=u[e],n=!0),e=m(e));return n?(e=m(e),u[t]=e,e):".html"===e.slice(-5)?e.substring(0,e.length-5):e}function m(e){let t;do{let n;t=e;try{n=_(e,"{!","!}")}catch(t){return e}for(let t=0;t<n.length;t++)e=e.replace(`{!${n[t]}!}`,"");try{n=_(e,"\x3c!--!","--\x3e")}catch(t){return e}for(let t=0;t<n.length;t++)e=e.replace(`\x3c!--!${n[t]}--\x3e`,"");try{n=_(e,"\x3c!--#","--\x3e")}catch(t){return e}for(let t=0;t<n.length;t++)e=e.replace(`\x3c!--#${n[t]}--\x3e`,`<escape>${n[t]}</escape>`)}while(t!==e);return e}function y(e,t){let n;do{n=0;const s=e("cache:not([defer])");if(s.length>0)for(const r of s){l&&(r.attribs=j(r));const s=r.attribs.name;if(s.includes("{"))continue;const o=r.attribs.key||"none";if(o.includes("{"))continue;const i=f[s];if(i&&i.entries){const n=r.attribs.key?R(t,o):"none";if(i.entries[n]){const t=Date.now();if(!i.maxAge||i.maxage||i.entries[n].lastAccessed+(i.maxAge||i.maxage)>t){const s=i.entries[n].markup;i.entries[n].lastAccessed=t,e(r).replaceWith(s)}else delete f[s].entries[n],e(r).attr("defer","true")}else e(r).attr("defer","true")}else e(r).attr("defer","true");n++}}while(n);return e}function b(e,t){let n;do{n=0;let s=e("noteddy:not([id]), noparse:not([id])");if(s.length>0)for(const r of s){const s=t._noTeddyBlocks.push(e(r).html())-1;e(r).replaceWith(`<noteddy id="${s}"></noteddy>`),n++}if(s=e("pre:not([id]):not([parse])"),s.length>0)for(const r of s){const s=t._noTeddyBlocks.push(e(r).toString())-1;e(r).replaceWith(`<noteddy id="${s}" pre="true"></noteddy>`),n++}}while(n);return e}function g(e,t,n){let s,r=0;do{if(r++,r>d.maxPasses)throw new Error(`teddy could not finish rendering the template because the max number of passes over the template (${d.maxPasses}) was exceeded; there may be an infinite loop in your template logic.`);let i;if(s=0,i=e(n?"include":"include:not([teddydeferreddynamicinclude])"),i.length>0)for(const n of i){let r=!1,i=!1,a=n.parent||n.parentNode;for(;!r;){let e;if(e=a?a.nodeName?.toLowerCase()||a.name:"body","noparse"===e||"noteddy"===e){i=!0;break}"body"===e?r=!0:a=a.parent||a.parentNode}if(i)continue;l&&(n.attribs=j(n));const c=n.attribs.src;if(!c){d.verbosity>1&&console.warn("teddy encountered an include tag with no src attribute.");continue}if(c.startsWith("{")){e(n).attr("teddydeferreddynamicinclude","true");continue}h(c);let f=u[c]||"";"string"!=typeof u[c]&&"display"===d.includeNotFoundBehavior&&(f=`Template "${c}" not found!`,d.verbosity>1&&console.warn(`teddy encountered an include tag with a src set to a template that could not be found: ${c}`));const p=Object.assign({},t);for(const t of e(n).children()){if("arg"===(l?t.nodeName?.toLowerCase():t.name)){l&&(t.attribs=j(t));R(p,Object.keys(t.attribs)[0],e(t).html())}}const m=f.includes("</noteddy>"),y=f.includes("</noparse>"),g=f.includes("</pre>"),w=f.includes("</if>"),v=f.includes("</unless>"),C=f.includes(" true="),T=f.includes(" false="),S=f.includes("</loop>"),O=f.includes("</inline>"),L=f.includes("</escape>")||f.includes("\x3c!--#"),_=f.includes(" selected-value=")||f.includes(" checked-value=");let B;L&&(f=N(f)),(m||y||g)&&(B=o(f),B=b(B,p),f=B.html()),B=o(W(f,p)),(w||v)&&(B=x(B,p)),(C||T)&&(B=k(B,p)),S&&(B=$(B,p)),O&&(B=A(B,p)),_&&(B=E(B,p)),e(n).replaceWith(B.html()),s++}}while(s);return e}function x(e,t){let n;do{n=0;const s=e("if, unless");if(s.length>0)for(const r of s){let s=!1,o=!1,i=r.parent||r.parentNode;for(;!s;){let e;if(e=i?i.nodeName?.toLowerCase()||i.name:"body","loop"===e||"noparse"===e||"noteddy"===e){o=!0;break}"body"===e?s=!0:i=i.parent||i.parentNode}if(o)continue;let a=[];l&&(r.attribs=j(r));for(let e in r.attribs){e.includes("-teddyduplicate")&&(e=e.split("-teddyduplicate")[0]);let n=r.attribs[e];n?(n.startsWith("{")&&(n=W(n,t)),a.push(`${e}=${n}`)):a.push(e)}let c=!0;"unless"===(l?r.nodeName?.toLowerCase():r.name)&&(c=!1);const d=w(a,t);if(c&&d||!c&&!d){let t=r.nextSibling;const s=[];for(;t;){switch(l?t.nodeName?.toLowerCase():t.name){case"elseif":case"elseunless":case"else":s.push(t),t=t.nextSibling;break;case"if":case"unless":t=!1;break;default:t=t.nextSibling}}for(const t of s)e(t).replaceWith("");e(r).replaceWith(r.childNodes||r.children),n++}else{let s=r.nextSibling;for(;s;){switch(l?s.nodeName?.toLowerCase():s.name){case"elseif":a=[],l&&(s.attribs=j(s));for(const e in s.attribs){const t=s.attribs[e];t?a.push(`${e}=${t}`):a.push(e)}if(w(a,t)){const t=s;e(t).replaceWith(t.childNodes||t.children),s=r.nextSibling;const o=[];for(;s;){switch(l?s.nodeName?.toLowerCase():s.name){case"elseif":case"elseunless":case"else":o.push(s),s=s.nextSibling;break;case"if":case"unless":s=!1;break;default:s=s.nextSibling}}for(const t of o)e(t).replaceWith("");s=!1,n++}else{const t=s;s=s.nextSibling,e(t).replaceWith("")}break;case"elseunless":a=[],l&&(s.attribs=j(s));for(const e in s.attribs){const t=s.attribs[e];t?a.push(`${e}=${t}`):a.push(e)}if(w(a,t)){const t=s;s=s.nextSibling,e(t).replaceWith("")}else{const t=s;e(t).replaceWith(t.childNodes||t.children),s=r.nextSibling;const o=[];for(;s;){switch(l?s.nodeName?.toLowerCase():s.name){case"elseif":case"elseunless":case"else":o.push(s),s=s.nextSibling;break;case"if":case"unless":s=!1;break;default:s=s.nextSibling}}for(const t of o)e(t).replaceWith("");s=!1,n++}break;case"else":e(s).replaceWith(s.childNodes||s.children),s=!1,n++;break;case"if":case"unless":s=!1;break;default:s=s.nextSibling}}e(r).replaceWith("")}}}while(n);return e}function w(e,t){const n=e.length;for(let s=0;s<n;s++){const n=e[s];if("boolean"!=typeof n){if(n.startsWith("=")||n.endsWith("="))return d.verbosity>1&&console.warn('teddy encountered a conditional statement with "=" at the beginning or end of a condition.'),!1;if(n.includes(":")&&!n.startsWith("not:"))return d.verbosity>1&&console.warn('teddy encountered a conditional statement with a "not:" that isn\'t at the beginning of a condition.'),!1;"and"===n?e[s-1]&&v(e[s+1],t)?(e[s-1]=!0,e[s]=!0,e[s+1]=!0):(e[s-1]=!1,e[s]=!1,e[s+1]=!1):"or"===n?e[s-1]||v(e[s+1],t)?e.fill(!0,0,s+2):(e[s-1]=!1,e[s]=!1,e[s+1]=!1):"xor"===n?!!e[s-1]==!!v(e[s+1],t)?(e[s-1]=!1,e[s]=!1,e[s+1]=!1):(e[s-1]=!0,e[s]=!0,e[s+1]=!0):e[s]=v(n,t)}}return e.every((e=>!0===e))||!1}function v(e,t){let n;if("string"==typeof e&&e.includes("=")){n=!!e.startsWith("not:"),n&&(e=e.slice(4));const s=e.split("="),r=s[0];delete s[0];const o=s.join("");return R(t,r)==o?!n:n}{n="string"==typeof e&&!!e.startsWith("not:"),n&&(e=e.slice(4));const s=R(t,e);return s?"object"==typeof s&&0===Object.keys(s).length?n:!n:n}}function k(e,t){let n;do{n=0;const s=e("[true], [false]");if(s.length>0)for(const r of s){let s=!1;l&&(r.attribs=j(r));for(const e in r.attribs){if(r.attribs[e].startsWith("{")){s=!0;break}}if(s){e(r).attr("teddydeferredonelineconditional","true");continue}let o,i,a=!1,c=!1,d=r.parent||r.parentNode;for(;!a;){let e;if(e=d?d.nodeName?.toLowerCase()||d.name:"body","loop"===e||"noparse"===e||"noteddy"===e){c=!0;break}"body"===e?a=!0:d=d.parent||d.parentNode}if(c)continue;l&&(r.attribs=j(r));const u=[];for(const n in r.attribs){let s=n,a=r.attribs[s];if(s.includes("-teddyduplicate")&&(s=s.split("-teddyduplicate")[0]),a?.startsWith("{")&&(a=W(a,t)),s.startsWith("if-")){const t=s.split("if-");a?u.push(`${t[1]}=${a}`):u.push(t[1]),e(r).removeAttr(n)}else"true"===s?(o=a.replaceAll("&quot;",'"'),e(r).removeAttr(n)):"false"===s?(i=a.replaceAll("&quot;",'"'),e(r).removeAttr(n)):"and"!==s&&"or"!==s&&"xor"!==s||(u.push(s),e(r).removeAttr(n))}if(w(u,t)){if(o){const t=o.split("=");e(r).attr(t[0],t[1]?t[1].replace(/["']/g,""):"")}n++}else if(i){if(i){const t=i.split("=");e(r).attr(t[0],t[1]?t[1].replace(/["']/g,""):"")}n++}}}while(n);return e}function $(e,t){let n;do{n=0;const s=e("loop");if(s.length>0)for(const r of s){let s,i,a;l&&(r.attribs=j(r));for(const e in r.attribs)if("through"===e){let n=r.attribs[e];n.startsWith("{")&&(n=W(n,t)),s=R(t,n)}else"key"===e?i=r.attribs[e]:"val"===e&&(a=r.attribs[e]);if(!s){d.verbosity>1&&console.warn("teddy encountered a loop without a through attribute."),e(r).replaceWith("");continue}if(!i&&!a){d.verbosity>1&&console.warn("teddy encountered a loop without a key or a val attribute."),e(r).replaceWith("");continue}let c="",u=e(r).html();s instanceof Set&&(s=[...s]);for(const e in s){const n=s[e],r=Object.assign({},t);R(r,i,e),R(r,a,n);const l=u.includes("</noteddy>"),d=u.includes("</noparse>"),f=u.includes("</pre>");if((u.includes("</escape>")||u.includes("\x3c!--#"))&&(u=N(u)),l||d||f){let e=o(u);e=b(e,r),u=e.html()}const p=W(u,r)||"",h=p.includes("</noteddy>"),m=p.includes("</noparse>"),y=p.includes("</if>"),g=p.includes("</unless>"),w=p.includes(" true="),v=p.includes(" false="),C=p.includes("</loop>"),T=p.includes("</inline>"),S=p.includes(" selected-value=")||p.includes(" checked-value=");let O=o(p||"");(h||m)&&(O=b(O,r)),(y||g)&&(O=x(O,r)),(w||v)&&(O=k(O,r)),C&&(O=$(O,r)),T&&(O=A(O,r)),S&&(O=E(O,r)),c+=O.html()}const f=o(c||"");e(r).replaceWith(f.html()),n++}}while(n);return e}function A(e,t){let n;do{n=0;const s=e("inline");if(s.length>0)for(const r of s){let s,o;l&&(r.attribs=j(r));for(const e in r.attribs)"css"===e?s=R(t,r.attribs[e]):"js"===e&&(o=R(t,r.attribs[e]));if(!s&&!o){d.verbosity>1&&console.warn("teddy encountered an <inline> element without a css or js attribute."),e(r).replaceWith("");continue}let i="";i=s?`<style>${s}</style>`:`<script>${o}<\/script>`,e(r).replaceWith(i),n++}}while(n);return e}function N(e){return e.replace(/<escape>(.*?)<\/escape>/gs,((e,t)=>L(t.trim())))}function E(e,t){let n;do{n=0;const s=e("select[selected-value], [checked-value]");if(s.length>0)for(const r of s){l&&(r.attribs=j(r));for(let n in r.attribs){const s=n;if(n.includes("-teddyduplicate")&&(n=n.split("-teddyduplicate")[0]),"selected-value"===n){const n=W(r.attribs[s],t)||r.attribs[s],o=e(r).find("option[value]");for(const t of o)l&&(t.attribs=j(t)),t.attribs.value===n&&e(t).attr("selected","selected");e(r).removeAttr(s)}else if("checked-value"===n){const n=W(r.attribs[s],t)||r.attribs[s],o=e(r).find('input[type="checkbox"][value], input[type="radio"][value]');for(const t of o)l&&(t.attribs=j(t)),t.attribs.value===n&&e(t).attr("checked","checked");e(r).removeAttr(s)}}n++}}while(n);return e}function W(e,t){let n;try{n=_(e,"{","}")}catch(t){return e}const s=n.length;for(let r=0;r<s;r++){let s=n[r];if(""===s)continue;if(!/^(\d+|[a-zA-Z_$][a-zA-Z0-9_$|{}.-]*(\.[a-zA-Z_$][a-zA-Z0-9_$|{}.-]*)*)$/.test(s)){d.verbosity>2&&console.warn(`teddy.parseVars encountered a {variable} that could not be parsed: {${s}}`);continue}if(s.includes("{")){const n=s;s=W(s,t);try{e=(e=e.replace(new RegExp(`\${${n}}`,"i"),(()=>`\${${s}}`))).replace(new RegExp(`{${n}}`,"i"),(()=>`{${s}}`))}catch(e){d.verbosity>2&&console.warn(`teddy.parseVars encountered a {variable} that could not be parsed: {${n}}`)}}const o=s.slice(-6);if(o.includes("|p")){const n=s;s=s.substring(0,s.length-2*(o.split("|").length-1));let r=R(t,s);if(r||o.includes("|d")||"hide"!==d.emptyVarBehavior&&!o.includes("|h")||(r=""),"string"==typeof r&&r.startsWith("{")&&r.includes("|d")&&(r=r.replace("|d","")),r||""===r){const s=t._noTeddyBlocks.push(r)-1;try{try{e=(e=e.replace(new RegExp(`\${${n}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),`<noteddy id="${s}"></noteddy>`)).replace(new RegExp(`{${n}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),`<noteddy id="${s}"></noteddy>`)}catch(e){d.verbosity>2&&console.warn(`teddy.parseVars encountered a {variable} that could not be parsed: {${n}}`)}}catch(t){return e}}}else if(o.includes("|s")){const n=s;s=s.substring(0,s.length-2*(o.split("|").length-1));let r=R(t,s),i=!1;r||o.includes("|d")||"hide"!==d.emptyVarBehavior&&!o.includes("|h")?r||""===r||(i=!0,r=`{${n}}`):r="","string"==typeof r&&r.startsWith("{")&&r.includes("|d")&&(r=r.replace("|d",""));try{i||(e=e.replace(new RegExp(`\${${n}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),(()=>r))),e=e.replace(new RegExp(`{${n}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),(()=>r))}catch(t){return e}}else{let n=R(t,s),r=!1;n||o.includes("|d")||"hide"!==d.emptyVarBehavior&&!o.includes("|h")?n||""===n?n=L(n):0===n?n="0":(r=!0,n=`{${s}}`):n="","string"==typeof n&&n.startsWith("{")&&n.includes("|d")&&(n=n.replace("|d",""));try{r||(e=e.replace(new RegExp(`\${${s}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),(()=>n))),e=e.replace(new RegExp(`{${s}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),(()=>n))}catch(t){return e}}}return e}function C(e,t){let n;do{n=0;const s=e("cache[defer]");if(s.length>0)for(const r of s){l&&(r.attribs=j(r));const s=r.attribs.name,o=r.attribs.key||"none",i=parseInt(r.attribs.maxAge||r.attribs.maxage)||0,a=parseInt(r.attribs.maxCaches||r.attribs.maxcaches)||1e3,c=Date.now(),d=e(r).html();if(f[s]||(f[s]={key:o,maxAge:i,maxCaches:a,entries:{}}),f[s].entries[r.attribs.key?R(t,o):"none"]={lastAccessed:c,created:c,markup:d},Object.keys(f[s].entries).length>a){const e=Object.keys(f[s].entries).reduce(((e,t)=>f[s].entries[e].lastAccessed<f[s].entries[t].lastAccessed?e:t));delete f[s].entries[e]}e(r).replaceWith(d),n++}}while(n);return e}const T={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&#34;","'":"&#39;"},S=Object.keys(T),O=S.length;function L(e){let t,n,s=!1,r="";if("object"==typeof e)return!!e&&(Array.isArray(e)?0!==e.length&&"[Array]":"[Object]");if(void 0===e)return!1;if("boolean"==typeof e||"number"==typeof e)return e;for(t=0;t<e.length;t++){for(s=!1,n=0;n<O;n++)if(e[t]===S[n]){r+=T[S[n]],s=!0;break}s||(r+=e[t])}return r}function _(e,t,n){const s=[],r=[],o=t.length,i=n.length;for(let a=0;a<e.length;a++)if(e.substring(a,a+o)===t)s.push(a+o),a+=o-1;else if(e.substring(a,a+i)===n){const t=s.pop();0===s.length&&r.push(e.substring(t,a)),a+=i-1}const a=[],c=/{!([^{}]*)!}/g;let l;for(const e of r){for(;null!==(l=c.exec(e));)a.push(l[1]);a.push(e)}return a}function R(e,t,n){return!!e&&(t&&"boolean"!=typeof t&&"number"!=typeof t?"string"==typeof t?R(e,t.split("."),n):1===t.length&&void 0!==n?(e[t[0]]=n,e[t[0]]):0===t.length?e:1===t.length?!!e&&s(e,t[0]):R(s(e,t[0]),t.slice(1),n):t);function s(e,t){if("length"===t)return e.length;const n=t.toLowerCase();return Object.keys(e).reduce(((t,n)=>(t[n.toLowerCase()]=e[n],t)),{})[n]}}function j(e){const t=e.attributes,n={};for(let e=0;e<t.length;e++){const s=t[e];n[s.name]=s.value}return n}function B(){d.verbosity=1,d.templateRoot="./",d.maxPasses=1e3,d.emptyVarBehavior="display",d.includeNotFoundBehavior="display"}function M(e,t,n){if("string"!=typeof e)return d.verbosity>1&&console.warn("teddy.render attempted to render a template which is not a string."),"function"==typeof n?n(null,""):"";let s,i;"object"!=typeof t&&(d.verbosity>1&&console.warn("teddy.render was passed an invalid model."),t={}),t._noTeddyBlocks=[],t.settings&&t.settings.views&&r&&(d.templateRoot=r.resolve(t.settings.views)),e.slice(d.templateRoot.length)===d.templateRoot&&(e=e.replace(d.templateRoot,""));const a=p[e];let c=null,u=null;if(a){const e=a.none;if(e)if(e.created){if(!e.maxAge&&e.maxage)return"function"==typeof n?n(null,e.markup):e.markup;if(!(e.created+(e.maxAge||e.maxage)<Date.now()))return"function"==typeof n?n(null,e.markup):e.markup;c="none"}else c="none";else for(const e in a)if(u=R(t,e),u){const t=a[e];for(const s in t.entries)if(s===u){const r=t.entries[s];if(t.maxAge||t.maxage){if(r.created+(t.maxAge||t.maxage)<Date.now()){c=e;break}return"function"==typeof n?n(null,r.markup):r.markup}return"function"==typeof n?n(null,r.markup):r.markup}c=e;break}}i=h(e),l||(i=i.replace(/<([a-zA-Z][a-zA-Z0-9-]*)([^>]*)>/g,((e,t,n)=>{const s=new Map;let r=1;return`<${t}${n.replace(/([a-zA-Z0-9-:._]+)(?:=(["'])(.*?)\2|([^>\s]+))?/g,((e,t,n,o)=>{if(s.has(t)){const n=`${t}-teddyduplicate${r++}`;return e.replace(t,n)}return s.set(t,!0),e}))}>`})));let f;(i.includes("</escape>")||i.includes("\x3c!--#"))&&(i=N(i)),s=o(i||"");let w=0,v=!1;do{if(w++,w>d.maxPasses){d.verbosity>0&&console.error(`teddy could not finish rendering the template because the max number of passes over the template (${d.maxPasses}) was exceeded; there may be an infinite loop in your template logic.`);break}const e=i.includes("</cache>"),n=i.includes("</noteddy>"),r=i.includes("</noparse>"),a=i.includes("</pre>"),c=i.includes("</if>"),l=i.includes("</unless>"),u=i.includes(" true="),p=i.includes(" false="),h=i.includes("</include>"),N=i.includes("</loop>"),T=i.includes("</inline>"),S=i.includes(" selected-value=")||i.includes(" checked-value=");f=i||"",w>1&&(s=o(i||""),v&&(s=g(s,t,!0))),e&&(s=y(s,t)),(n||r||a)&&(s=b(s,t)),(c||l)&&(s=x(s,t)),(u||p)&&(s=k(s,t)),h&&(s=g(s,t)),N&&(s=$(s,t)),T&&(s=A(s,t)),S&&(s=E(s,t));const O=i.includes("</cache>");i=s.html(),i=W(i,t),v&&(i=m(i),v=!1),i.includes('teddydeferreddynamicinclude="true"')&&(f="",v=!0),f===i&&O&&(s=o(i||""),s=C(s,t),i=s.html())}while(f!==i);(i.includes('teddydeferredonelineconditional="true"')||i.includes("</include>")||i.includes("</arg>")||i.includes("</if>")||i.includes("</unless>")||i.includes("</elseif>")||i.includes("</elseunless>")||i.includes("</else>")||i.includes("</loop>")||i.includes("</cache>")||i.includes("</pre>"))&&(s=o(i||""),s=function(e){let t;{t=0;const n=e("[teddydeferredonelineconditional], pre[parse], include, arg, if, unless, elseif, elseunless, else, loop, cache");if(n.length>0)for(const t of n){const n=l?t.nodeName?.toLowerCase():t.name;"include"!==n&&"arg"!==n&&"if"!==n&&"unless"!==n&&"elseif"!==n&&"elseunless"!==n&&"else"!==n&&"loop"!==n&&"cache"!==n||e(t).remove(),l&&(t.attribs=j(t));for(const n in t.attribs)("true"===n||"false"===n||"parse"===n||"teddydeferredonelineconditional"===n||n.startsWith("if-"))&&e(t).removeAttr(n)}}return e}(s),i=s.html());for(const e in t._noTeddyBlocks)i=i.replace(`<noteddy id="${e}"></noteddy>`,(()=>t._noTeddyBlocks[e])),i=i.replace(`<noteddy id="${e}" pre="true"></noteddy>`,(()=>t._noTeddyBlocks[e]));if(l&&(i=i.replace(/&amp;(#\d+;|#x[0-9A-Fa-f]+;|[A-Za-z]+;)/g,"&$1"),i=i.replaceAll("data-teddy-defer-attr-src","src").replaceAll("data-teddy-defer-attr-href","href")),"none"===c)p[e].none.markup=i,p[e].none.created=Date.now();else if(c&&(p[e][c].entries[u]||(p[e][c].entries[u]={}),p[e][c].entries[u].markup=i,p[e][c].entries[u].created=Date.now(),Object.keys(p[e][c].entries).length>p[e][c].maxCaches)){const t=Object.keys(p[e][c].entries).reduce(((t,n)=>p[e][c].entries[t].created<p[e][c].entries[n].created?t:n));delete p[e][c].entries[t]}return"function"==typeof n?n(null,i):i}const D={params:d,caches:f,templateCaches:p,compile:function(e){return function(t){return M(e,t)}},setDefaultParams:B,setVerbosity:function(e){switch(e){case"none":case 0:e=0;break;case"verbose":case 2:e=2;break;case"debug":case"DEBUG":case 3:e=3;break;default:e=1}d.verbosity=e},setTemplateRoot:function(e){d.templateRoot=String(e)},setMaxPasses:function(e){d.maxPasses=Number(e)},setEmptyVarBehavior:function(e){d.emptyVarBehavior="hide"===e?"hide":"display"},setIncludeNotFoundBehavior:function(e){d.includeNotFoundBehavior="hide"===e?"hide":"display"},getTemplates:function(){return u},setTemplate:function(e,t){u[e]=t},clearTemplates:function(){u={}},setCache:function(e){p[e.template]||(p[e.template]={}),e.key?p[e.template][e.key]={maxAge:e.maxAge||e.maxage,maxCaches:e.maxCaches||e.maxcaches||1e3,entries:{}}:p[e.template].none={maxAge:e.maxAge||e.maxage,markup:null,created:null}},clearCache:function(e,t){if("string"==typeof e)t?delete f[e].entries[t]:delete f[e];else if("object"==typeof e){const t=e;t.key?delete p[t.template][t.key]:delete p[t.template]}else d.verbosity>0&&console.error("teddy: invalid params passed to clearCache.")},render:M,__express:M};export{D as default}; //# sourceMappingURL=teddy.min.mjs.map