From 47eb154ccff6562d463fee8168228050a4bf1eaa Mon Sep 17 00:00:00 2001 From: Jason Davies Date: Tue, 9 Nov 2010 22:59:03 +0000 Subject: [PATCH] Preserve backslashes in _.template() For example, templates containing things like the literal string `\ribbit` weren't being escaped properly when being compiled. The `\r` was being interpreted as a carriage return character. I've added a test for this too, and all the existing tests pass. --- test/utility.js | 3 +++ underscore.js | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/test/utility.js b/test/utility.js index db87a4d08..0265c0328 100644 --- a/test/utility.js +++ b/test/utility.js @@ -55,6 +55,9 @@ $(document).ready(function() { var result = basicTemplate({thing : 'This'}); equals(result, "This is gettin' on my noives!", 'can do basic attribute interpolation'); + var backslashTemplate = _.template("<%= thing %> is \\ridanculous"); + equals(backslashTemplate({thing: 'This'}), "This is \\ridanculous"); + var fancyTemplate = _.template(""); diff --git a/underscore.js b/underscore.js index 6f23ffbd8..7b17fc649 100644 --- a/underscore.js +++ b/underscore.js @@ -646,7 +646,8 @@ var c = _.templateSettings; var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' + 'with(obj||{}){__p.push(\'' + - str.replace(/'/g, "\\'") + str.replace(/\\/g, '\\\\') + .replace(/'/g, "\\'") .replace(c.interpolate, function(match, code) { return "'," + code.replace(/\\'/g, "'") + ",'"; })