Skip to content
This repository
Browse code

cleaning up

  • Loading branch information...
commit d99027d44e528b2a1ed507a757bc264983271f98 1 parent 3db3bdd
Laura Doktorova authored April 28, 2012
9  benchmarks/templating/doT.js
@@ -72,7 +72,7 @@
72 72
 	}
73 73
 
74 74
 	function unescape(code) {
75  
-		return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
  75
+		return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, ' ');
76 76
 	}
77 77
 
78 78
 	doT.template = function(tmpl, c, def) {
@@ -86,8 +86,7 @@
86 86
 		} else str = tmpl;
87 87
 
88 88
 		str = ("var out='" + ((c.strip) ? str.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g, ''): str)
89  
-			.replace(/\\/g, '\\\\')
90  
-			.replace(/'/g, "\\'")
  89
+			.replace(/'|\\/g, '\\$&')
91 90
 			.replace(c.interpolate || skip, function(m, code) {
92 91
 				return cse.start + unescape(code) + cse.end;
93 92
 			})
@@ -110,9 +109,7 @@
110 109
 				return "';" + unescape(code) + "out+='";
111 110
 			})
112 111
 			+ "';return out;")
113  
-			.replace(/\n/g, '\\n')
114  
-			.replace(/\t/g, '\\t')
115  
-			.replace(/\r/g, '\\r')
  112
+			.replace(/[\r\t\n]/g, '\\$&')
116 113
 			.split("out+='';").join('')
117 114
 			.split("var out='';out+=").join('var out=');
118 115
 
9  doT.js
@@ -72,7 +72,7 @@
72 72
 	}
73 73
 
74 74
 	function unescape(code) {
75  
-		return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
  75
+		return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, ' ');
76 76
 	}
77 77
 
78 78
 	doT.template = function(tmpl, c, def) {
@@ -86,8 +86,7 @@
86 86
 		} else str = tmpl;
87 87
 
88 88
 		str = ("var out='" + ((c.strip) ? str.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g, ''): str)
89  
-			.replace(/\\/g, '\\\\')
90  
-			.replace(/'/g, "\\'")
  89
+			.replace(/'|\\/g, '\\$&')
91 90
 			.replace(c.interpolate || skip, function(m, code) {
92 91
 				return cse.start + unescape(code) + cse.end;
93 92
 			})
@@ -110,9 +109,7 @@
110 109
 				return "';" + unescape(code) + "out+='";
111 110
 			})
112 111
 			+ "';return out;")
113  
-			.replace(/\n/g, '\\n')
114  
-			.replace(/\t/g, '\\t')
115  
-			.replace(/\r/g, '\\r')
  112
+			.replace(/[\r\t\n]/g, '\\$&')
116 113
 			.split("out+='';").join('')
117 114
 			.split("var out='';out+=").join('var out=');
118 115
 
11  doT.min.js
... ...
@@ -1,7 +1,6 @@
1 1
 // Laura Doktorova https://github.com/olado/doT
2  
-(function(){function o(){var b={"&":"&#38;","<":"&#60;",">":"&#62;",'"':"&#34;","'":"&#39;","/":"&#47;"},a=/&(?!\\w+;)|<|>|\"|'|\//g;return function(f){return f?f.toString().replace(a,function(g){return b[g]||g}):f}}function p(b,a,f){return(typeof a==="string"?a:a.toString()).replace(b.define||h,function(g,e,c,i){if(e.indexOf("def.")===0)e=e.substring(4);if(!(e in f))if(c===":")f[e]=i;else eval("def['"+e+"']="+i);return""}).replace(b.use||h,function(g,e){var c=eval(e);return c?p(b,c,f):c})}function k(b){return b.replace(/\\'/g,
3  
-"'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g," ")}var j={version:"0.2.0",templateSettings:{evaluate:/\{\{([\s\S]+?)\}\}/g,interpolate:/\{\{=([\s\S]+?)\}\}/g,encode:/\{\{!([\s\S]+?)\}\}/g,use:/\{\{#([\s\S]+?)\}\}/g,define:/\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,conditional:/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,iterate:/\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,varname:"it",strip:true,append:true,selfcontained:false},template:undefined,compile:undefined},l=function(){return this||
4  
-(0,eval)("this")}();if(typeof module!=="undefined"&&module.exports)module.exports=j;else if(typeof define==="function"&&define.amd)define(function(){return j});else l.doT=j;l.encodeHTML=o();var q={append:{start:"'+(",end:")+'",startencode:"'+encodeHTML("},split:{start:"';out+=(",end:");out+='",startencode:"';out+=encodeHTML("}},h=/$^/;j.template=function(b,a,f){a=a||j.templateSettings;var g=a.append?q.append:q.split,e,c=0,i;if(a.use||a.define){var r=l.def;l.def=f||{};b=p(a,b,l.def);l.def=r}b=("var out='"+
5  
-(a.strip?b.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g,""):b).replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(a.interpolate||h,function(n,d){return g.start+k(d)+g.end}).replace(a.encode||h,function(n,d){e=true;return g.startencode+k(d)+g.end}).replace(a.conditional||h,function(n,d,m){return d?m?"';}else if("+k(m)+"){out+='":"';}else{out+='":m?"';if("+k(m)+"){out+='":"';}out+='"}).replace(a.iterate||h,function(n,d,m,s){if(!d)return"'} };out+='";c+=1;i=s||"i"+c;d=k(d);return"';var arr"+
6  
-c+"="+d+";if(arr"+c+"){var "+i+"=-1,l"+c+"=arr"+c+".length-1;while("+i+"<l"+c+"){"+m+"=arr"+c+"["+i+"+=1];out+='"}).replace(a.evaluate||h,function(n,d){return"';"+k(d)+"out+='"})+"';return out;").replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r").split("out+='';").join("").split("var out='';out+=").join("var out=");if(e&&a.selfcontained)b="var encodeHTML=("+o.toString()+"());"+b;try{return new Function(a.varname,b)}catch(t){typeof console!=="undefined"&&console.log("Could not create a template function: "+
7  
-b);throw t;}};j.compile=function(b,a){return j.template(b,null,a)}})();
  2
