Permalink
Browse files

`extends` throws for circular extends. closes gh-8

  • Loading branch information...
1 parent b568d41 commit 390738cf0a3fa5edba798d179dfa36438b97c1b6 @paularmstrong paularmstrong committed Sep 27, 2011
Showing with 33 additions and 4 deletions.
  1. +8 −4 lib/parser.js
  2. +19 −0 tests/tags.test.js
  3. +3 −0 tests/templates/extends_circular1.html
  4. +3 −0 tests/templates/extends_circular2.html
View
@@ -224,10 +224,14 @@ exports.compile = function compile(indent, parentBlock) {
if (this.type !== TEMPLATE) {
throw new Error('Block tag found inside another tag on line' + token.line + '.');
}
- if (this.hasOwnProperty('parent') && this.parent.blocks.hasOwnProperty(blockname)) {
- this.blocks[blockname] = compile.call(token, indent + ' ', this.parent.blocks[blockname]);
- } else {
- this.blocks[blockname] = compile.call(token, indent + ' ');
+ try {
+ if (this.hasOwnProperty('parent') && this.parent.blocks.hasOwnProperty(blockname)) {
+ this.blocks[blockname] = compile.call(token, indent + ' ', this.parent.blocks[blockname]);
+ } else {
+ this.blocks[blockname] = compile.call(token, indent + ' ');
+ }
+ } catch (error) {
+ throw new Error('Circular extends found on line ' + token.line + ' of "' + this.id + '"!');
}
}
tokens.push(token);
View
@@ -19,6 +19,25 @@ exports['custom tags'] = function (test) {
test.done();
};
+exports.extends = testCase({
+ setUp: function (callback) {
+ swig.init({ root: __dirname + '/templates' });
+ callback();
+ },
+
+ basic: function (test) {
+ var tmpl8 = swig.fromFile('extends_1.html');
+ test.strictEqual('This is from the "extends_base.html" template.\n\n\n This is the "extends_1.html" content in block \'one\'\n\n\n This is the default content in block \'two\'\n', tmpl8.render());
+ test.done();
+ },
+
+ circular: function (test) {
+ var tmpl8 = swig.fromFile('extends_circular1.html');
+ test.ok((/^<pre>Error: Circular extends found on line 3 of \"extends_circular1\.html\"\!/).test(tmpl8.render()), 'throws an error');
+ test.done();
+ }
+});
+
exports.include = testCase({
setUp: function (callback) {
swig.init({ root: __dirname + '/templates' });
@@ -0,0 +1,3 @@
+{% extends 'extends_circular2.html' %}
+
+{% block content %}Foobar{% endblock %}
@@ -0,0 +1,3 @@
+{% extends 'extends_circular1.html' %}
+
+{% block content %}Barfoo{% endblock %}

0 comments on commit 390738c

Please sign in to comment.