-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjTDAL.min.js
1 lines (1 loc) · 7.48 KB
/
jTDAL.min.js
1
"use strict";var jTDAL;!function(t){const e="(?:[\\w\\-\\/]*[\\w](?:[\\s]*\\|[\\s]*[\\w\\-\\/]*[\\w])*)",r="STRING:(?:[^;](?:(?!<=;);)?)+",n="(?:(?:!)?[\\w\\-\\/]*[\\w](?:[\\s]*\\|[\\s]*[\\w\\-\\/]*[\\w])*)",s="(?:"+r+"|(?:"+n+")(?:[\\s*]\\|[\\s*]"+r+")?)",l={tagWithTDAL:new RegExp("<((?:\\w+:)?\\w+)(\\s+[^<>]+?)??\\s+data-tdal-(?:"+["condition","repeat","content","replace","attributes","omittag"].join("|")+")=(['\"])(.*?)\\3(\\s+[^<>]+?)??\\s*(/)?>","i"),tagWithAttribute:new RegExp("<((?:w+:)?w+)(s+[^<>]+?)??s+%s=(['\"])(.*?)\\3(s+[^<>]+?)??s*(/)?>","i"),tagAttributes:new RegExp("(?<=\\s)((?:[\\w\\-]+:)?[\\w\\-]+)=(?:(['\"])(.*?)\\2|([^>\\s'\"]+))","gi"),pathString:new RegExp("(?:{("+e+")}|{\\?("+n+")}(.*?){\\?\\2})"),pathInString:new RegExp("{("+e+")}","g"),conditionInString:new RegExp("{\\?("+n+")}(.*?){\\?\\1}","g"),condition:new RegExp("^[\\s]*("+s+")[\\s]*$"),repeat:new RegExp("^[\\s]*([\\w\\-]+?)[\\s]+("+e+")[\\s]*$"),content:new RegExp("^[\\s]*(?:(text|structure)[\\s]+)?("+s+")[\\s]*$"),attributes:new RegExp("[\\s]*(?:(?:([\\w\\-]+?)[\\s]+("+s+")[\\s]*)(?:;;[\\s]*|$))","g"),attributesTDAL:new RegExp("\\s*(data-tdal-[\\w\\-]+)=(?:(['\"])(.*?)\\2|([^>\\s'\"]+))","gi")},i=["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"];function a(t){let e='""',r=null;for(;null!=(r=l.pathString.exec(t));)if(0<r.index&&(e+="+"+JSON.stringify(String(t.substring(0,r.index)))),t=t.substring(r.index+r[0].length),r[1])e+="+(a("+u(r[1],!1)+')&&("string"===typeof t[t[0]]||("number"===typeof t[t[0]]&&!isNaN(t[t[0]])))?t[t[0]]:"")';else if(r[2]){const t=u(r[2],!0);"true"===t?e+="+"+a(r[3]):"false"!==t&&(e+="+(true==="+t+'?""+'+a(r[3])+':"")')}return 0<t.length&&(e+="+"+JSON.stringify(String(t))),e}function u(t,e=!1){let r="";if(t){let n=0;t:{const n=t.split("|"),s=n.length;for(let t=0;t<s;++t){0!=t&&(r+="||");let s=n[t].replace(/^\s+/,"");if(s.startsWith("STRING:")){r+="("+a(s.substring(7))+")";break t}{s=s.replace(/\s+$/,"");const t="!"===s[0],n=e||t,l=(t?s.substring(1):s).split("/");if(0<l.length&&0<l[0].length)switch(l[0]){case"FALSE":r+=t?"true":"false";break t;case"TRUE":r+=t?"false":"true";break t;case"REPEAT":3==l.length&&(r+=(n?(t?"!":"")+"b(":"")+'c(r,"'+l.join("/")+'")'+(n?")":""));break;case"GLOBAL":1<l.length&&0<l[1].length&&(r+=(n?(t?"!":"")+"b(":"")+'c(d,"'+l.slice(1).join("/")+'")'+(n?")":""));break;default:r+=n?(t?"!(":"")+'(a(c(r,"'+l.join("/")+'"))&&false!==b(t[t[0]]))||(a(c(d,"'+l.join("/")+'"))&&false!==b(t[t[0]]))'+(t?")":""):'((a(c(r,"'+l.join("/")+'"))||a(c(d,"'+l.join("/")+'")))?t[t[0]]:false)'}}}}for(let t=0;t<n;t++)r+=")"}else r="false";return r}function o(t){let e="",r=null;for(;null!==(r=l.tagWithTDAL.exec(t));){0<r.index&&(e+="+"+JSON.stringify(String(t.substring(0,r.index)))),t=t.substring(r.index+r[0].length);let n,s={};for(;null!==(n=l.tagAttributes.exec(r[0]));)s[n[1]]=n;let a=["",r[0],"","","","","",""];a[1]=a[1].replace(l.attributesTDAL,"");let f=!!r[6]||i.includes(r[1].toLowerCase());if(!f){let e=[];const s=new RegExp("<(\\/)?"+r[1]+"[^<>]*(?<!\\/)>","gi");let l=1;for(;void 0===e[0]&&null!==(n=s.exec(t));)n[1]?l--:l++,0==l&&(e=[n.index,n[0].length]);void 0===e[0]?f=!0:(a[4]+=o(t.substring(0,e[0])),a[6]+=t.substring(e[0],e[0]+e[1]),t=t.substring(e[0]+e[1]))}t:{if(s["data-tdal-condition"]&&l.condition.exec(s["data-tdal-condition"][3])){let t=u(s["data-tdal-condition"][3],!0);if("false"===t)break t;"true"!==t&&(a[0]+="+(true==="+t+'?""',a[7]=':"")'+a[7])}let t;if(s["data-tdal-repeat"]&&(t=l.repeat.exec(s["data-tdal-repeat"][3]))){let e=u(t[2],!1);if("false"==e||'""'==e||"true"==e)break t;a[0]+="+(",a[0]+="((",a[0]+="a("+e+")&&",a[0]+="(!Array.isArray(t[t[0]])||(t[t[0]]=Object.assign({},t[t[0]])))&&",a[0]+='("object"===typeof t[t[0]]&&null!==t[t[0]]&&Object.keys(t[t[0]]).length)',a[0]+=")?((t[++t[0]]=1)&&t[0]++):((t[0]+=2)&&false))",a[0]+="?",a[0]+="Object.keys(t[t[0]-2]).reduce((o,e)=>{",a[0]+='r["'+t[1]+'"]=t[t[0]-2][e];',a[0]+='r["REPEAT"]["'+t[1]+'"]={};',a[0]+='r["REPEAT"]["'+t[1]+'"]["index"]=e;',a[0]+='r["REPEAT"]["'+t[1]+'"]["number"]=t[t[0]-1]++;',a[0]+='r["REPEAT"]["'+t[1]+'"]["even"]=0==(r["REPEAT"]["'+t[1]+'"]["number"]%2);',a[0]+='r["REPEAT"]["'+t[1]+'"]["odd"]=1==(r["REPEAT"]["'+t[1]+'"]["number"]%2);',a[0]+='r["REPEAT"]["'+t[1]+'"]["first"]=1==r["REPEAT"]["'+t[1]+'"]["number"];',a[0]+='r["REPEAT"]["'+t[1]+'"]["length"]=Object.keys(t[t[0]-2]);',a[0]+='r["REPEAT"]["'+t[1]+'"]["last"]=r["REPEAT"]["'+t[1]+'"]["length"]==r["REPEAT"]["'+t[1]+'"]["number"];',a[0]+="return o",a[7]=';},""):"")+((t[0]-=2)&&(delete r["REPEAT"]["'+t[1]+'"])&&delete(r["'+t[1]+'"])?"":"")'+a[7]}if(s["data-tdal-content"]&&(t=l.content.exec(s["data-tdal-content"][3]))){let e=u(t[2],!1);if("false"==e)a[4]="";else if("true"!=e){let r=["",""];"structure"!=t[1]&&(r[0]="String(",r[1]=').replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """)'),a[3]+="+(a("+e+')&&("string"===typeof t[t[0]]||("number"===typeof t[t[0]]&&!isNaN(t[t[0]])))?'+r[0]+"t[t[0]]"+r[1]+':(true!==t[t[0]]?"":""',a[5]+="))"}}else if(s["data-tdal-replace"]&&(t=l.content.exec(s["data-tdal-replace"][3]))){let e=u(t[2],!1);if("false"==e)a[1]="",a[4]="",a[6]="";else if("true"!=e){let r=["",""];"structure"!=t[1]&&(r[0]="String(",r[1]=').replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """)'),a[0]+="+(a("+e+')&&("string"===typeof t[t[0]]||("number"===typeof t[t[0]]&&!isNaN(t[t[0]])))?'+r[0]+"t[t[0]]"+r[1]+':(true!==t[t[0]]?"":""',a[7]="))"+a[7]}}if(s["data-tdal-attributes"]&&(t=l.attributes.exec(s["data-tdal-attributes"][3])))for(;null!==t;){let e=u(t[2],!1);"false"===e?void 0!==s[t[1]]&&(a[1]=a[1].replace(new RegExp("\\s*"+t[1]+"(?:=(['\"]).*?\\1)?"),"")):"true"!==e&&(a[2]+="+(a("+e+')&&("string"===typeof t[t[0]]||("number"===typeof t[t[0]]&&!isNaN(t[t[0]])))?" '+t[1]+'=\\""+t[t[0]]+"\\"":(true!==t[t[0]]?"":"'+t[1]+'"',void 0!==s[t[1]]&&(a[1]=a[1].replace(new RegExp("\\s*"+t[1]+"(?:=(['\"]).*?\\1)?"),""),a[2]+=void 0!==s[t[1]][3]&&""!=s[t[1]][3]?'+"="+'+JSON.stringify(String(s[t[1]][2]+s[t[1]][3]+s[t[1]][2])):""),a[2]+="))"),t=l.attributes.exec(s["data-tdal-attributes"][3])}if(s["data-tdal-omittag"]&&l.condition.exec(s["data-tdal-omittag"][3])){let t=u(s["data-tdal-omittag"][3],!0);"true"==t?(a[1]="",a[6]=""):"false"!=t&&(a[0]+="+("+t+'?"":""',a[3]=")"+a[3],a[5]+="+("+t+'?"":""',a[7]=")"+a[7])}}a[1]=a[1].replace(/\s*\/?>$/,""),!f||""==a[4]&&""==a[3]&&""==a[5]||(a[6]="</"+r[1]+">",f=!1),e+=a[0]+"+"+JSON.stringify(String(a[1]))+a[2]+(""!=a[1]?'+"'+(f?"/":"")+'>"':"")+a[3]+a[4]+a[5]+"+"+JSON.stringify(String(a[6]))+a[7]}return e+="+"+JSON.stringify(String(t)),e}function f(t,e=!0,r=!0){let n=o(r?t.replace(/<!--.*?-->/gs,""):t),s='let r={"REPEAT":{}},t=[1];';s+="const ",s+="a=(e)=>{",s+="t[t[0]]=e;",s+="return false!==t[t[0]]",s+="}",s+=",c=(a,b)=>{",s+='let z=!1,y=b.split("/"),x,w;',s+="if(0<y.length&&0<y[0].length)",s+="for(z=a,x=0;x<y.length&&1!==z;x++)",s+='z="object"===typeof z&&null!==z&&void 0!==(w="function"===typeof z[y[x]]?z[y[x]](d,r):z[y[x]])&&w;',s+="return z",s+="}",s+=",b=(v)=>{",s+='return "object"===typeof v?null!==v&&0<Object.keys(v).length:(Array.isArray(v)?0<v.length:void 0!==typeof v&&false!==v&&""!==v)',s+="}",s+=";",s+="return "+(e?"(":"")+'""'+n+(e?").trim()":""),n="";do{n=s,s=s.replace(/(?<!\\)"\+"/g,"").replace('(true!==t[t[0]]?"":"")','""')}while(n!=s);return s}t.CompileToFunction=function(t,e=!0,r=!0){return new Function("d",f(t,e,r))},t.CompileToString=function(t,e=!0,r=!0){return"function(d){"+f(t,e,r)+"}"}}(jTDAL||(jTDAL={}));export default{CompileToFunction:jTDAL.CompileToFunction,CompileToString:jTDAL.CompileToString};