+(function(){function o(){var b={"&":"&#38;","<":"&#60;",">":"&#62;",'"':"&#34;","'":"&#39;","/":"&#47;"},a=/&(?!\\w+;)|<|>|\"|'|\//g;return function(f){return f?f.toString().replace(a,function(g){return b[g]||g}):f}}function p(b,a,f){return(typeof a==="string"?a:a.toString()).replace(b.define||h,function(g,e,c,i){if(e.indexOf("def.")===0)e=e.substring(4);if(!(e in f))if(c===":")f[e]=i;else eval("def['"+e+"']="+i);return""}).replace(b.use||h,function(g,e){var c=eval(e);return c?p(b,c,f):c})}function k(b){return b.replace(/\\('|\\)/g,
  3
+"$1").replace(/[\r\t\n]/g," ")}var j={version:"0.2.0",templateSettings:{evaluate:/\{\{([\s\S]+?)\}\}/g,interpolate:/\{\{=([\s\S]+?)\}\}/g,encode:/\{\{!([\s\S]+?)\}\}/g,use:/\{\{#([\s\S]+?)\}\}/g,define:/\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,conditional:/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,iterate:/\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,varname:"it",strip:true,append:true,selfcontained:false},template:undefined,compile:undefined},l=function(){return this||(0,eval)("this")}();
  4
+if(typeof module!=="undefined"&&module.exports)module.exports=j;else if(typeof define==="function"&&define.amd)define(function(){return j});else l.doT=j;l.encodeHTML=o();var q={append:{start:"'+(",end:")+'",startencode:"'+encodeHTML("},split:{start:"';out+=(",end:");out+='",startencode:"';out+=encodeHTML("}},h=/$^/;j.template=function(b,a,f){a=a||j.templateSettings;var g=a.append?q.append:q.split,e,c=0,i;if(a.use||a.define){var r=l.def;l.def=f||{};b=p(a,b,l.def);l.def=r}b=("var out='"+(a.strip?b.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g,
  5
+""):b).replace(/'|\\/g,"\\$&").replace(a.interpolate||h,function(n,d){return g.start+k(d)+g.end}).replace(a.encode||h,function(n,d){e=true;return g.startencode+k(d)+g.end}).replace(a.conditional||h,function(n,d,m){return d?m?"';}else if("+k(m)+"){out+='":"';}else{out+='":m?"';if("+k(m)+"){out+='":"';}out+='"}).replace(a.iterate||h,function(n,d,m,s){if(!d)return"'} };out+='";c+=1;i=s||"i"+c;d=k(d);return"';var arr"+c+"="+d+";if(arr"+c+"){var "+i+"=-1,l"+c+"=arr"+c+".length-1;while("+i+"<l"+c+"){"+
  6
+m+"=arr"+c+"["+i+"+=1];out+='"}).replace(a.evaluate||h,function(n,d){return"';"+k(d)+"out+='"})+"';return out;").replace(/[\r\t\n]/g,"\\$&").split("out+='';").join("").split("var out='';out+=").join("var out=");if(e&&a.selfcontained)b="var encodeHTML=("+o.toString()+"());"+b;try{return new Function(a.varname,b)}catch(t){typeof console!=="undefined"&&console.log("Could not create a template function: "+b);throw t;}};j.compile=function(b,a){return j.template(b,null,a)}})();

0 notes on commit d99027d

Please sign in to comment.
Something went wrong with that request. Please try again.