From 57bb74bee7a778752d62e2975fb44b03977cb86c Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Fri, 6 Apr 2012 08:28:00 -0400 Subject: [PATCH 1/2] _.template uses concatenation instead of join. --- underscore.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/underscore.js b/underscore.js index 5cf516b80..a5b177efb 100644 --- a/underscore.js +++ b/underscore.js @@ -957,25 +957,26 @@ // Compile the template source, taking care to escape characters that // cannot be included in a string literal and then unescape them in code // blocks. - var source = "__p.push('" + text + var source = "__p+='" + text .replace(escaper, function(match) { return '\\' + escapes[match]; }) .replace(settings.escape || noMatch, function(match, code) { - return "',\n_.escape(" + unescape(code) + "),\n'"; + return "'+\n_.escape(" + unescape(code) + ")+\n'"; }) .replace(settings.interpolate || noMatch, function(match, code) { - return "',\n" + unescape(code) + ",\n'"; + return "'+(\n" + unescape(code) + ")+\n'"; }) .replace(settings.evaluate || noMatch, function(match, code) { - return "');\n" + unescape(code) + "\n;__p.push('"; - }) + "');\n"; + return "';\n" + unescape(code) + "\n;__p+='"; + }) + "';\n"; // If a variable is not specified, place data values in local scope. if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n'; - source = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};\n' + - source + "return __p.join('');\n"; + source = "var __p='';" + + "var print=function(){__p+=Array.prototype.join.call(arguments, '')};\n" + + source + "return __p;\n"; var render = new Function(settings.variable || 'obj', '_', source); if (data) return render(data, _); From 9392d3df186c753cfaf5b31fb4c6f25294fcc8ed Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Fri, 6 Apr 2012 09:59:59 -0400 Subject: [PATCH 2/2] Correct errant newlines. --- underscore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/underscore.js b/underscore.js index a5b177efb..56be06fa6 100644 --- a/underscore.js +++ b/underscore.js @@ -965,7 +965,7 @@ return "'+\n_.escape(" + unescape(code) + ")+\n'"; }) .replace(settings.interpolate || noMatch, function(match, code) { - return "'+(\n" + unescape(code) + ")+\n'"; + return "'+\n(" + unescape(code) + ")+\n'"; }) .replace(settings.evaluate || noMatch, function(match, code) { return "';\n" + unescape(code) + "\n;__p+='"; @@ -987,7 +987,7 @@ // Provide the compiled function source as a convenience for build time // precompilation. template.source = 'function(' + (settings.variable || 'obj') + '){\n' + - source + '\n}'; + source + '}'; return template; };