Permalink
Browse files

fix #51 add utils.genRandomName() and warning when missing name in ta…

…g {% block %} and {% capture %}
  • Loading branch information...
1 parent 80b1393 commit 84614638372aff3a514587fb28ec60943f5e93a7 @leizongmin committed Nov 26, 2016
Showing with 28 additions and 0 deletions.
  1. +6 −0 lib/parser.js
  2. +10 −0 lib/utils.js
  3. +12 −0 test/tag_capture.js
View
@@ -305,6 +305,9 @@ var baseTags = {
'capture': function (context, name, body) {
var blocks = arrayRemoveEmptyString(splitText(body, [' ']));
var name = blocks[0] || genRandomName();
+ if (!blocks[0]) {
+ context.astStack.push(context.astNode(OPCODE.PRINTSTRING, 'warning: missing name in {% capture %}'));
+ }
context.astStack.newChild(context.astNode(OPCODE.CAPTURE, name));
},
@@ -317,6 +320,9 @@ var baseTags = {
'block': function (context, name, body) {
var blocks = arrayRemoveEmptyString(splitText(body, [' ']));
var name = blocks[0] || genRandomName();
+ if (!blocks[0]) {
+ context.astStack.push(context.astNode(OPCODE.PRINTSTRING, 'warning: missing name in {% block %}'));
+ }
context.astStack.newChild(context.astNode(OPCODE.BLOCK, name));
},
View
@@ -519,6 +519,16 @@ utils.asyncFor = function (test, fn, callback, a1, a2, a3) {
next();
};
+utils.genRandomName = function () {
+ var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
+ var max = chars.length;
+ var ret = '';
+ for (var i = 0; i < 10; i++) {
+ ret += chars.charAt(Math.floor(Math.random() * max));
+ }
+ return ret;
+};
+
/******************************************************************************/
/**
View
@@ -15,5 +15,17 @@ describe('Tag: capture', function () {
})
.end(done);
});
+
+ it('#capture (no name)', function (done) {
+ common.taskList()
+ .add(function (done) {
+ common.render('{% capture %}ABC{% endcapture %}{{a}}-{{a}}', function (err, buf) {
+ assert.equal(err, null);
+ assert.equal(buf, 'warning: missing name in {% capture %}-');
+ done();
+ });
+ })
+ .end(done);
+ });
});

0 comments on commit 8461463

Please sign in to comment.