Repository navigation
Expand file tree
/
Copy pathTangular.min.js
More file actions
1 lines (1 loc) · 7.14 KB
/
Copy pathTangular.min.js
File metadata and controls
1 lines (1 loc) · 7.14 KB
1
(function(e){if(e.Tangular)return;var a={};var v=a.helpers={};a.version='v5.0.6';a.cache={};e.Ta=e.Tangular=a;e.Thelpers=v;var f={null:true,undefined:true,true:true,false:true,Object:1,String:1,Number:1,Boolean:1,Date:1,Array:1,window:1,global:1,arguments:1,eval:1,Function:1,function:1,var:1,let:1,const:1,delete:1,typeof:1,instanceof:1,new:1,in:1,void:1,this:1,NaN:1,Infinity:1,Math:1,JSON:1,RegExp:1,parseInt:1,parseFloat:1,isNaN:1,isFinite:1,encodeURIComponent:1,decodeURIComponent:1,$index:1};var l=/\{\{.*?\}\}/g;var s=/\{\{|\}\}/g;var t=/[<>&"']/g;var n=/[<>&"']/;var u=/\n$/g;var o=/[.*+?^${}()|[\]\\]/g;var c=/^[a-zA-Z_$][\w$]*(?:\[[^[\]]*\])*(?:\.[a-zA-Z_$][\w$]*(?:\[[^[\]]*\])*)+/;var p=1e3;var h=0;function d(e,r){return e>64&&e<91||e>96&&e<123||e===95||e===36||!r&&e>47&&e<58}function w(e,r){var t=[];var n=e.length;var i=0;var a=0;while(a<n){var l=e.charCodeAt(a);if(l===96||l===34||l===39){a++;while(a<n){var s=e.charCodeAt(a++);if(s===92)a++;else if(s===l)break}i=l;continue}if(l>47&&l<58){a++;while(a<n&&(d(e.charCodeAt(a))||e.charCodeAt(a)===46))a++;i=l;continue}if(d(l,true)){var u=a++;while(a<n&&d(e.charCodeAt(a)))a++;if(i!==46&&i!==124){var o=e.substring(u,a);if(!f[o]&&t.indexOf(o)===-1&&(!r||r.indexOf(o)===-1))t.push(o)}i=l;continue}if(l!==32&&l!==9&&l!==10&&l!==13)i=l;a++}return t}function y(e){var r=[];var t=0;var n=0;var i=0;for(var a=0;a<e.length;a++){var l=e.charCodeAt(a);if(i){if(l===92)a++;else if(l===i)i=0;continue}if(l===96||l===34||l===39)i=l;else if(l===40||l===91||l===123)n++;else if(l===41||l===93||l===125)n--;else if(l===124&&n<=0){r.push(e.substring(t,a));t=a+1}}r.push(e.substring(t));return r}a.toArray=function(e){var r=Object.keys(e);var t=[];for(var n=0,i=r.length;n<i;n++)t.push({key:r[n],value:e[r[n]]});return t};function m(){this.commands;this.variables;this.builder;this.split='\0'}m.prototype.compile=function(e,r,t){var d=this;var m=0;var g=0;var b=[];var n=l;var $=s;var x;if(r&&t){if(r.indexOf('\\')===-1)r=r.replace(o,'\\$&');if(t.indexOf('\\')===-1)t=t.replace(o,'\\$&');n=new RegExp(r+'.*?'+t,'g');$=new RegExp(r+'|'+t,'g')}d.template=e;e=e.replace(/\|\|/g,'');d.variables={};d.commands=[];d.builder=e.replace(n,function(e){var r=e.replace($,'').trim();var t=null;var n=null;var i;var a=false;var l=false;var s=true;if(r==='fi'){m--}else if(r==='end'){g--;b.pop()}else if(r.substring(0,3)==='if '){m++;t=w(r.substring(3),b);if(t.length){for(var u=0;u<t.length;u++){var o=t[u];if(d.variables[o])d.variables[o]++;else d.variables[o]=1}}else t=null;a=true;s=true}else if(r.substring(0,8)==='foreach '){g++;x=r.substring(8).split(' ');b.push(x[0].trim());i=x[2].indexOf('.');if(i!==-1)x[2]=x[2].substring(0,i);t=x[2].trim();if(b.indexOf(t)===-1){if(d.variables[t])d.variables[t]++;else d.variables[t]=1;t=[t]}else t=null;l=true}else if(r.substring(0,8)==='else if '){t=w(r.substring(8),b);if(t.length){for(var u=0;u<t.length;u++){var o=t[u];if(d.variables[o])d.variables[o]++;else d.variables[o]=1}}else t=null;a=true}else if(r!=='continue'&&r!=='break'&&r!=='else'){t=w(r);var f=false;for(var u=0;u<t.length;u++){var c=t[u];if(c+'('===r.substring(0,c.length+1)){f=true;continue}if(d.variables[c])d.variables[c]++;else d.variables[c]=1}if(!t.length)t=null;var v=null;n=y(r);if(f){v=n[0];n[0]=''}else if(n.length===1)n.push(' encode');r=n[0];n=n.slice(1);if(f)n.unshift(v);if(n.length){for(var u=0;u<n.length;u++){var p=n[u].trim();var h=f&&!u;i=p.indexOf('(');if(i===-1){p='Thelpers.$execute($helpers,model,\''+p+'\','+(h?'':')')}else p='Thelpers.$execute($helpers,model,\''+p.substring(0,i)+'\','+(h?'':',')+p.substring(i+1);n[u]=p}}else n=null;r=d.safe(r.trim()||'model');s=false}d.commands.push({index:d.commands.length,cmd:r,ifcount:m,loopcount:g,variable:t,helpers:n,isloop:l,isif:a,iscode:s});return d.split}).split(d.split);for(var i=0;i<d.builder.length;i++){var a=d.builder[i];d.builder[i]=a?a.replace(u,''):a}return d.make()};m.prototype.safe=function(e){var r=e.match(c);if(!r)return e;var t=r[0];var n=[];var i=0;for(var a=0;a<t.length;a++){var l=t.charCodeAt(a);if(l===91)i++;else if(l===93)i--;else if(l===46&&!i)n.push(t.substring(0,a)+'==null?\'\':')}return n.join('')+e};m.prototype.make=function(){var e=this;var r=['var $output=$text[0];var $tmp;var $index=0;'];var t=[];var n=0;for(var i=0,a=e.commands.length;i<a;i++){var l=e.commands[i];var s;i&&r.push('$output+=$text['+i+'];');if(l.iscode){if(l.isloop){var u='$i'+n++;var o=u+'a';var f=u+'s';var c=l.cmd.split(' ')[1];var v=l.cmd.lastIndexOf(' in ');if(v===-1)v=l.cmd.lastIndexOf(' of ');s=l.cmd.substring(v+4).trim();s=o+'='+e.safe(s)+';if(!('+o+' instanceof Array)){if('+o+'&&typeof('+o+')===\'object\')'+o+'=Tangular.toArray('+o+')}if('+o+' instanceof Array&&'+o+'.length){for(var '+u+'=0,'+u+'l='+o+'.length;'+u+'<'+u+'l;'+u+'++){$index='+u+';var '+c+'='+o+'['+u+'];';r.push('var '+f+'='+c+';'+s);t.push({name:u,backup:f,item:c})}else if(l.isif){if(l.cmd.substring(0,8)==='else if ')r.push('}'+l.cmd.substring(0,8).trim()+'('+l.cmd.substring(8).trim()+'){');else r.push(l.cmd.substring(0,3).trim()+'('+l.cmd.substring(3).trim()+'){')}else{switch(l.cmd){case'else':r.push('}else{');break;case'end':s=t.pop();r.push('}}'+(s?s.item+'='+s.backup+';':'')+(t.length?'$index='+t[t.length-1].name+';':''));break;case'fi':r.push('}');break;case'break':r.push('break;');break;case'continue':r.push('continue;');break}}}else{if(l.helpers){var p='';for(var h=0;h<l.helpers.length;h++){var d=l.helpers[h];if(h===0)p=d.replace('',l.cmd.trim()).trim();else p=d.replace('',p.trim())}r.push('$tmp='+p+';if($tmp!=null)$output+=$tmp;')}else r.push('if('+l.cmd+'!=null)$output+='+l.cmd+';')}}r.push((a?'$output+=$text['+a+'];':'')+'return $output.charAt(0) === \'\\n\'?$output.substring(1):$output;');delete e.variables.$;var m=Object.keys(e.variables);var f=['$helpers||{}','$||{}','model'];for(var i=0;i<m.length;i++)f.push('model.'+m[i]);for(var i=0;i<e.builder.length;i++)e.builder[i]=e.builder[i].replace(/\1/g,'||');var g='var tangular=function($helpers,$,model'+(m.length?','+m.join(','):'')+'){'+r.join('')+'};return function(model,$,$helpers){if(model==null)model={};try{return tangular('+f.join(',')+')}catch(e){console.error(\'Tangular error:\',e + \'\',$template)}}';return new Function('$text','$template',g.replace(/\1/g,'||'))(e.builder,e.template)};v.$execute=function(e,r,t,n,i,a,l,s,u,o,f){var c=e[t]||v[t];if(!c){console&&console.warn('Tangular: missing helper','"'+t+'"');return n}return c.call(r,n,i,a,l,s,u,o,f)};v.encode=function(e,r){if(r){if(typeof r==='function')return r(e);if(r==='json')return JSON.stringify(e);if(r==='json2')return JSON.stringify(e,null,'\t');if(r==='url'||r==='urlencoded')return encodeURIComponent(e);if(r==='querify')return typeof QUERIFY==='function'?QUERIFY(e):e}if(e==null)return'';e=e+'';return n.test(e)?e.replace(t,i):e};function i(e){switch(e){case'&':return'&';case'<':return'<';case'>':return'>';case'"':return'"';case'\'':return'''}return e}v.raw=function(e){return e};a.render=function(e,r,t,n){var i=a.cache[e];if(!i){if(h++>=p){a.cache={};h=1}i=a.cache[e]=(new m).compile(e)}return i(r==null?{}:r,t,n)};a.compile=function(e,r,t){return(new m).compile(e,r,t)};a.register=function(e,r){v[e]=r;return a};if(typeof module!=='undefined'&&module.exports)module.exports=a})(typeof window==='undefined'?global:window);