mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
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.
This commit is contained in:
@@ -55,6 +55,9 @@ $(document).ready(function() {
|
|||||||
var result = basicTemplate({thing : 'This'});
|
var result = basicTemplate({thing : 'This'});
|
||||||
equals(result, "This is gettin' on my noives!", 'can do basic attribute interpolation');
|
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("<ul><% \
|
var fancyTemplate = _.template("<ul><% \
|
||||||
for (key in people) { \
|
for (key in people) { \
|
||||||
%><li><%= people[key] %></li><% } %></ul>");
|
%><li><%= people[key] %></li><% } %></ul>");
|
||||||
|
|||||||
@@ -646,7 +646,8 @@
|
|||||||
var c = _.templateSettings;
|
var c = _.templateSettings;
|
||||||
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
|
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
|
||||||
'with(obj||{}){__p.push(\'' +
|
'with(obj||{}){__p.push(\'' +
|
||||||
str.replace(/'/g, "\\'")
|
str.replace(/\\/g, '\\\\')
|
||||||
|
.replace(/'/g, "\\'")
|
||||||
.replace(c.interpolate, function(match, code) {
|
.replace(c.interpolate, function(match, code) {
|
||||||
return "'," + code.replace(/\\'/g, "'") + ",'";
|
return "'," + code.replace(/\\'/g, "'") + ",'";
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user