Merging in escaping for Underscore templates, using <%- syntax. Sorry Eco.

This commit is contained in:
Jeremy Ashkenas
2011-10-05 16:19:00 -04:00
parent b501ba49e9
commit cc6a9d494d
2 changed files with 7 additions and 3 deletions

View File

@@ -81,6 +81,10 @@ $(document).ready(function() {
var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.');
equals(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');
var template = _.template("<i><%- value %></i>");
var result = template({value: "<script>"});
equals(result, '<i>&lt;script&gt;</i>');
if (!$.browser.msie) {
var fromHTML = _.template($('#template').html());
equals(fromHTML({data : 12345}).replace(/\s/g, ''), '<li>24690</li>');

View File

@@ -816,7 +816,7 @@
for (var i = 0; i < n; i++) iterator.call(context, i);
};
// Escape string for HTML
// Escape a string for HTML interpolation.
_.escape = function(string) {
return (''+string).replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g,'&#x2F;');
};
@@ -842,7 +842,7 @@
_.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
encode : /<%==([\s\S]+?)%>/g
escape : /<%-([\s\S]+?)%>/g
};
// JavaScript micro-templating, similar to John Resig's implementation.
@@ -854,7 +854,7 @@
'with(obj||{}){__p.push(\'' +
str.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(c.encode, function(match, code) {
.replace(c.escape, function(match, code) {
return "',_.escape(" + code.replace(/\\'/g, "'") + "),'";
})
.replace(c.interpolate, function(match, code) {