bunkerbox/templates/js/mustache.min.js

1 line
7.0 KiB
JavaScript
Raw Permalink Normal View History

2022-04-20 23:47:48 +02:00
var objectToString=Object.prototype.toString,isArray=Array.isArray||function(e){return"[object Array]"===objectToString.call(e)};function isFunction(e){return"function"==typeof e}function typeStr(e){return isArray(e)?"array":typeof e}function escapeRegExp(e){return e.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function hasProperty(e,t){return null!=e&&"object"==typeof e&&t in e}function primitiveHasOwnProperty(e,t){return null!=e&&"object"!=typeof e&&e.hasOwnProperty&&e.hasOwnProperty(t)}var regExpTest=RegExp.prototype.test;function testRegExp(e,t){return regExpTest.call(e,t)}var nonSpaceRe=/\S/;function isWhitespace(e){return!testRegExp(nonSpaceRe,e)}var entityMap={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;","/":"&#x2F;","`":"&#x60;","=":"&#x3D;"};function escapeHtml(e){return String(e).replace(/[&<>"'`=\/]/g,function(e){return entityMap[e]})}var whiteRe=/\s*/,spaceRe=/\s+/,equalsRe=/\s*=/,curlyRe=/\s*\}/,tagRe=/#|\^|\/|>|\{|&|=|!/;function parseTemplate(e,t){if(!e)return[];var r,n,i,a=!1,s=[],o=[],c=[],p=!1,u=!1,h="",l=0;function f(){if(p&&!u)for(;c.length;)delete o[c.pop()];else c=[];p=!1,u=!1}function g(e){if("string"==typeof e&&(e=e.split(spaceRe,2)),!isArray(e)||2!==e.length)throw new Error("Invalid tags: "+e);r=new RegExp(escapeRegExp(e[0])+"\\s*"),n=new RegExp("\\s*"+escapeRegExp(e[1])),i=new RegExp("\\s*"+escapeRegExp("}"+e[1]))}g(t||mustache.tags);for(var d,v,y,m,w,x,C=new Scanner(e);!C.eos();){if(d=C.pos,y=C.scanUntil(r))for(var W=0,R=y.length;W<R;++W)isWhitespace(m=y.charAt(W))?(c.push(o.length),h+=m):(u=!0,a=!0,h+=" "),o.push(["text",m,d,d+1]),d+=1,"\n"===m&&(f(),h="",l=0,a=!1);if(!C.scan(r))break;if(p=!0,v=C.scan(tagRe)||"name",C.scan(whiteRe),"="===v?(y=C.scanUntil(equalsRe),C.scan(equalsRe),C.scanUntil(n)):"{"===v?(y=C.scanUntil(i),C.scan(curlyRe),C.scanUntil(n),v="&"):y=C.scanUntil(n),!C.scan(n))throw new Error("Unclosed tag at "+C.pos);if(w=">"==v?[v,y,d,C.pos,h,l,a]:[v,y,d,C.pos],l++,o.push(w),"#"===v||"^"===v)s.push(w);else if("/"===v){if(!(x=s.pop()))throw new Error('Unopened section "'+y+'" at '+d);if(x[1]!==y)throw new Error('Unclosed section "'+x[1]+'" at '+d)}else"name"===v||"{"===v||"&"===v?u=!0:"="===v&&g(y)}if(f(),x=s.pop())throw new Error('Unclosed section "'+x[1]+'" at '+C.pos);return nestTokens(squashTokens(o))}function squashTokens(e){for(var t,r,n=[],i=0,a=e.length;i<a;++i)(t=e[i])&&("text"===t[0]&&r&&"text"===r[0]?(r[1]+=t[1],r[3]=t[3]):(n.push(t),r=t));return n}function nestTokens(e){for(var t,r=[],n=r,i=[],a=0,s=e.length;a<s;++a)switch((t=e[a])[0]){case"#":case"^":n.push(t),i.push(t),n=t[4]=[];break;case"/":i.pop()[5]=t[2],n=i.length>0?i[i.length-1][4]:r;break;default:n.push(t)}return r}function Scanner(e){this.string=e,this.tail=e,this.pos=0}function Context(e,t){this.view=e,this.cache={".":this.view},this.parent=t}function Writer(){this.templateCache={_cache:{},set:function(e,t){this._cache[e]=t},get:function(e){return this._cache[e]},clear:function(){this._cache={}}}}Scanner.prototype.eos=function(){return""===this.tail},Scanner.prototype.scan=function(e){var t=this.tail.match(e);if(!t||0!==t.index)return"";var r=t[0];return this.tail=this.tail.substring(r.length),this.pos+=r.length,r},Scanner.prototype.scanUntil=function(e){var t,r=this.tail.search(e);switch(r){case-1:t=this.tail,this.tail="";break;case 0:t="";break;default:t=this.tail.substring(0,r),this.tail=this.tail.substring(r)}return this.pos+=t.length,t},Context.prototype.push=function(e){return new Context(e,this)},Context.prototype.lookup=function(e){var t,r=this.cache;if(r.hasOwnProperty(e))t=r[e];else{for(var n,i,a,s=this,o=!1;s;){if(e.indexOf(".")>0)for(n=s.view,i=e.split("."),a=0;null!=n&&a<i.length;)a===i.length-1&&(o=hasProperty(n,i[a])||primitiveHasOwnProperty(n,i[a])),n=n[i[a++]];else n=s.view[e],o=hasProperty(s.view,e);if(o){t=n;break}s=s.parent}r[e]=t}return isFunction(t)&&(t=t.call(this.view)),t},Writer.prototype.clearCache=function(){void 0!==this.templateCache&&this.templateCache.clear()},Writer.prototype.parse=function(e,t){var r=this.templateCache,n=e+":"+(t||mustache.tags).join(":"),i=void