Underscore 0.5.4 -- bugfix for single quotes in _.template strings

This commit is contained in:
Jeremy Ashkenas
2010-01-05 11:26:14 -05:00
parent fde8b1f63f
commit 0a8a4834b2
3 changed files with 21 additions and 14 deletions

View File

@@ -38,7 +38,7 @@
propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
// Current version.
_.VERSION = '0.5.3';
_.VERSION = '0.5.4';
// ------------------------ Collection Functions: ---------------------------
@@ -561,19 +561,19 @@
// JavaScript templating a-la ERB, pilfered from John Resig's
// "Secrets of the JavaScript Ninja", page 83.
// Single-quote fix from Rick Strahl's version.
_.template = function(str, data) {
var fn = new Function('obj',
'var p=[],print=function(){p.push.apply(p,arguments);};' +
'with(obj){p.push(\'' +
str
.replace(/[\r\t\n]/g, " ")
.split("<%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%>").join("p.push('")
.split("\r").join("\\'")
+ "');}return p.join('');");
str.replace(/[\r\t\n]/g, " ")
.replace(/'(?=[^%]*%>)/g,"\t")
.split("'").join("\\'")
.split("\t").join("'")
.replace(/<%=(.+?)%>/g, "',$1,'")
.split("<%").join("');")
.split("%>").join("p.push('")
+ "');}return p.join('');");
return data ? fn(data) : fn;
};