stpl
Version:
tiny mustache extended style template system
2 lines • 2.97 kB
JavaScript
/* (c) Jonathan Gotti - licence: https://github.com/malko/stpl/LICENCE.txt */
(function(r){"use strict";function t(r){return r[p](/[<>"'`]|&(?!amp;)/g,function(r){return y[r]})}function n(r,t){return g[r]=t[p](/^\s+|\s+$/g,""),this}function e(r,t){return n(t||r[a]("rel"),r.innerHTML),r.parentNode.removeChild(r),this}function i(r,t){return h[r]=t||d[r],this}function s(){var r,t,n=document;if(n.querySelectorAll)r=n.querySelectorAll('script[type="'+f.tplType+'"][rel]');else{var i=n.getElementsByTagName("script");for(t=i[c],r=[];t--;)i[t][a]("type")===f.tplType&&i[t][a]("rel")&&r.push(i[t])}for(t=r[c];t--;)e(r[t]);return this}function u(r,t,n){var e,s;if(~t.indexOf("|")){var o,f,a,g=t.split("|");for(t=g.shift(),o=u(r,t,n),e=0,s=g[c];s>e;e++)g[e].match(/^\s*!?=/)?g[e][p](/^\s*(!?)=(.*)\s*$/,function(r,t,n){o=t?o!=n?o:!1:o==n?o:!1}):(~(a=g[e].indexOf(":"))?((f=g[e][l](a+1).split(",")).splice(0,0,o),g[e]=g[e][l](0,a)):f=[o],h[g[e]]||i(g[e]),o=h[g[e]].apply(r,f));return o}for(t.match(/^\//)&&(t=t[l](1),n="");t.match(/^..\//);)n=~n.indexOf(".")?"":n[p](/\.[^\.]*$/,""),t=t[p](/^..\//,"");if(t=t[p](/^\.+/,""),n=n[p](/^\/+/,"")[p](/^\.+/,""),t=(n||"")+(n&&t?".":"")+t,!t[c])return r;t=t.split(/\s*\.\s*/);var y=r;for(e=0,s=t[c];s>e;e++){if(!("object"==typeof y&&t[e]in y))return null;y=y[t[e]]}return y}function o(r,n,e){return e||(e=""),r[p](/\{\{\s*#([^\{\s]+?)(?:\s+as\s+([^\{\s]+?))?\}\}([\s\S]*?)\{\{\/\1\}\}/g,function(r,t,i,s){var f,c,p,a,l=[],g=u(n,t,e),h=!1;if(!g)return"";if(!i){p=(e?e+".":"")+t;for(a in g)g.hasOwnProperty(a)&&l.push(o(s,n,p+"."+a));return l.join("")}if(f=g,g=u(n,"",e),!g)return"";i in g&&(h=!0,c=g[i]);for(a in f)g[i]=f[a],f.hasOwnProperty(a)&&l.push(o(s,n,e));return h?g[i]=c:delete g[i],l.join("")})[p](/\{\{\s*(([^\{\s\|]+?)(\|[^\{\s]+?)?)\s*(!)?\?\s*\}\}([\s\S]*?)\{\{\s*\?\s*(\1|\2)\s*\}\}/g,function(r,t,i,s,f,p){var a=u(n,t,e),l=a?a instanceof Array?a[c]:!0:!1;return f&&(l=!l),l?o(p,n,e):""})[p](/\{\{\s*>\s*([a-z_][a-z_0-9-]*)\s*\}\}/gi,function(r,t){return g[t]?o(g[t],n,e):""})[p](/\{\{\{\s*([^\{\s]*?)\s*\}\}\}/g,function(r,i){return t(u(n,i,e))})[p](/\{\{\s*([^\{\s]*?)\s*\}\}/g,function(r,t){return u(n,t,e)})}function f(r,t){return void 0===g[r]&&(s(),void 0===g[r])?!1:o(g[r],t)}var c="length",p="replace",a="getAttribute",l="substr",g={},h={empty:function(r){return!!(!r||"0"===r||r instanceof Array&&!r[c])},jsonEncode:function(r){return JSON.stringify(r)},lowerCase:function(r){return(""+r).toLowerCase()},ucFirst:function(r){return r=""+r,r[l](0,1).toUpperCase()+r[l](1)},upperCase:function(r){return(""+r).toUpperCase()},nl2br:function(r){return(""+r).replace(/\n/g,"<br />")}},y={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},d="undefined"!=typeof global?global:Function("return this;")();f.tplType="text/stpl",f.registerScriptTag=e,f.registerString=n,f.registerFilter=function(r,t){return i(r,t),this},f.preload=s,f.renderString=o,r.stpl=f})("object"==typeof exports?exports:this);