Skip to content

Commit

Permalink
Merge branch 't576'
Browse files Browse the repository at this point in the history
* t576:
  Add test verifying that block-set can wrap a block.
  Fix filter block tag (fixes #576).
  Refactor this block of test assertions into separate tests.
  Adds *failing* tests illustrating both issues mentioned in #576. The additional tests focus on template constructs within the filter block (`{% filter ... %}...{% endfilter %}` as introduced by #254): 1. variable evaluation inside filter block 2. block declaration inside filter block
  • Loading branch information
carljm committed Mar 14, 2016
2 parents e635171 + 2bb4cfb commit c7f8403
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 51 deletions.
17 changes: 12 additions & 5 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ var Compiler = Object.extend({
compileFilter: function(node, frame) {
var name = node.name;
this.assertType(name, nodes.Symbol);

this.emit('env.getFilter("' + name.value + '").call(context, ');
this._compileAggregate(node.args, frame);
this.emit(')');
Expand Down Expand Up @@ -529,11 +528,9 @@ var Compiler = Object.extend({
this.emitLine(';');
}
else {
this.emitLine(ids.join(' = ') + ' = (function() {');
this.emitLine('var output = "";');
this.emit(ids.join(' = ') + ' = ');
this.compile(node.body, frame);
this.emitLine('return output;');
this.emitLine('})();');
this.emitLine(';');
}

lib.each(node.targets, function(target, i) {
Expand Down Expand Up @@ -1046,6 +1043,16 @@ var Compiler = Object.extend({
this.compileLiteral(node, frame);
},

compileCapture: function(node, frame) {
this.emitLine('(function() {');
this.emitLine('var output = "";');
this.withScopedSyntax(function () {
this.compile(node.body, frame);
});
this.emitLine('return output;');
this.emitLine('})()');
},

compileOutput: function(node, frame) {
var children = node.children;
for(var i=0, l=children.length; i<l; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var Extends = TemplateRef.extend('Extends');
var Include = Node.extend('Include', { fields: ['template', 'ignoreMissing'] });
var Set = Node.extend('Set', { fields: ['targets', 'value'] });
var Output = NodeList.extend('Output');
var Capture = Node.extend('Capture', { fields: ['body'] });
var TemplateData = Literal.extend('TemplateData');
var UnaryOp = Node.extend('UnaryOp', { fields: ['target'] });
var BinOp = Node.extend('BinOp', { fields: ['left', 'right'] });
Expand Down Expand Up @@ -255,6 +256,7 @@ module.exports = {
Pair: Pair,
Dict: Dict,
Output: Output,
Capture: Capture,
TemplateData: TemplateData,
If: If,
IfAsync: IfAsync,
Expand Down
16 changes: 11 additions & 5 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,11 @@ var Parser = Object.extend({
tag.colno);
}
else {
node.body = this.parseUntilBlocks('endset');
node.body = new nodes.Capture(
tag.lineno,
tag.colno,
this.parseUntilBlocks('endset')
);
node.value = null;
this.advanceAfterBlockEnd();
}
Expand Down Expand Up @@ -1025,7 +1029,11 @@ var Parser = Object.extend({
var args = this.parseFilterArgs(name);

this.advanceAfterBlockEnd(filterTok.value);
var body = this.parseUntilBlocks('endfilter');
var body = new nodes.Capture(
name.lineno,
name.colno,
this.parseUntilBlocks('endfilter')
);
this.advanceAfterBlockEnd();

var node = new nodes.Filter(
Expand All @@ -1035,9 +1043,7 @@ var Parser = Object.extend({
new nodes.NodeList(
name.lineno,
name.colno,
// Body is a NodeList with an Output node as a child,
// need to strip those
body.children[0].children.concat(args)
[body].concat(args)
)
);

Expand Down
110 changes: 69 additions & 41 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,41 +1101,41 @@
finish(done);
});

it('should compile set blocks', function(done) {
equal('{% set block_content %}{% endset %}'+
'{{ block_content }}',
''
);

equal('{% set block_content %}test string{% endset %}'+
'{{ block_content }}',
'test string'
);

equal('{% set block_content %}'+
'{% for item in [1, 2, 3] %}'+
'{% include "item.j2" %} '+
'{% endfor %}'+
'{% endset %}'+
'{{ block_content }}',
'showing 1 showing 2 showing 3 '
);

equal('{% set block_content %}'+
'{% set inner_block_content %}'+
'{% for i in [1, 2, 3] %}'+
'item {{ i }} '+
'{% endfor %}'+
'{% endset %}'+
'{% for i in [1, 2, 3] %}'+
'inner {{i}}: "{{ inner_block_content }}" '+
'{% endfor %}'+
'{% endset %}'+
'{{ block_content | safe }}',
'inner 1: "item 1 item 2 item 3 " '+
'inner 2: "item 1 item 2 item 3 " '+
'inner 3: "item 1 item 2 item 3 " '
);
it('should compile block-set', function(done) {
equal('{% set block_content %}{% endset %}'+
'{{ block_content }}',
''
);

equal('{% set block_content %}test string{% endset %}'+
'{{ block_content }}',
'test string'
);

equal('{% set block_content %}'+
'{% for item in [1, 2, 3] %}'+
'{% include "item.j2" %} '+
'{% endfor %}'+
'{% endset %}'+
'{{ block_content }}',
'showing 1 showing 2 showing 3 '
);

equal('{% set block_content %}'+
'{% set inner_block_content %}'+
'{% for i in [1, 2, 3] %}'+
'item {{ i }} '+
'{% endfor %}'+
'{% endset %}'+
'{% for i in [1, 2, 3] %}'+
'inner {{i}}: "{{ inner_block_content }}" '+
'{% endfor %}'+
'{% endset %}'+
'{{ block_content | safe }}',
'inner 1: "item 1 item 2 item 3 " '+
'inner 2: "item 1 item 2 item 3 " '+
'inner 3: "item 1 item 2 item 3 " '
);

equal('{% set x,y,z %}'+
'cool'+
Expand All @@ -1147,6 +1147,14 @@
finish(done);
});

it('should compile block-set wrapping an inherited block', function(done) {
equal('{% extends "base-set-wraps-block.j2" %}'+
'{% block somevar %}foo{% endblock %}',
'foo\n'
);
finish(done);
});

it('should throw errors', function(done) {
render('{% from "import.j2" import boozle %}',
{},
Expand Down Expand Up @@ -1452,13 +1460,33 @@
finish(done);
});

it('should handle filter blocks', function(done) {
equal('{% filter title %}may the force be with you{% endfilter %}',
'May The Force Be With You');
describe('the filter tag', function() {

equal('{% filter replace("force", "forth") %}may the force be with you{% endfilter %}',
'may the forth be with you');
finish(done);
it('should apply the title filter to the body', function(done) {
equal('{% filter title %}may the force be with you{% endfilter %}',
'May The Force Be With You');
finish(done);
});

it('should apply the replace filter to the body', function(done) {

equal('{% filter replace("force", "forth") %}may the force be with you{% endfilter %}',
'may the forth be with you');
finish(done);
});

it('should work with variables in the body', function(done) {
equal('{% set foo = "force" %}{% filter replace("force", "forth") %}may the {{ foo }} be with you{% endfilter %}',
'may the forth be with you');
finish(done);
});

it('should work with blocks in the body', function(done) {
equal('{% extends "filter-block.html" %}' +
'{% block block1 %}force{% endblock %}',
'may the forth be with you\n');
finish(done);
});
});

it('should throw an error when including a file that calls an undefined macro', function(done) {
Expand Down
1 change: 1 addition & 0 deletions tests/templates/base-set-wraps-block.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% set somevar %}{% block somevar %}{% endblock %}{% endset %}{{ somevar }}
1 change: 1 addition & 0 deletions tests/templates/filter-block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
may the {% filter replace("force", "forth") %}{% block block1 %}bar{% endblock %}{% endfilter %} be with you

0 comments on commit c7f8403

Please sign in to comment.