-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhisker.min.js
2 lines (2 loc) · 12.3 KB
/
whisker.min.js
1
2
!function(){function e(a,b){var c=a+"$#"+b;return c=c.replace(/./g,function(a){return d[a]?"\\"+a:"#"==a?"([_a-zA-Z0-9]*)":a}),new RegExp(c,"g")}function f(a,b){b=b||a,a.length+b.length<=4?(this.beginNum=a.length,this.begin=a,this.beginNum>1&&(this.b=[a.charAt(0),a.charAt(1)]),this.endNum=b.length,this.end=b,this.endNum>1&&(this.e=[b.charAt(0),b.charAt(1)])):console&&console.warn&&console.warn("max delimeter length is 2"),c=e(a,b)}function g(a){return new Function("","return "+a)()}function h(a,b,c,d){this.result=[],this.branchStack=[],this.blockName=b,this.blockArgs=c,this.blockScope=d,this.type="block",this.idx=a.idx,this.parent=a.Block(),this.context=a}function i(a,b,c){b=b||{},this.blockStack=[{result:[],blockName:"",blockArgs:"",blockScope:b,type:"block",branchStack:[]}],this.text="",this.idx=0,this.skipMode=!1,this.scope=b,this.blockContent={},this.partials=c||{},this.html=a,this.htmlStack=[],this.deferEval=!1,this.Block=function(){return this.blockStack[this.blockStack.length-1]};for(var d=0;d<l.mode.length;d++)this.blockContent[l.mode.charAt(d)]=""}function m(c,d){var e,f;if(d.state!=a.IN_BLOCK&&b.delimeter.isBegin(c,d.idx,d.html))e=d.html.charAt(d.idx+b.delimeter.beginNum),l.filter.test(e)?(d.skipMode||(d.Block().result.push(d.text),d.text=""),d.state=a.IN_BLOCK,d.blockType=e,f=l.handlers[e],f.onStartBlock&&f.onStartBlock(d),d.idx+=b.delimeter.beginNum):d.skipMode||(d.text+=c);else if(d.state==a.IN_BLOCK)if(b.delimeter.isEnd(c,d.idx,d.html)){d.state=a.OUT_BLOCK;var g=d.blockContent[d.blockType];f=l.handlers[d.blockType],d.blockContent[d.blockType]="",f.onEndBlock&&f.onEndBlock(g,d),d.idx+=b.delimeter.endNum-1}else f=l.handlers[d.blockType],f.onInBlock?f.onInBlock(c,d):d.blockContent[d.blockType]+=c;else d.skipMode||(d.text+=c)}function v(a,b,c){if(!a)return a;for(var d="",e=new i(a,b,c);e.idx<e.html.length&&(d=e.html.charAt(e.idx),m(d,e),!e.error);)e.idx++,e.endPartials();if(e.error)return e.errorMsg;if(e.blockStack.length>1){var f=e.Block().blockName;e.throwError("{#"+f+'} need a close block "{/'+f+'}"')}return e.Block().result.push(e.text),e.resolveBlock()}function w(a,b){return a.replace(c,function(a,c){var d=b[c];return void 0==d?"undefined":d.toString()})}var c,a={OUT_BLOCK:0,IN_BLOCK:1},b={},d=function(){for(var a="^$()[]{}.?+*|".split(""),b={},c=0;c<a.length;c++)b[a[c]]=!0;return b}();f.prototype={isBegin:function(a,b,c){return 1==this.beginNum?a==this.begin:a==this.b[0]&&c.charAt(b+1)==this.b[1]},isEnd:function(a,b,c){return 1==this.endNum?a==this.end:a==this.e[0]&&c.charAt(b+1)==this.e[1]}},b.delimeter=new f("{","}"),h.prototype={resolve:function(){}},i.prototype={startPartials:function(a){var b=this.partials[a];b?(this.htmlStack.push({html:this.html,idx:this.idx+1}),this.html=b,this.idx=-1):this.throwError('undefined partials! :"'+a+'"')},endPartials:function(){if(this.idx>=this.html.length&&this.htmlStack.length>0)do{var a=this.htmlStack.pop();this.html=a.html,this.idx=a.idx}while(this.idx>=this.html.length&&this.htmlStack.length>0)},saveVar:function(a,b){this.Block().result.push({exp:a,type:b,idx:this.idx})},saveBranch:function(a){var b={type:"branch",exp:a,elseGroup:[]},c=this.Block();c.result.push(b),c.branchStack.push(b)},setElse:function(a,b){var c=this.Block(),d=c.branchStack[c.branchStack.length-1],e={exp:a,If:d,flag:b,type:"else"};if(!d)return this.throwError('else or else if need a if in this block "'+c.blockName+'"',this),void 0;var f=d.elseGroup[d.elseGroup.length-1];f&&f.flag&&!a&&this.throwError("else if or else can't after else",this),f||(f=d),f.nextIndex=c.result.length,c.result.push(e),d.elseGroup.push(e)},endBranch:function(){var a=this.Block(),b=a.branchStack.pop();if(b){b.endIndex=a.result.length;var c=b.elseGroup[b.elseGroup.length-1];c&&(c.nextIndex=a.result.length)}else this.throwError("{/if} need a if",this)},newBlock:function(a,b,c){this.blockStack.push({result:[],branchStack:[],blockName:a,blockArgs:b,blockScope:c||this.Block().blockStack,type:"block",idx:this.idx,parent:this.Block()})},closeBlock:function(){var a=this.blockStack.pop();a.branchStack.length>0&&this.throwError("unclosed if!",this),this.Block().result.push(a),1==this.blockStack.length&&(this.deferEval=!1)},test:function(a,b){var e,c=this,d=b,f=/^(\!)?(\$\^?[$_a-zA-Z0-9.$]*)$/.exec(b);if(null!=f){var h=this.eval(a,f[2].slice(1));return e=h instanceof Array?h.length>0:!!h,"!"==f[1]&&(e=!e),e}b=b.replace(/\$(\^?[_a-zA-Z0-9.$]*)/g,function(b,d){var e=c.eval(a,d);if("string"==typeof e)e='"'+e+'"';else{if(void 0==e)return"undefined";if("object"==typeof e)return!!e}return e.toString()});try{e=g(b)}catch(i){this.throwError('criteria error :"'+d+'" real value="'+b+'"')}return e},invoke:function(a,b){var d=/\((.*)\)/.exec(b);if(d&&d[1])for(var e=d[1].split(","),f=[],g=0;g<e.length;g++){var h=e[g];isNaN(+h)?"'"==h.charAt(0)&&"'"==h.charAt(h.length-1)?f.push(h.slice(1,-1)):/^\$\^?[_a-zA-Z0-9.$]*$/.test(h)?f.push(this.eval(a,h.slice(1))):this.throwError('unrecognized arguments "'+h+'" for invoke "'+b+'"',this):f.push(+h)}else f=[];g=b.indexOf("("),-1!=g&&(b=b.slice(0,g));var i=k[b]||this.eval(a,b);return i&&i.apply(a,f)},eval:function(a,b){var d,c=0,e=a;if(!b)return a;b=b.replace(/\$([^.]*)/g,function(a,b){return""==b?e:e[b]}),"^"==b.charAt(0)&&(e=this.scope,b=b.slice(1)),"."==b.charAt(0)&&(b=b.slice(1));for(var f=b.split(".");(d=f[c++])&&(e=e[d],"object"==typeof e););if(void 0==e)for(var g=this.curBlock||this.Block();g=g.parent;){for(e=g.blockScope,c=0;(d=f[c++])&&(e=e[d],"object"==typeof e););if(void 0!=e)break}return e},resolveBlock:function(a,b){a=a||this.Block(),b=b||a.blockScope,this.curBlock=a,a.blockScope=b;var c={i:0,result:""};for(c.i=0;c.i<a.result.length;c.i++){var d=a.result[c.i];"object"==typeof d?(d.idx>=0&&(this.idx=d.idx),n.handlers[d.type].call(this,d,b,c)):c.result+=d}return c.result},throwError:function(a){a="Parse Error:"+a;var b=this.html.substring(this.idx-40,this.idx)+'"'+this.html.charAt(this.idx)+'"'+this.html.substring(this.idx+1,this.idx+40);console&&"function"==typeof console.log&&console.log(a+"\n at: "+b),this.errorMsg=a+"\n at: "+b,this.error=!0}};var j={flag:!0,after:function(a){if(this.flag){var b=a.html.charAt(a.idx+1);"\n"==b&&a.idx++,"\r"==b&&(a.idx++,"\n"==a.html.charAt(a.idx+1)&&a.idx++)}},before:function(a){if(this.flag){var b=a.Block().result,c=b[b.length-1];if("string"==typeof c){for(var d=c.length,e=d;" "==c.charAt(--d);)e--;b[b.length-1]=c.slice(0,e)}}}},k={add:function(a,b){this[a]=b}},l={mode:"",addMode:function(a,b){this.mode+=a,this.filter=new RegExp("["+this.mode+"]"),this.handlers[a]=b},handlers:{}},n={handlers:{},add:function(a,b){this.handlers[a]=b}};n.add("method",function(a,b,c){c.result+=this.invoke(b,a.exp)}),n.add("expression",function(a,b,c){var d=this,e=a.exp.replace(/\$(\^?[_a-zA-Z0-9.$]*)/g,function(a,c){var e=d.eval(b,c);if("string"==typeof e)e='"'+e+'"';else{if(void 0==e)return"undefined";if("object"==typeof e)return!!e}return e.toString()});try{var f=g(e)}catch(h){this.throwError('%express error:"'+a.exp+'" real value="'+e+'"')}c.result+=f}),n.add("property",function(a,b,c){var d=!1;-1!=a.exp.indexOf("~")&&(d=!0,a.exp=a.exp.replace("~",""));var e=this.eval(b,a.exp);e&&d&&(e=e.replace("<","<").replace(">",">")),c.result+=e}),n.add("branch",function(a,b,c){var d=this.test(b,a.exp);d?a.flag=!0:(a.flag=!1,c.i=(a.nextIndex||a.endIndex)-1)}),n.add("else",function(a,b,c){1==a.If.flag?c.i=a.If.endIndex-1:a.flag||void 0==a.flag&&this.test(b,a.exp)?a.If.flag=!0:c.i=a.nextIndex-1}),n.add("block",function(a,b,c){a.blockScope=b,c.result+=s[a.blockName].onClose.call(a.blockName,this,a)});var o={onEndBlock:function(a,b){if(!b.skipMode)if(b.deferEval)b.saveVar(a,"method");else{var c=b.Block();c.result.push(b.invoke(c.blockScope,a))}}},p={onEndBlock:function(a,b){if(!b.skipMode)if(b.deferEval)b.saveVar(a,"property");else{var c=!1;block=b.Block(),-1!=a.indexOf("~")&&(c=!0,a=a.replace("~",""));var d=b.eval(block.blockScope,a);"object"==typeof d&&(d=d.toString()),c&&d&&(d=d.replace("<","<").replace(">",">")),block.result.push(d)}},onInBlock:function(c,d){d.skipMode||("~"==c&&0==d.blockContent[d.blockType].length||/[$^_a-zA-Z0-9.]/.test(c)?d.blockContent[d.blockType]+=c:(d.text+=b.delimeter.begin+d.blockType+d.blockContent[d.blockType]+c,d.state=a.OUT_BLOCK))}},q={onStartBlock:j.before,onEndBlock:function(a,b){var c=a.indexOf(" ");-1==c&&(c=a.length);var d=a.substring(0,c),e=a.slice(c+1).replace(/^ *| *$/g,""),f=s[d];f?b.skipMode?f.onSkipBegin&&f.onSkipBegin(b,e):f.onBegin&&f.onBegin.call(d,b,e):console&&console.log&&console.log("Warning:unidentified group name:"+a),j.after(b)}},r={onStartBlock:function(a){var b=a.Block().result,c=b[b.length-1];if("string"==typeof c){for(var d=c.length,e=d;" "==c.charAt(--d);)e--;b[b.length-1]=c.slice(0,e)}},onEndBlock:function(a,b){var c=s[a];if(c)if(b.skipMode)c.onSkipClose&&c.onSkipClose(b);else if(c.onClose){var d=c.onClose.call(a,b,b.Block());void 0!=d&&b.Block().result.push(d)}else console&&console.log&&console.log('Warning:group "'+a+'" need a onClose Handler');j.after(b)}};l.addMode("$",p),l.addMode("#",q),l.addMode("/",r),l.addMode("@",o),l.addMode("%",{onEndBlock:function(a,b){if(!b.skipMode)if(b.deferEval)b.saveVar(a,"expression");else{var c=b.Block(),d=a.replace(/\$(\^?[_a-zA-Z0-9.$]*)/g,function(a,d){var e=b.eval(c.blockScope,d);if("string"==typeof e)e='"'+e+'"';else{if(void 0==e)return"undefined";if("object"==typeof e)return!!e}return e.toString()});try{var e=g(d)}catch(f){b.throwError('%express error:"'+a+'" real value="'+d+'"')}b.Block().result.push(e)}}}),l.addMode("<",{onEndBlock:function(a,b){b.startPartials(a)}}),l.addMode("!",{onEndBlock:function(){}});var s={register:function(a,b){this[a]=b}},t={onBegin:function(a,b){a.newBlock(this.valueOf(),b,a.Block().blockScope),a.deferEval=!0},onClose:function(a,b){if(b.blockName==this){if(!a.deferEval)return u[this](a,b);a.closeBlock()}else a.throwError("unmatched {/"+this+"}",a)}},u={register:function(a,b){this[a]=b,s.register(a,t)}};u.register("each",function(a,b){var c="",d=/^\$(\^?[a-zA-Z0-9_.$]*)(?:\(\$([a-zA-Z0-9_]+)=>\$([a-zA-Z0-9_]+)\))?$/.exec(b.blockArgs);if(d){if(b.blockScope=a.eval(b.blockScope,d[1]),d[2])var e=d[2],f=d[3];var g=b.blockScope;if(g)if(g.length>0)for(var h=0;h<g.length;h++){if(e){var i={};i[e]=h,i[f]=g[h]}else i=g[h];c+=a.resolveBlock(b,i)}else for(var j in g)e?(i={},i[e]=j,i[f]=g[j]):i=g[j],c+=a.resolveBlock(b,i)}else a.throwError("can't resolve arguments of {each}:\""+b.blockArgs+'"');return c}),s.register("if",{onSkipBegin:function(a){a.Block().branchStack.push(null)},onBegin:function(a,b){if(a.deferEval)a.saveBranch(b);else{var c=a.test(a.Block().blockScope,b);c?(a.skipMode=!1,a.Block().branchStack.push({flag:!0})):(a.skipMode=!0,a.Block().branchStack.push({flag:!1}))}},onSkipClose:function(a){var b=a.Block().branchStack.pop();b&&(a.skipMode=!1)},onClose:function(a){if(a.deferEval)a.endBranch(),a.skipMode=!1;else{var b=a.Block().branchStack.pop();b&&(a.skipMode=!1),void 0==b&&a.throwError("unmatched /if",a)}}}),s.register("else",{onSkipBegin:function(a){var b=a.Block().branchStack,c=b[b.length-1];c&&(a.skipMode=c.flag)},onBegin:function(a){if(a.deferEval)a.setElse("",!0);else{var c=a.Block().branchStack,d=c[c.length-1];0==c.length&&a.throwError("else need a if",a),d&&(a.skipMode=d.flag)}}}),s.register("elseif",{onSkipBegin:function(a,b){var c=a.Block().branchStack,d=c[c.length-1];if(d)if(d.flag)a.skipMode=!0;else{var e=a.test(a.Block().blockScope,b);e&&(a.skipMode=!1,d.flag=!0)}},onBegin:function(a,b){if(a.deferEval)a.setElse(b);else{var c=a.Block().branchStack;0==c.length&&a.throwError("elseif need a if",a);var d=c[c.length-1];if(d)if(d.flag)a.skipMode=!0;else{var e=a.test(a.Block().blockScope,b);e&&(a.skipMode=!1,d.flag=!0)}}}});var x={};x.Context=i,x.GroupManager=s,x.BlockMode=l,x.render=v,x.renderSimple=w,x.tmpl=function(a){var b=document.getElementById(a);return b&&b.innerHTML},x.setDelimeter=function(a,c){b.delimeter=new f(a,c)},x.setFormat=function(a){j.flag=a},x.config=function(a){this["set"+a.charAt(0).toUpperCase()+a.slice(1)].apply(this,Array.prototype.slice.call(arguments,1))},x.register=function(a,b){u.register(a,b)},x.register("repeat",function(a,b){var c=b.blockArgs,d="";"$"==c.charAt(0)&&(c=a.eval(b.blockScope,c.slice(1)));var e=+c;if(isNaN(e))a.throwError('repeat need a number as its argument! error param:"'+c+'"');else for(var f=0;e>f;f++)d+=a.resolveBlock(b,{INDEX:f,SEQ:f+1});return d}),"object"==typeof exports&&exports?module.exports=x:"function"==typeof define?define.cmd?define(function(a,b,c){c.exports=x}):define.amd?define(x):window.Whisker=x:window.Whisker=x}();
//好无奈 13K了。。。