mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 17:07:49 +00:00
Allow natural multi-line code evaluation in templates.
By escaping `\r`, `\n`, and `\t` earlier, we can unescape them along with backslashes and single quotes allowing for the inclusion of single line comments and code without a terminating semicolon.
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="underscore-test">
|
<div class="underscore-test">
|
||||||
<h1 id="qunit-header">Underscore Test Suite</h1>
|
<h1 id="qunit-header">Underscore Test Suite</h1>
|
||||||
|
<div id="qunit-testrunner-toolbar"></div>
|
||||||
<h2 id="qunit-banner"></h2>
|
<h2 id="qunit-banner"></h2>
|
||||||
<h2 id="qunit-userAgent"></h2>
|
<h2 id="qunit-userAgent"></h2>
|
||||||
<ol id="qunit-tests"></ol>
|
<ol id="qunit-tests"></ol>
|
||||||
@@ -34,6 +35,7 @@
|
|||||||
|
|
||||||
<script type="text/html" id="template">
|
<script type="text/html" id="template">
|
||||||
<%
|
<%
|
||||||
|
// a comment
|
||||||
if (data) { data += 12345; }; %>
|
if (data) { data += 12345; }; %>
|
||||||
<li><%= data %></li>
|
<li><%= data %></li>
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
|
|
||||||
module("Utility");
|
var templateSettings = _.templateSettings;
|
||||||
|
|
||||||
|
module("Utility", {
|
||||||
|
|
||||||
|
teardown: function() {
|
||||||
|
_.templateSettings = templateSettings;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
test("utility: noConflict", function() {
|
test("utility: noConflict", function() {
|
||||||
var underscore = _.noConflict();
|
var underscore = _.noConflict();
|
||||||
|
|||||||
@@ -905,7 +905,15 @@
|
|||||||
// 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(/\\\\/g, '\\').replace(/\\'/g, "'");
|
return code.replace(/\\(\\|'|r|n|t)/g, function(match, char) {
|
||||||
|
switch (char) {
|
||||||
|
case '\\': return '\\';
|
||||||
|
case "'": return "'";
|
||||||
|
case 'r': return '\r';
|
||||||
|
case 'n': return '\n';
|
||||||
|
case 't': return '\t';
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// JavaScript micro-templating, similar to John Resig's implementation.
|
// JavaScript micro-templating, similar to John Resig's implementation.
|
||||||
@@ -917,18 +925,18 @@
|
|||||||
'with(obj||{}){__p.push(\'' +
|
'with(obj||{}){__p.push(\'' +
|
||||||
str.replace(/\\/g, '\\\\')
|
str.replace(/\\/g, '\\\\')
|
||||||
.replace(/'/g, "\\'")
|
.replace(/'/g, "\\'")
|
||||||
.replace(c.escape || noMatch, function(match, code) {
|
|
||||||
return "',_.escape(" + unescape(code) + "),'";
|
|
||||||
})
|
|
||||||
.replace(c.interpolate || noMatch, function(match, code) {
|
|
||||||
return "'," + unescape(code) + ",'";
|
|
||||||
})
|
|
||||||
.replace(c.evaluate || noMatch, function(match, code) {
|
|
||||||
return "');" + unescape(code).replace(/[\r\n\t]/g, ' ') + ";__p.push('";
|
|
||||||
})
|
|
||||||
.replace(/\r/g, '\\r')
|
.replace(/\r/g, '\\r')
|
||||||
.replace(/\n/g, '\\n')
|
.replace(/\n/g, '\\n')
|
||||||
.replace(/\t/g, '\\t')
|
.replace(/\t/g, '\\t')
|
||||||
|
.replace(c.escape || noMatch, function(match, code) {
|
||||||
|
return "',_.escape(" + unescape(code) + "),\n'";
|
||||||
|
})
|
||||||
|
.replace(c.interpolate || noMatch, function(match, code) {
|
||||||
|
return "'," + unescape(code) + ",\n'";
|
||||||
|
})
|
||||||
|
.replace(c.evaluate || noMatch, function(match, code) {
|
||||||
|
return "');" + unescape(code) + ";\n__p.push('";
|
||||||
|
})
|
||||||
+ "');}return __p.join('');";
|
+ "');}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, _);
|
||||||
|
|||||||
Reference in New Issue
Block a user