mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 23:57:49 +00:00
Merging in escaping for Underscore templates, using <%- syntax. Sorry Eco.
This commit is contained in:
@@ -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><script></i>');
|
||||
|
||||
if (!$.browser.msie) {
|
||||
var fromHTML = _.template($('#template').html());
|
||||
equals(fromHTML({data : 12345}).replace(/\s/g, ''), '<li>24690</li>');
|
||||
|
||||
@@ -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, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
|
||||
};
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user