mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Clean up _.template.
* Cache regexes. * Use object properties for lookup instead of switch.
This commit is contained in:
@@ -902,19 +902,27 @@
|
|||||||
// guaranteed not to match.
|
// guaranteed not to match.
|
||||||
var noMatch = /.^/;
|
var noMatch = /.^/;
|
||||||
|
|
||||||
|
// Certain characters need to be escaped so that they can be put into a
|
||||||
|
// string literal.
|
||||||
|
var escapes = {
|
||||||
|
'\\': '\\',
|
||||||
|
"'": "'",
|
||||||
|
'r': '\r',
|
||||||
|
'n': '\n',
|
||||||
|
't': '\t',
|
||||||
|
'u2028': '\u2028',
|
||||||
|
'u2029': '\u2029'
|
||||||
|
};
|
||||||
|
|
||||||
|
for (var p in escapes) escapes[escapes[p]] = p;
|
||||||
|
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
|
||||||
|
var unescaper = /\\(\\|'|r|n|t|u2028|u2029)/g;
|
||||||
|
|
||||||
// Within an interpolation, evaluation, or escaping, remove HTML escaping
|
// Within an interpolation, evaluation, or escaping, remove HTML escaping
|
||||||
// that had been previously added.
|
// that had been previously added.
|
||||||
var unescape = function(code) {
|
var unescape = function(code) {
|
||||||
return code.replace(/\\(\\|'|r|n|t|u2028|u2029)/g, function(match, char) {
|
return code.replace(unescaper, function(match, escape) {
|
||||||
switch (char) {
|
return escapes[escape];
|
||||||
case '\\': return '\\';
|
|
||||||
case "'": return "'";
|
|
||||||
case 'r': return '\r';
|
|
||||||
case 'n': return '\n';
|
|
||||||
case 't': return '\t';
|
|
||||||
case 'u2028': return '\u2028';
|
|
||||||
case 'u2029': return '\u2029';
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -925,23 +933,20 @@
|
|||||||
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(escaper, function(match) {
|
||||||
.replace(/\r/g, '\\r')
|
return '\\' + escapes[match];
|
||||||
.replace(/\n/g, '\\n')
|
})
|
||||||
.replace(/\t/g, '\\t')
|
.replace(c.escape || noMatch, function(match, code) {
|
||||||
.replace(/\u2028/g, '\\u2028')
|
return "',_.escape(" + unescape(code) + "),\n'";
|
||||||
.replace(/\u2029/g, '\\u2029')
|
})
|
||||||
.replace(c.escape || noMatch, function(match, code) {
|
.replace(c.interpolate || noMatch, function(match, code) {
|
||||||
return "',_.escape(" + unescape(code) + "),\n'";
|
return "'," + unescape(code) + ",\n'";
|
||||||
})
|
})
|
||||||
.replace(c.interpolate || noMatch, function(match, code) {
|
.replace(c.evaluate || noMatch, function(match, code) {
|
||||||
return "'," + unescape(code) + ",\n'";
|
return "');" + unescape(code) + ";\n__p.push('";
|
||||||
})
|
})
|
||||||
.replace(c.evaluate || noMatch, function(match, code) {
|
+ "');}return __p.join('');";
|
||||||
return "');" + unescape(code) + ";\n__p.push('";
|
|
||||||
})
|
|
||||||
+ "');}return __p.join('');";
|
|
||||||
var func = new Function('obj', '_', tmpl);
|
var func = new Function('obj', '_', tmpl);
|
||||||
if (data) return func(data, _);
|
if (data) return func(data, _);
|
||||||
return function(data) {
|
return function(data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user