1 Star 0 Fork 26

bobo/fasty

forked from Michael Yang/fasty 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fasty.min.js 8.00 KB
一键复制 编辑 原始数据 按行查看 历史
Michael Yang 提交于 2022-08-12 11:28 . v1.0.5 released!
/*
* fasty 1.0.5
* The fastest javascript templating engine.
* https://github.com/yangfuhai/fasty
*
* Copyright 2022, Michael <[email protected]>
* Released under the MIT license.
*/
var Fasty=function(t){this.share=t&&t.share?t.share:null,this.shareDataFirst=!(!t||void 0===t.shareDataFirst)&&t.shareDataFirst,this.safelyAccess=!t||void 0===t.safelyAccess||t.safelyAccess,this.debugMode=!(!t||void 0===t.debugMode)&&t.debugMode,this.windowObjectEnable=!(!t||void 0===t.windowObjectEnable)&&t.windowObjectEnable,this.windowObjects=t&&void 0!==t.windowObjects?t.windowObjects:null,this.funs={},this.rootParaName=t&&"string"==typeof t.rootParaName?t.rootParaName:"$DATA"};Fasty.prototype={Tok:function(t){this.type=t,this.tag="",this.text="",this.addTokText=function(t){this.text+=t},this.isEmpty=function(){return""===this.text},this.isText=function(){return 0===t},this.isOutput=function(){return 1===t||2===t||3===t},this.isEscape=function(){return 2===t},this.isUnEscape=function(){return 3===t},this.arrange=function(t){var e,i;this.isText()||(this.text=this.text.trim(),this.isOutput()&&t&&t.safelyAccess&&(this.text=this.text.replace(/\?\./g,"__safe.").replace(/\?\(/g,"__safe(")),t=this.text.indexOf(" "),e=this.text.indexOf("("),i=this.text.indexOf("."),this.tag=t===i&&t===e&&-1===t?this.text:this.text.substring(0,this.calcMin([t,e,i])),"else"===this.tag&&(t=this.getTagAfter().trim()).startsWith("if")&&(this.tag="elseif",this.text="elseif "+t.substring(2)))},this.getTagAfter=function(){return this.tag?this.text.substring(this.tag.length):this.text},this.calcMin=function(t){var e,i=t[0];for(e of t)(i<=0||0<e&&e<i)&&(i=e);return i}},render:function(e,t){var i=this.funs[e];if(!i){var r=this._parseTokens(e);if(!r||0===r.length)return;try{var s=this._compileTokens(r);this.debugMode&&console.log("method body >>>>",s),i=new Function(this.rootParaName,s),this.funs[e]=i}catch(t){return console.error("template error >>>",t),console.error("template >>>",e),""}}if((t=t||{}).$escape=function(t){return t&&t.toString().replace(/\&/g,"&amp;").replace(/\</g,"&lt;").replace(/\>/g,"&gt;").replace(/\'/g,"&#39;").replace(/\"/g,"&quot;")},t.$unescape=function(t){return t&&t.toString().replace(/\&amp;/g,"&").replace(/\&lt;/g,"<").replace(/\&gt;/g,">").replace(/\&#39;/g,"'").replace(/\&quot;/g,'"')},this.share)for(var n of Object.keys(this.share))!this.shareDataFirst&&t[n]||(t[n]=this.share[n]);return i(this._proxy(t,!1))},_proxy:function(t,e,i){var r=this;return this.safelyAccess?new Proxy(t,{withSafe:e,pattr:i,get:function(t,e){return"symbol"==typeof e?()=>"":e.endsWith("__safe")?(i=t[e=e.substring(0,e.length-6)])?r._proxy(i,!0,e):r._proxy(()=>{},!0,e):null!=(i=t[e])?i:this.withSafe?"":void 0;var i},apply:function(t,e,i){return Reflect.has(e,t.name)?t(i):r._proxy({},!0,this.pattr)}}):t},_parseTokens:function(t){if(t&&0!==t.trim().length){for(var e,i,r=[],s=0,n=!1;!this._isEnd(s,t);){var o=t.charAt(s);"\n"===o||"\t"===o?s++:!e||e.isText()||n||'"'!==o&&"'"!==o?e&&n&&o===i?(n=!1,e.addTokText(o),s++):n||"{"!==o||"{"!==t.charAt(s+1)?n||"}"!==o||"}"!==t.charAt(s+1)?((e=e||new this.Tok(0)).addTokText(o),s++):(s+=2,e&&(r.push(e),e.arrange(this),e=null)):(e&&(r.push(e),e.arrange(this),e=null),e="~"===t.charAt(s+2)?(s+=3,new this.Tok(9)):"!"===t.charAt(s+2)?(s+=3,new this.Tok(2)):"@"===t.charAt(s+2)?(s+=3,new this.Tok(3)):(s+=2,new this.Tok(1))):(n=!0,e.addTokText(i=o),s++)}return e&&(e.arrange(this),r.push(e)),r}},_isEnd:function(t,e){return t>=e.length},_compileComparison:function(t,e){t=t.getTagAfter().trim(),t=t.substring(1,t.length-1);return this._compileComparisonString(t,e)},_compileComparisonString:function(t,e){if(!t)return"";var i=!1,r=(0===(t=t.trim()).indexOf("(")&&t.lastIndexOf(")")===t.length-1&&(t=t.substring(1,t.length-1),i=!0),t.indexOf("&&")),s=t.indexOf("||");return r=-1!==r&&(r<s||-1===s)?this._getComparison(t.substring(0,r),e).toString()+" && "+this._compileComparisonString(t.trim().substring(r+2),e):-1!==s&&(s<r||-1===r)?this._getComparison(t.substring(0,s),e).toString()+" || "+this._compileComparisonString(t.trim().substring(s+2),e):this._getComparison(t,e).toString(),i?"( "+r+" )":r},_compileVars:function(t,e,i){var r,s,n=[];for(r of t.getTagAfter().split("="))r=r.trim(),this._isArrayOrString(r)||-1===(s=r.lastIndexOf(","))?n.push(r):(n.push(r.substring(0,s)),n.push(r.substring(s+1)));if(0===n.length||n.length%2!=0)throw new Error("Variable definition error: "+t.text);var o="";for(let t=0;t<n.length;t+=2){var a=n[t],h=this._compileObjectOrMethodInvoke(e,n[t+1]);this._pushContextVars(e,i,a),o+=a+"="+h,t===n.length-2?o+=";":o+=","}return o},_isArrayOrString:function(t){return 0===t.indexOf("[")&&t.indexOf("]")===t.length-1||0===t.indexOf('"')&&t.indexOf('"')===t.length-1||0===t.indexOf("'")&&t.indexOf("'")===t.length-1},_compileTokens:function(t){var e,i='var ret = "";',r={},s=0;for(e of t)if(e.isEmpty())i+="ret += ' ';";else if(e.isText())i+="ret += '"+e.text.replace(/\'/g,"\\'").replace(/\"/g,'\\"')+"';";else if(e.isOutput())e.isEscape()?i+="ret += ("+this.rootParaName+".$escape("+this._compileObjectOrMethodInvoke(r,e.text)+') ?? "");':e.isUnEscape()?i+="ret += ("+this.rootParaName+".$unescape("+this._compileObjectOrMethodInvoke(r,e.text)+') ?? "");':i+="ret += ("+this._compileObjectOrMethodInvoke(r,e.text)+') ?? "";';else switch(e.tag){case"for":var n=e.text.substring(3).trim(),o=(n=n.substring(1,n.length-1).trim().replace(/\s+/g," ")).split(" ");if(3===o.length&&"of"===o[1]||"in"===o[1]){var a=o[0],h=this._compileObjectOrMethodInvoke(r,o[2]);this._pushContextVars(r,++s,a),i+="for ("+a+" "+o[1]+" "+h+"){";break}if(4===o.length&&"of"===o[2]||"in"===o[2]){a=o[1],h=this._compileObjectOrMethodInvoke(r,o[3]);this._pushContextVars(r,++s,a),i+="for ("+o[0]+" "+a+" "+o[2]+" "+h+"){";break}o=n.split(";");if(3!==o.length)throw Error("for loop is error: "+e.text);n=new this.Tok(9),n=(n.text=o[0],n.arrange(this),i=(i+="for (")+(n.tag+" "+this._compileVars(n,r,++s)),this._getComparison(o[1],r)),i=(i+=n.before+n.op+n.after+";")+(o[2]+"){");break;case"if":s++,i+="if("+this._compileComparison(e,r)+"){";break;case"else":i+="}else{";break;case"elseif":i+="}else if("+this._compileComparison(e,r)+"){";break;case"end":case"/if":case"/for":r[s--]=null,i+="}";break;case"var":case"let":case"const":i+=e.tag+" "+this._compileVars(e,r,s);break;default:i+=e.text+";"}return i+="return ret;"},_pushContextVars:function(t,e,i){if(t[e]||(t[e]=[]),Array.isArray(i))for(var r of i)t[e].push(r);else t[e].push(i)},_compileObjectOrMethodInvoke:function(i,t){function e(t,e){return{value:t,isOperator:e,append:function(t){this.value+=t},compile:function(t){return 0===this.value.trim().length||this.isOperator||t||o._inContextVars(i,this.value.trim())?this.value:o.rootParaName+'["'+this.value.trim()+'"]'}}}for(var r,s=[],n=0,o=this,a=e("",!1),h=!1;!this._isEnd(n,t);){var c=t.charAt(n);"\n"===c||"\t"===c?n++:(!h&&0<=["+","-","*","/","%","(",")","[","]",",","?",":",".","="].indexOf(c)?(s.push(a),s.push(e(c,!0)),a=e("",!1)):h||'"'!==c&&"'"!==c?h&&c===r?(s.push(a),a=e(c,!1),h=!1):a.append(c):(s.push(a),a=e(c,!1),r=c,h=!0),n++)}s.push(a);var f,u,l="";for(u of s){var p=f&&"."===f.value;l+=u.compile(p),f=u}return l},_inContextVars:function(t,e){if(!isNaN(e))return!0;if(0===e.indexOf('"')||0===e.indexOf("'"))return!0;if(0===e.indexOf("[")&&0<e.indexOf("]"))return!0;var i,r=e.indexOf(".");0<(r=(e=0<r?e.substring(0,r).trim():e).indexOf("["))&&e.indexOf("]")>r&&(e=e.substring(0,r).trim());for(i of Object.values(t))if(i&&i.includes(e))return!0;return e.endsWith("__safe")&&(e=e.substring(0,e.length-6)),-1<[this.rootParaName,"Object","Number","String","Boolean","Array","Math","Date","window"].indexOf(e)||!!this.windowObjectEnable&&(!!(this.windowObjects&&Array.isArray(this.windowObjects)&&-1<this.windowObjects.indexOf(e))||window&&window[e])},_getComparison:function(t,e){var i;for(i of["===","!==","==","!=",">=","<=",">","<"]){var r=t.indexOf(i);if(0<r)return{before:this._compileObjectOrMethodInvoke(e,t.substring(0,r)),op:i,after:this._compileObjectOrMethodInvoke(e,t.substring(r+i.length)),toString:function(){return this.before+this.op+this.after}}}return this._compileObjectOrMethodInvoke(e,t)}};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/baitongda/fasty.git
[email protected]:baitongda/fasty.git
baitongda
fasty
fasty
master

搜索帮助