UNPKG

teddy

Version:

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

2 lines • 24.2 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("teddy",[],t):"object"==typeof exports?exports.teddy=t():e.teddy=t()}(this,(()=>(()=>{var e={175:()=>{},507:()=>{}},t={};function n(r){var s=t[r];if(void 0!==s)return s.exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var r={};return(()=>{"use strict";n.d(r,{default:()=>M});var e=n(175),t=n(507);function s(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"),r=[n],s=[],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(!r[r.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);r[r.length-1].appendChild(e)}if(a[0].startsWith("\x3c!--")){const e=document.createComment(a[3]);r[r.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);r[r.length-1].appendChild(e)}else s.length>0&&s[s.length-1]===c&&(s.pop(),r.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,r=1;do{n=`${e}-teddyduplicate${r}`,r++}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.")}r[r.length-1].appendChild(a),t.has(c)||e.endsWith("/>")||(r.push(a),s.push(c))}}c=o.lastIndex}if(c<e.length){const t=e.slice(c);if(t.trim()){const e=document.createTextNode(t);r[r.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 i(n)},toString:function(){return a(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 i(t)},n.toString=function(){return a(t)},n}s.isCheerioPolyfill=!0;const o=Object.entries({"&amp;amp;":"&amp;","&amp;lt;":"&lt;","&amp;gt;":"&gt;","&amp;quot;":"&quot;","&amp;#39;":"&#39;","&amp;#x2F;":"&#x2F;"});function i(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 o)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 o)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 o)e=e.replace(new RegExp(t,"g"),n);t+=`\x3c!--${e}--\x3e`}return t}function a(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 o)t=t.replace(new RegExp(e,"g"),n);return t}const c=s.isCheerioPolyfill,l={};R();let d={};const u={},f={};function p(t){if("string"!=typeof t)return l.verbosity>1&&console.warn("teddy.loadTemplate attempted to load a template which is not a string."),"";const n=t;let r=!1;if(!d[t]&&-1===t.indexOf("<")&&e&&e.readFileSync){r=!0,".html"!==t.slice(-5)&&(t+=".html");try{t=e.readFileSync(t,"utf8")}catch(n){try{t=e.readFileSync(l.templateRoot+t,"utf8")}catch(n){try{t=e.readFileSync(l.templateRoot+"/"+t,"utf8")}catch(e){r=!1}}}}else d[t]?(t=d[t],r=!0):(".html"!==t.slice(-5)&&(t+=".html"),d[t]&&(t=d[t],r=!0),t=h(t));return r?(t=h(t),d[n]=t,t):".html"===t.slice(-5)?t.substring(0,t.length-5):t}function h(e){let t;do{let n;t=e;try{n=j(e,"{!","!}")}catch(t){return e}for(let t=0;t<n.length;t++)e=e.replace(`{!${n[t]}!}`,"");try{n=j(e,"\x3c!--!","--\x3e")}catch(t){return e}for(let t=0;t<n.length;t++)e=e.replace(`\x3c!--!${n[t]}--\x3e`,"");try{n=j(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 m(e,t){let n;do{n=0;const r=e("cache:not([defer])");if(r.length>0)for(const s of r){c&&(s.attribs=_(s));const r=s.attribs.name;if(r.includes("{"))continue;const o=s.attribs.key||"none";if(o.includes("{"))continue;const i=u[r];if(i&&i.entries){const n=s.attribs.key?L(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 r=i.entries[n].markup;i.entries[n].lastAccessed=t,e(s).replaceWith(r)}else delete u[r].entries[n],e(s).attr("defer","true")}else e(s).attr("defer","true")}else e(s).attr("defer","true");n++}}while(n);return e}function y(e,t){let n;do{n=0;let r=e("noteddy:not([id]), noparse:not([id])");if(r.length>0)for(const s of r){const r=t._noTeddyBlocks.push(e(s).html())-1;e(s).replaceWith(`<noteddy id="${r}"></noteddy>`),n++}if(r=e("pre:not([id]):not([parse])"),r.length>0)for(const s of r){const r=t._noTeddyBlocks.push(e(s).toString())-1;e(s).replaceWith(`<noteddy id="${r}" pre="true"></noteddy>`),n++}}while(n);return e}function b(e,t,n){let r,o=0;do{if(o++,o>l.maxPasses)throw new Error(`teddy could not finish rendering the template because the max number of passes over the template (${l.maxPasses}) was exceeded; there may be an infinite loop in your template logic.`);let i;if(r=0,i=e(n?"include":"include:not([teddydeferreddynamicinclude])"),i.length>0)for(const n of i){let o=!1,i=!1,a=n.parent||n.parentNode;for(;!o;){let e;if(e=a?a.nodeName?.toLowerCase()||a.name:"body","noparse"===e||"noteddy"===e){i=!0;break}"body"===e?o=!0:a=a.parent||a.parentNode}if(i)continue;c&&(n.attribs=_(n));const u=n.attribs.src;if(!u){l.verbosity>1&&console.warn("teddy encountered an include tag with no src attribute.");continue}if(u.startsWith("{")){e(n).attr("teddydeferreddynamicinclude","true");continue}p(u);let f=d[u]||"";"string"!=typeof d[u]&&"display"===l.includeNotFoundBehavior&&(f=`Template "${u}" not found!`,l.verbosity>1&&console.warn(`teddy encountered an include tag with a src set to a template that could not be found: ${u}`));const h=Object.assign({},t);for(const t of e(n).children()){if("arg"===(c?t.nodeName?.toLowerCase():t.name)){c&&(t.attribs=_(t));L(h,Object.keys(t.attribs)[0],e(t).html())}}const m=f.includes("</noteddy>"),b=f.includes("</noparse>"),x=f.includes("</pre>"),w=f.includes("</if>"),W=f.includes("</unless>"),C=f.includes(" true="),T=f.includes(" false="),O=f.includes("</loop>"),S=f.includes("</inline>"),j=f.includes("</escape>")||f.includes("\x3c!--#"),R=f.includes(" selected-value=")||f.includes(" checked-value=");let B;j&&(f=A(f)),(m||b||x)&&(B=s(f),B=y(B,h),f=B.html()),B=s(E(f,h)),(w||W)&&(B=g(B,h)),(C||T)&&(B=v(B,h)),O&&(B=k(B,h)),S&&(B=$(B,h)),R&&(B=N(B,h)),e(n).replaceWith(B.html()),r++}}while(r);return e}function g(e,t){let n;do{n=0;const r=e("if, unless");if(r.length>0)for(const s of r){let r=!1,o=!1,i=s.parent||s.parentNode;for(;!r;){let e;if(e=i?i.nodeName?.toLowerCase()||i.name:"body","loop"===e||"noparse"===e||"noteddy"===e){o=!0;break}"body"===e?r=!0:i=i.parent||i.parentNode}if(o)continue;let a=[];c&&(s.attribs=_(s));for(let e in s.attribs){e.includes("-teddyduplicate")&&(e=e.split("-teddyduplicate")[0]);let n=s.attribs[e];n?(n.startsWith("{")&&(n=E(n,t)),a.push(`${e}=${n}`)):a.push(e)}let l=!0;"unless"===(c?s.nodeName?.toLowerCase():s.name)&&(l=!1);const d=x(a,t);if(l&&d||!l&&!d){let t=s.nextSibling;const r=[];for(;t;){switch(c?t.nodeName?.toLowerCase():t.name){case"elseif":case"elseunless":case"else":r.push(t),t=t.nextSibling;break;case"if":case"unless":t=!1;break;default:t=t.nextSibling}}for(const t of r)e(t).replaceWith("");e(s).replaceWith(s.childNodes||s.children),n++}else{let r=s.nextSibling;for(;r;){switch(c?r.nodeName?.toLowerCase():r.name){case"elseif":a=[],c&&(r.attribs=_(r));for(const e in r.attribs){const t=r.attribs[e];t?a.push(`${e}=${t}`):a.push(e)}if(x(a,t)){const t=r;e(t).replaceWith(t.childNodes||t.children),r=s.nextSibling;const o=[];for(;r;){switch(c?r.nodeName?.toLowerCase():r.name){case"elseif":case"elseunless":case"else":o.push(r),r=r.nextSibling;break;case"if":case"unless":r=!1;break;default:r=r.nextSibling}}for(const t of o)e(t).replaceWith("");r=!1,n++}else{const t=r;r=r.nextSibling,e(t).replaceWith("")}break;case"elseunless":a=[],c&&(r.attribs=_(r));for(const e in r.attribs){const t=r.attribs[e];t?a.push(`${e}=${t}`):a.push(e)}if(x(a,t)){const t=r;r=r.nextSibling,e(t).replaceWith("")}else{const t=r;e(t).replaceWith(t.childNodes||t.children),r=s.nextSibling;const o=[];for(;r;){switch(c?r.nodeName?.toLowerCase():r.name){case"elseif":case"elseunless":case"else":o.push(r),r=r.nextSibling;break;case"if":case"unless":r=!1;break;default:r=r.nextSibling}}for(const t of o)e(t).replaceWith("");r=!1,n++}break;case"else":e(r).replaceWith(r.childNodes||r.children),r=!1,n++;break;case"if":case"unless":r=!1;break;default:r=r.nextSibling}}e(s).replaceWith("")}}}while(n);return e}function x(e,t){const n=e.length;for(let r=0;r<n;r++){const n=e[r];if("boolean"!=typeof n){if(n.startsWith("=")||n.endsWith("="))return l.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 l.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[r-1]&&w(e[r+1],t)?(e[r-1]=!0,e[r]=!0,e[r+1]=!0):(e[r-1]=!1,e[r]=!1,e[r+1]=!1):"or"===n?e[r-1]||w(e[r+1],t)?e.fill(!0,0,r+2):(e[r-1]=!1,e[r]=!1,e[r+1]=!1):"xor"===n?!!e[r-1]==!!w(e[r+1],t)?(e[r-1]=!1,e[r]=!1,e[r+1]=!1):(e[r-1]=!0,e[r]=!0,e[r+1]=!0):e[r]=w(n,t)}}return e.every((e=>!0===e))||!1}function w(e,t){let n;if("string"==typeof e&&e.includes("=")){n=!!e.startsWith("not:"),n&&(e=e.slice(4));const r=e.split("="),s=r[0];delete r[0];const o=r.join("");return L(t,s)==o?!n:n}{n="string"==typeof e&&!!e.startsWith("not:"),n&&(e=e.slice(4));const r=L(t,e);return r?"object"==typeof r&&0===Object.keys(r).length?n:!n:n}}function v(e,t){let n;do{n=0;const r=e("[true], [false]");if(r.length>0)for(const s of r){let r=!1;c&&(s.attribs=_(s));for(const e in s.attribs){if(s.attribs[e].startsWith("{")){r=!0;break}}if(r){e(s).attr("teddydeferredonelineconditional","true");continue}let o,i,a=!1,l=!1,d=s.parent||s.parentNode;for(;!a;){let e;if(e=d?d.nodeName?.toLowerCase()||d.name:"body","loop"===e||"noparse"===e||"noteddy"===e){l=!0;break}"body"===e?a=!0:d=d.parent||d.parentNode}if(l)continue;c&&(s.attribs=_(s));const u=[];for(const n in s.attribs){let r=n,a=s.attribs[r];if(r.includes("-teddyduplicate")&&(r=r.split("-teddyduplicate")[0]),a?.startsWith("{")&&(a=E(a,t)),r.startsWith("if-")){const t=r.split("if-");a?u.push(`${t[1]}=${a}`):u.push(t[1]),e(s).removeAttr(n)}else"true"===r?(o=a.replaceAll("&quot;",'"'),e(s).removeAttr(n)):"false"===r?(i=a.replaceAll("&quot;",'"'),e(s).removeAttr(n)):"and"!==r&&"or"!==r&&"xor"!==r||(u.push(r),e(s).removeAttr(n))}if(x(u,t)){if(o){const t=o.split("=");e(s).attr(t[0],t[1]?t[1].replace(/["']/g,""):"")}n++}else if(i){if(i){const t=i.split("=");e(s).attr(t[0],t[1]?t[1].replace(/["']/g,""):"")}n++}}}while(n);return e}function k(e,t){let n;do{n=0;const r=e("loop");if(r.length>0)for(const o of r){let r,i,a;c&&(o.attribs=_(o));for(const e in o.attribs)if("through"===e){let n=o.attribs[e];n.startsWith("{")&&(n=E(n,t)),r=L(t,n)}else"key"===e?i=o.attribs[e]:"val"===e&&(a=o.attribs[e]);if(!r){l.verbosity>1&&console.warn("teddy encountered a loop without a through attribute."),e(o).replaceWith("");continue}if(!i&&!a){l.verbosity>1&&console.warn("teddy encountered a loop without a key or a val attribute."),e(o).replaceWith("");continue}let d="",u=e(o).html();r instanceof Set&&(r=[...r]);for(const e in r){const n=r[e],o=Object.assign({},t);L(o,i,e),L(o,a,n);const c=u.includes("</noteddy>"),l=u.includes("</noparse>"),f=u.includes("</pre>");if((u.includes("</escape>")||u.includes("\x3c!--#"))&&(u=A(u)),c||l||f){let e=s(u);e=y(e,o),u=e.html()}const p=E(u,o)||"",h=p.includes("</noteddy>"),m=p.includes("</noparse>"),b=p.includes("</if>"),x=p.includes("</unless>"),w=p.includes(" true="),W=p.includes(" false="),C=p.includes("</loop>"),T=p.includes("</inline>"),O=p.includes(" selected-value=")||p.includes(" checked-value=");let S=s(p||"");(h||m)&&(S=y(S,o)),(b||x)&&(S=g(S,o)),(w||W)&&(S=v(S,o)),C&&(S=k(S,o)),T&&(S=$(S,o)),O&&(S=N(S,o)),d+=S.html()}const f=s(d||"");e(o).replaceWith(f.html()),n++}}while(n);return e}function $(e,t){let n;do{n=0;const r=e("inline");if(r.length>0)for(const s of r){let r,o;c&&(s.attribs=_(s));for(const e in s.attribs)"css"===e?r=L(t,s.attribs[e]):"js"===e&&(o=L(t,s.attribs[e]));if(!r&&!o){l.verbosity>1&&console.warn("teddy encountered an <inline> element without a css or js attribute."),e(s).replaceWith("");continue}let i="";i=r?`<style>${r}</style>`:`<script>${o}<\/script>`,e(s).replaceWith(i),n++}}while(n);return e}function A(e){return e.replace(/<escape>(.*?)<\/escape>/gs,((e,t)=>S(t.trim())))}function N(e,t){let n;do{n=0;const r=e("select[selected-value], [checked-value]");if(r.length>0)for(const s of r){c&&(s.attribs=_(s));for(let n in s.attribs){const r=n;if(n.includes("-teddyduplicate")&&(n=n.split("-teddyduplicate")[0]),"selected-value"===n){const n=E(s.attribs[r],t)||s.attribs[r],o=e(s).find("option[value]");for(const t of o)c&&(t.attribs=_(t)),t.attribs.value===n&&e(t).attr("selected","selected");e(s).removeAttr(r)}else if("checked-value"===n){const n=E(s.attribs[r],t)||s.attribs[r],o=e(s).find('input[type="checkbox"][value], input[type="radio"][value]');for(const t of o)c&&(t.attribs=_(t)),t.attribs.value===n&&e(t).attr("checked","checked");e(s).removeAttr(r)}}n++}}while(n);return e}function E(e,t){let n;try{n=j(e,"{","}")}catch(t){return e}const r=n.length;for(let s=0;s<r;s++){let r=n[s];if(""===r)continue;if(!/^(\d+|[a-zA-Z_$][a-zA-Z0-9_$|{}.-]*(\.[a-zA-Z_$][a-zA-Z0-9_$|{}.-]*)*)$/.test(r)){l.verbosity>2&&console.warn(`teddy.parseVars encountered a {variable} that could not be parsed: {${r}}`);continue}if(r.includes("{")){const n=r;r=E(r,t);try{e=(e=e.replace(new RegExp(`\${${n}}`,"i"),(()=>`\${${r}}`))).replace(new RegExp(`{${n}}`,"i"),(()=>`{${r}}`))}catch(e){l.verbosity>2&&console.warn(`teddy.parseVars encountered a {variable} that could not be parsed: {${n}}`)}}const o=r.slice(-6);if(o.includes("|p")){const n=r;r=r.substring(0,r.length-2*(o.split("|").length-1));let s=L(t,r);if(s||o.includes("|d")||"hide"!==l.emptyVarBehavior&&!o.includes("|h")||(s=""),"string"==typeof s&&s.startsWith("{")&&s.includes("|d")&&(s=s.replace("|d","")),s||""===s){const r=t._noTeddyBlocks.push(s)-1;try{try{e=(e=e.replace(new RegExp(`\${${n}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),`<noteddy id="${r}"></noteddy>`)).replace(new RegExp(`{${n}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),`<noteddy id="${r}"></noteddy>`)}catch(e){l.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=r;r=r.substring(0,r.length-2*(o.split("|").length-1));let s=L(t,r),i=!1;s||o.includes("|d")||"hide"!==l.emptyVarBehavior&&!o.includes("|h")?s||""===s||(i=!0,s=`{${n}}`):s="","string"==typeof s&&s.startsWith("{")&&s.includes("|d")&&(s=s.replace("|d",""));try{i||(e=e.replace(new RegExp(`\${${n}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),(()=>s))),e=e.replace(new RegExp(`{${n}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),(()=>s))}catch(t){return e}}else{let n=L(t,r),s=!1;n||o.includes("|d")||"hide"!==l.emptyVarBehavior&&!o.includes("|h")?n||""===n?n=S(n):0===n?n="0":(s=!0,n=`{${r}}`):n="","string"==typeof n&&n.startsWith("{")&&n.includes("|d")&&(n=n.replace("|d",""));try{s||(e=e.replace(new RegExp(`\${${r}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),(()=>n))),e=e.replace(new RegExp(`{${r}}`.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d"),"i"),(()=>n))}catch(t){return e}}}return e}function W(e,t){let n;do{n=0;const r=e("cache[defer]");if(r.length>0)for(const s of r){c&&(s.attribs=_(s));const r=s.attribs.name,o=s.attribs.key||"none",i=parseInt(s.attribs.maxAge||s.attribs.maxage)||0,a=parseInt(s.attribs.maxCaches||s.attribs.maxcaches)||1e3,l=Date.now(),d=e(s).html();if(u[r]||(u[r]={key:o,maxAge:i,maxCaches:a,entries:{}}),u[r].entries[s.attribs.key?L(t,o):"none"]={lastAccessed:l,created:l,markup:d},Object.keys(u[r].entries).length>a){const e=Object.keys(u[r].entries).reduce(((e,t)=>u[r].entries[e].lastAccessed<u[r].entries[t].lastAccessed?e:t));delete u[r].entries[e]}e(s).replaceWith(d),n++}}while(n);return e}const C={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&#34;","'":"&#39;"},T=Object.keys(C),O=T.length;function S(e){let t,n,r=!1,s="";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(r=!1,n=0;n<O;n++)if(e[t]===T[n]){s+=C[T[n]],r=!0;break}r||(s+=e[t])}return s}function j(e,t,n){const r=[],s=[],o=t.length,i=n.length;for(let a=0;a<e.length;a++)if(e.substring(a,a+o)===t)r.push(a+o),a+=o-1;else if(e.substring(a,a+i)===n){const t=r.pop();0===r.length&&s.push(e.substring(t,a)),a+=i-1}const a=[],c=/{!([^{}]*)!}/g;let l;for(const e of s){for(;null!==(l=c.exec(e));)a.push(l[1]);a.push(e)}return a}function L(e,t,n){return!!e&&(t&&"boolean"!=typeof t&&"number"!=typeof t?"string"==typeof t?L(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&&r(e,t[0]):L(r(e,t[0]),t.slice(1),n):t);function r(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 _(e){const t=e.attributes,n={};for(let e=0;e<t.length;e++){const r=t[e];n[r.name]=r.value}return n}function R(){l.verbosity=1,l.templateRoot="./",l.maxPasses=1e3,l.emptyVarBehavior="display",l.includeNotFoundBehavior="display"}function B(e,n,r){if("string"!=typeof e)return l.verbosity>1&&console.warn("teddy.render attempted to render a template which is not a string."),"function"==typeof r?r(null,""):"";let o,i;"object"!=typeof n&&(l.verbosity>1&&console.warn("teddy.render was passed an invalid model."),n={}),n._noTeddyBlocks=[],n.settings&&n.settings.views&&t&&(l.templateRoot=t.resolve(n.settings.views)),e.slice(l.templateRoot.length)===l.templateRoot&&(e=e.replace(l.templateRoot,""));const a=f[e];let d=null,u=null;if(a){const e=a.none;if(e)if(e.created){if(!e.maxAge&&e.maxage)return"function"==typeof r?r(null,e.markup):e.markup;if(!(e.created+(e.maxAge||e.maxage)<Date.now()))return"function"==typeof r?r(null,e.markup):e.markup;d="none"}else d="none";else for(const e in a)if(u=L(n,e),u){const t=a[e];for(const n in t.entries)if(n===u){const s=t.entries[n];if(t.maxAge||t.maxage){if(s.created+(t.maxAge||t.maxage)<Date.now()){d=e;break}return"function"==typeof r?r(null,s.markup):s.markup}return"function"==typeof r?r(null,s.markup):s.markup}d=e;break}}i=p(e),c||(i=i.replace(/<([a-zA-Z][a-zA-Z0-9-]*)([^>]*)>/g,((e,t,n)=>{const r=new Map;let s=1;return`<${t}${n.replace(/([a-zA-Z0-9-:._]+)(?:=(["'])(.*?)\2|([^>\s]+))?/g,((e,t,n,o)=>{if(r.has(t)){const n=`${t}-teddyduplicate${s++}`;return e.replace(t,n)}return r.set(t,!0),e}))}>`})));let x;(i.includes("</escape>")||i.includes("\x3c!--#"))&&(i=A(i)),o=s(i||"");let w=0,C=!1;do{if(w++,w>l.maxPasses){l.verbosity>0&&console.error(`teddy could not finish rendering the template because the max number of passes over the template (${l.maxPasses}) was exceeded; there may be an infinite loop in your template logic.`);break}const e=i.includes("</cache>"),t=i.includes("</noteddy>"),r=i.includes("</noparse>"),a=i.includes("</pre>"),c=i.includes("</if>"),d=i.includes("</unless>"),u=i.includes(" true="),f=i.includes(" false="),p=i.includes("</include>"),A=i.includes("</loop>"),T=i.includes("</inline>"),O=i.includes(" selected-value=")||i.includes(" checked-value=");x=i||"",w>1&&(o=s(i||""),C&&(o=b(o,n,!0))),e&&(o=m(o,n)),(t||r||a)&&(o=y(o,n)),(c||d)&&(o=g(o,n)),(u||f)&&(o=v(o,n)),p&&(o=b(o,n)),A&&(o=k(o,n)),T&&(o=$(o,n)),O&&(o=N(o,n));const S=i.includes("</cache>");i=o.html(),i=E(i,n),C&&(i=h(i),C=!1),i.includes('teddydeferreddynamicinclude="true"')&&(x="",C=!0),x===i&&S&&(o=s(i||""),o=W(o,n),i=o.html())}while(x!==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>"))&&(o=s(i||""),o=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=c?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(),c&&(t.attribs=_(t));for(const n in t.attribs)("true"===n||"false"===n||"parse"===n||"teddydeferredonelineconditional"===n||n.startsWith("if-"))&&e(t).removeAttr(n)}}return e}(o),i=o.html());for(const e in n._noTeddyBlocks)i=i.replace(`<noteddy id="${e}"></noteddy>`,(()=>n._noTeddyBlocks[e])),i=i.replace(`<noteddy id="${e}" pre="true"></noteddy>`,(()=>n._noTeddyBlocks[e]));if(c&&(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"===d)f[e].none.markup=i,f[e].none.created=Date.now();else if(d&&(f[e][d].entries[u]||(f[e][d].entries[u]={}),f[e][d].entries[u].markup=i,f[e][d].entries[u].created=Date.now(),Object.keys(f[e][d].entries).length>f[e][d].maxCaches)){const t=Object.keys(f[e][d].entries).reduce(((t,n)=>f[e][d].entries[t].created<f[e][d].entries[n].created?t:n));delete f[e][d].entries[t]}return"function"==typeof r?r(null,i):i}const M={params:l,caches:u,templateCaches:f,compile:function(e){return function(t){return B(e,t)}},setDefaultParams:R,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}l.verbosity=e},setTemplateRoot:function(e){l.templateRoot=String(e)},setMaxPasses:function(e){l.maxPasses=Number(e)},setEmptyVarBehavior:function(e){l.emptyVarBehavior="hide"===e?"hide":"display"},setIncludeNotFoundBehavior:function(e){l.includeNotFoundBehavior="hide"===e?"hide":"display"},getTemplates:function(){return d},setTemplate:function(e,t){d[e]=t},clearTemplates:function(){d={}},setCache:function(e){f[e.template]||(f[e.template]={}),e.key?f[e.template][e.key]={maxAge:e.maxAge||e.maxage,maxCaches:e.maxCaches||e.maxcaches||1e3,entries:{}}:f[e.template].none={maxAge:e.maxAge||e.maxage,markup:null,created:null}},clearCache:function(e,t){if("string"==typeof e)t?delete u[e].entries[t]:delete u[e];else if("object"==typeof e){const t=e;t.key?delete f[t.template][t.key]:delete f[t.template]}else l.verbosity>0&&console.error("teddy: invalid params passed to clearCache.")},render:B,__express:B}})(),r=r.default})())); //# sourceMappingURL=teddy.min.js.map