Skip to content
Browse files

Add {{verbatim}} tag and test

  • Loading branch information...
1 parent 93b18ac commit a3a337ca662302158decca8df478e06c7e3f62de @vadimg committed
Showing with 48 additions and 8 deletions.
  1. +37 −8 lib/jqtpl.js
  2. +11 −0 test/jqtpl.js
View
45 lib/jqtpl.js
@@ -101,6 +101,9 @@ exports.tag = {
'!': {
// Comment tag. Skipped by parser
open: ''
+ },
+ 'verbatim': {
+
}
};
@@ -114,6 +117,17 @@ function unescape(args) {
}
/**
+ * Escapes a string
+ * @param {string} str input string
+ * @return {string} escaped string
+ */
+function escapeString(str) {
+ return str.replace(/([\\"])/g, '\\$1')
+ .replace(/\n/g, '\\n')
+ .replace(/\r/g, '\\r');
+}
+
+/**
* Build reusable function for template generation
*
* @param {string} markup html string.
@@ -121,21 +135,34 @@ function unescape(args) {
* @export
*/
function buildTmplFn(markup) {
- return new Function('$', '$item',
- 'var call,_=[],$data=$item.data;' +
+ var verbatims = [];
+
+ // save all the data inside the verbatim tags
+ markup = markup.replace(/\{\{verbatim\}\}((.|\n)*?)\{\{\/verbatim\}\}/g,
+ function(all, content) {
+ verbatims.push(content);
+
+ // replace the {{verbatim}}data{{/verbatim}} with just {{verbatim}}
+ // this tag will let the parser know where to inject the corresponding data
+ return "{{verbatim}}";
+ });
+
+ return new Function('$', '$item', 'var call,_=[],$data=$item.data;' +
// Introduce the data as local variables using with(){}
'with($data){_.push("' +
// Convert the template into pure JavaScript
- markup
- .trim()
- .replace(/([\\"])/g, '\\$1')
- .replace(/\n/g, '\\n')
- .replace(/\r/g, '\\r')
+ escapeString(markup.trim())
.replace(/\$\{([^\}]*)\}/g, '{{= $1}}')
.replace(/\{\{(\/?)(\w+|.)(?:\(((?:[^\}]|\}(?!\}))*?)?\))?(?:\s+(.*?)?)?(\(((?:[^\}]|\}(?!\}))*?)\))?\s*\}\}/g,
function(all, slash, type, fnargs, target, parens, args) {
+
+ if (type == "verbatim") {
+ // inject the corresponding verbatim data
+ return escapeString(verbatims.shift());
+ }
+
var tag = exports.tag[type], def, expr, exprAutoFnDetect;
if (!tag) {
@@ -160,13 +187,15 @@ function buildTmplFn(markup) {
exprAutoFnDetect = expr = def.$1 || 'null';
}
fnargs = unescape(fnargs);
- return '");' +
+
+ x= '");' +
tag[slash ? 'close' : 'open']
.split('$notnull_1').join(target ? 'typeof(' + target + ')!=="undefined" && (' + target + ')!=null' : 'true')
.split('$1a').join(exprAutoFnDetect)
.split('$1').join(expr)
.split('$2').join(fnargs || def.$2 || '') +
'_.push("';
+ return x;
}
) +
'");}return _.join("");'
View
11 test/jqtpl.js
@@ -127,6 +127,17 @@ test('{{!}}', function() {
equal( tmpl("<div>{{! its a comment}}</div>", {a:1}), "<div></div>", "comments work" );
});
+test('{{verbatim}}', function() {
+ equal(
+ tmpl(
+ "<div>{{= a}}{{verbatim}}${a}12345{{/verbatim}{{/verbatim}}{{verbatim}}}{{= a}}{{/verbatim}}${a}</div>",
+ {a:1}
+ ),
+ "<div>1${a}12345{{/verbatim}}{{= a}}1</div>",
+ "verbatim"
+ );
+});
+
test('empty template', function() {
try {
equal(tmpl(''), '', 'empty template string rendered without errors');

0 comments on commit a3a337c

Please sign in to comment.
Something went wrong with that request. Please try again.