drT.js 8.4 KB

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