mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 19:07: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.');
|
var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.');
|
||||||
equals(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\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) {
|
if (!$.browser.msie) {
|
||||||
var fromHTML = _.template($('#template').html());
|
var fromHTML = _.template($('#template').html());
|
||||||
equals(fromHTML({data : 12345}).replace(/\s/g, ''), '<li>24690</li>');
|
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);
|
for (var i = 0; i < n; i++) iterator.call(context, i);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Escape string for HTML
|
// Escape a string for HTML interpolation.
|
||||||
_.escape = function(string) {
|
_.escape = function(string) {
|
||||||
return (''+string).replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
|
return (''+string).replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
|
||||||
};
|
};
|
||||||
@@ -842,7 +842,7 @@
|
|||||||
_.templateSettings = {
|
_.templateSettings = {
|
||||||
evaluate : /<%([\s\S]+?)%>/g,
|
evaluate : /<%([\s\S]+?)%>/g,
|
||||||
interpolate : /<%=([\s\S]+?)%>/g,
|
interpolate : /<%=([\s\S]+?)%>/g,
|
||||||
encode : /<%==([\s\S]+?)%>/g
|
escape : /<%-([\s\S]+?)%>/g
|
||||||
};
|
};
|
||||||
|
|
||||||
// JavaScript micro-templating, similar to John Resig's implementation.
|
// JavaScript micro-templating, similar to John Resig's implementation.
|
||||||
@@ -854,7 +854,7 @@
|
|||||||
'with(obj||{}){__p.push(\'' +
|
'with(obj||{}){__p.push(\'' +
|
||||||
str.replace(/\\/g, '\\\\')
|
str.replace(/\\/g, '\\\\')
|
||||||
.replace(/'/g, "\\'")
|
.replace(/'/g, "\\'")
|
||||||
.replace(c.encode, function(match, code) {
|
.replace(c.escape, function(match, code) {
|
||||||
return "',_.escape(" + code.replace(/\\'/g, "'") + "),'";
|
return "',_.escape(" + code.replace(/\\'/g, "'") + "),'";
|
||||||
})
|
})
|
||||||
.replace(c.interpolate, function(match, code) {
|
.replace(c.interpolate, function(match, code) {
|
||||||
|
|||||||
Reference in New Issue
Block a user