drT.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // drT.js
  2. // 2022/09/30 write by hjdhnx
  3. // Licensed under the MIT license.
  4. (function () {
  5. "use strict";
  6. var drT = {
  7. name: "drT",
  8. version: "1.0.0",
  9. templateSettings: {
  10. evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
  11. interpolate: /\{\{([\s\S]+?)\}\}/g, // 变量渲染
  12. encode: /\{\{@([\s\S]+?)\}\}/g, // 变量自动url编码
  13. use: /\{\{#([\s\S]+?)\}\}/g,
  14. useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
  15. define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
  16. defineParams:/^\s*([\w$]+):([\s\S]+)/,
  17. conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g, // ? if ?? else if ?? else
  18. iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
  19. varname: "fl",
  20. strip: true,
  21. append: true,
  22. selfcontained: false,
  23. doNotSkipEncoded: false
  24. },
  25. template: undefined, //fn, compile template
  26. compile: undefined, //fn, for express
  27. log: true
  28. }, _globals;
  29. drT.encodeHTMLSource = function(doNotSkipEncoded) {
  30. var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': "&#34;", "'": "&#39;", "/": "&#47;" },
  31. matchHTML = doNotSkipEncoded ? /[&<>"'\/]/g : /&(?!#?\w+;)|<|>|"|'|\//g;
  32. return function(code) {
  33. return code ? code.toString().replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : "";
  34. };
  35. };
  36. _globals = (function(){ return this || (0,eval)("this"); }());
  37. /* istanbul ignore else */
  38. if (typeof module !== "undefined" && module.exports) {
  39. module.exports = drT;
  40. } else if (typeof define === "function" && define.amd) {
  41. define(function(){return drT;});
  42. } else {
  43. _globals.drT = drT;
  44. }
  45. var startend = {
  46. append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
  47. split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML(" }
  48. }, skip = /$^/;
  49. function resolveDefs(c, block, def) {
  50. return ((typeof block === "string") ? block : block.toString())
  51. .replace(c.define || skip, function(m, code, assign, value) {
  52. if (code.indexOf("def.") === 0) {
  53. code = code.substring(4);
  54. }
  55. if (!(code in def)) {
  56. if (assign === ":") {
  57. if (c.defineParams) value.replace(c.defineParams, function(m, param, v) {
  58. def[code] = {arg: param, text: v};
  59. });
  60. if (!(code in def)) def[code]= value;
  61. } else {
  62. new Function("def", "def['"+code+"']=" + value)(def);
  63. }
  64. }
  65. return "";
  66. })
  67. .replace(c.use || skip, function(m, code) {
  68. if (c.useParams) code = code.replace(c.useParams, function(m, s, d, param) {
  69. if (def[d] && def[d].arg && param) {
  70. var rw = (d+":"+param).replace(/'|\\/g, "_");
  71. def.__exp = def.__exp || {};
  72. def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2");
  73. return s + "def.__exp['"+rw+"']";
  74. }
  75. });
  76. var v = new Function("def", "return " + code)(def);
  77. return v ? resolveDefs(c, v, def) : v;
  78. });
  79. }
  80. function unescape(code) {
  81. return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
  82. }
  83. drT.template = function(tmpl, c, def) {
  84. c = c || drT.templateSettings;
  85. var cse = c.append ? startend.append : startend.split, needhtmlencode, sid = 0, indv,
  86. str = (c.use || c.define) ? resolveDefs(c, tmpl, def || {}) : tmpl;
  87. // console.log(str);
  88. let beforeCode = '';
  89. if(str.match(c.interpolate || skip)){
  90. let inter_codes = str.match(c.interpolate || skip);
  91. let inter_dict = {};
  92. inter_codes.forEach(item=>{
  93. item.replace(c.interpolate || skip,function (m,code) {
  94. let varname = code.split('.')[0];
  95. if(!inter_dict.hasOwnProperty(varname)){
  96. let beginCode = `if(typeof(${varname})==='undefined'){${varname}={}}`;
  97. inter_dict[varname] = beginCode;
  98. }if(!inter_dict.hasOwnProperty(code)){
  99. let beginCode = `if(typeof(${code})==='undefined'){${code}=''};`;
  100. inter_dict[code] = beginCode;
  101. }
  102. });
  103. });
  104. let beginCode = Object.values(inter_dict).join('\n');
  105. // console.log(beginCode);
  106. beforeCode += beginCode;
  107. }
  108. str = beforeCode+("var out='" + (c.strip ? str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g," ")
  109. .replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,""): str)
  110. .replace(/'|\\/g, "\\$&")
  111. .replace(c.encode || skip, function(m, code) {
  112. needhtmlencode = true;
  113. return cse.startencode + unescape(code) + cse.end;
  114. })
  115. .replace(c.interpolate || skip, function(m, code) {
  116. let varname = code.split('.')[0];
  117. // console.log(varname === code);
  118. // console.log(`varname:${varname},code:${code}`);
  119. if(varname === code){
  120. let res = cse.start + `JSON.stringify(${unescape(code)})` + cse.end;
  121. // console.log(res);
  122. return res
  123. }
  124. return cse.start + unescape(code) + cse.end;
  125. })
  126. .replace(c.conditional || skip, function(m, elsecase, code) {
  127. return elsecase ?
  128. (code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
  129. (code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
  130. })
  131. .replace(c.iterate || skip, function(m, iterate, vname, iname) {
  132. if (!iterate) return "';} } out+='";
  133. sid+=1; indv=iname || "i"+sid; iterate=unescape(iterate);
  134. return "';var arr"+sid+"="+iterate+";if(arr"+sid+"){var "+vname+","+indv+"=-1,l"+sid+"=arr"+sid+".length-1;while("+indv+"<l"+sid+"){"
  135. +vname+"=arr"+sid+"["+indv+"+=1];out+='";
  136. })
  137. .replace(c.evaluate || skip, function(m, code) {
  138. return "';" + unescape(code) + "out+='";
  139. })
  140. + "';return out;")
  141. .replace(/\n/g, "\\n").replace(/\t/g, '\\t').replace(/\r/g, "\\r")
  142. .replace(/(\s|;|\}|^|\{)out\+='';/g, '$1').replace(/\+''/g, "");
  143. //.replace(/(\s|;|\}|^|\{)out\+=''\+/g,'$1out+=');
  144. if (needhtmlencode) {
  145. // console.log('需要编码');
  146. // console.log(c.doNotSkipEncoded);
  147. if (!c.selfcontained && _globals && !_globals._encodeHTML) _globals._encodeHTML = drT.encodeHTMLSource(c.doNotSkipEncoded);
  148. str = "var encodeHTML = typeof _encodeHTML !== 'undefined' ? _encodeHTML : ("
  149. + drT.encodeHTMLSource.toString() + "(" + (c.doNotSkipEncoded || '') + "));"
  150. + str;
  151. // console.log(str);
  152. }else{
  153. // console.log('不需要编码');
  154. }
  155. // console.log(c.varname);
  156. // console.log(str);
  157. try {
  158. return new Function(c.varname, str);
  159. } catch (e) {
  160. /* istanbul ignore else */
  161. // console.log(e.message);
  162. if (typeof console !== "undefined") console.log("Could not create a template function: " + str);
  163. throw e;
  164. }
  165. };
  166. drT.compile = function(tmpl, def) {
  167. return drT.template(tmpl, null, def);
  168. };
  169. drT.renderText = function (tmpl,dict,varname){
  170. varname = varname||'';
  171. if(varname){
  172. drT.templateSettings.varname = varname;
  173. }
  174. dict = dict||{};
  175. return drT.compile(tmpl)(dict);
  176. };
  177. }());