Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

for real {% else if ... %} support and a test to prove it.

  • Loading branch information...
commit 6fcfb48d0f6ed8a48d3ef0a30c750905c83aebcc 1 parent 988b87f
@paularmstrong authored
Showing with 49 additions and 27 deletions.
  1. +33 −25 lib/tags.js
  2. +16 −2 tests/tags.test.js
View
58 lib/tags.js
@@ -82,36 +82,36 @@ function checkIfArgs(leftOperand, operator, rightOperand) {
}
exports['if'] = function (indent) {
- var operand1 = parser.parseVariable(this.args[0]),
+ var leftOperand = parser.parseVariable(this.args[0]),
operator = this.args[1],
- operand2 = parser.parseVariable(this.args[2]),
- negation = checkIfArgs(operand1, operator, operand2),
+ rightOperand = parser.parseVariable(this.args[2]),
+ negation = checkIfArgs(leftOperand, operator, rightOperand),
out;
indent = indent || '';
out = ['(function () {'];
- out.push(helpers.setVar('__op1', operand1));
- if (operand2.name === '') {
+ out.push(helpers.setVar('__op1', leftOperand));
+ if (rightOperand.name === '') {
out.push(' if (' + (negation ? '!' : '!!') + '__op1) {');
out.push(parser.compile.call(this, indent + ' '));
out.push(' }');
} else {
- out.push(helpers.setVar('__op2', operand2));
+ out.push(helpers.setVar('__op2', rightOperand));
if (typeof operator !== 'undefined') {
if (operator === 'in') {
- out.push(' if (');
+ out.push('if (');
out.push(' (Array.isArray(__op2) && __op2.indexOf(__op1) > -1) ||');
out.push(' (typeof __op2 === "string" && __op2.indexOf(__op1) > -1) ||');
out.push(' (!Array.isArray(__op2) && typeof __op2 === "object" && __op1 in __op2)');
- out.push(' ) {');
+ out.push(') {');
out.push(parser.compile.call(this, indent + ' '));
- out.push(' }');
+ out.push('}');
} else {
- out.push(' if (__op1 ' + helpers.escape(operator) + ' __op2) {');
+ out.push('if (__op1 ' + helpers.escape(operator) + ' __op2) {');
out.push(parser.compile.call(this, indent + ' '));
- out.push(' }');
+ out.push('}');
}
}
}
@@ -126,31 +126,39 @@ exports.else = function (indent) {
}
var ifarg = this.args.shift(),
- operand1, operator, operand2, negation, out;
+ leftOperand, operator, rightOperand, negation, out;
if (ifarg) {
- operand1 = parser.parseVariable(this.args[0]);
+ leftOperand = parser.parseVariable(this.args[0]);
operator = this.args[1];
- operand2 = parser.parseVariable(this.args[2]);
- negation = checkIfArgs(operand1, operator, operand2);
+ rightOperand = parser.parseVariable(this.args[2]);
+ negation = checkIfArgs(leftOperand, operator, rightOperand);
out = [];
- if (operand2.name === '') {
- out.push('} else if (' + (negation ? '!' : '!!') + '__op1) {');
- out.push(parser.compile.call(this, indent + ' '));
- out.push('}');
+ if (rightOperand.name === '') {
+ out.push('} else if (' + (negation ? '!' : '!!') + '(function() {' + helpers.setVar('__op1', leftOperand) + ' return __op1; })()) {');
} else {
- out.push(helpers.setVar('__op2', operand2));
+ out.push(helpers.setVar('__op2', rightOperand));
- if (typeof operator !== 'undefined') {
+ if (operator) {
if (operator === 'in') {
out.push('} else if (');
- out.push(' (Array.isArray(__op2) && __op2.indexOf(__op1) > -1) ||');
- out.push(' (typeof __op2 === "string" && __op2.indexOf(__op1) > -1) ||');
- out.push(' (!Array.isArray(__op2) && typeof __op2 === "object" && __op1 in __op2)');
+ out.push(' (function() {');
+ out.push(' ' + helpers.setVar('__op1', leftOperand));
+ out.push(' ' + helpers.setVar('__op2', rightOperand));
+ out.push(' return (Array.isArray(__op2) && __op2.indexOf(__op1) > -1) ||');
+ out.push(' (typeof __op2 === "string" && __op2.indexOf(__op1) > -1) ||');
+ out.push(' (!Array.isArray(__op2) && typeof __op2 === "object" && __op1 in __op2);');
+ out.push(' })()');
out.push(') {');
} else {
- out.push('} else if (__op1 ' + helpers.escape(operator) + ' __op2) {');
+ out.push('} else if (');
+ out.push(' (function() {');
+ out.push(' ' + helpers.setVar('__op1', leftOperand));
+ out.push(' ' + helpers.setVar('__op2', rightOperand));
+ out.push(' return __op1 ' + helpers.escape(operator) + ' __op2;');
+ out.push(' })()');
+ out.push(') {');
}
}
}
View
18 tests/tags.test.js
@@ -19,9 +19,12 @@ exports['custom tags'] = function (test) {
};
exports.if = testCase({
- basic: function (test) {
+ setUp: function (callback) {
swig.init({});
+ callback();
+ },
+ basic: function (test) {
var tmpl8 = swig.fromString('{% if foo %}hi!{% endif %}{% if bar %}nope{% endif %}');
test.strictEqual(tmpl8.render({ foo: 1, bar: false }), 'hi!');
test.done();
@@ -54,6 +57,17 @@ exports.if = testCase({
test.strictEqual(tmpl8.render({ foo: [1] }), 'bar');
test.done();
+ },
+
+ 'multiple else if and else': function (test) {
+ var tmpl8 = swig.fromString('{% if foo %}foo{% else if bar === "bar" %}bar{% else if 3 in baz %}baz{% else %}bop{% endif %}');
+ test.strictEqual(tmpl8.render({ foo: true }), 'foo');
+ test.strictEqual(tmpl8.render({ bar: "bar" }), 'bar');
+ test.strictEqual(tmpl8.render({ baz: [3] }), 'baz');
+ test.strictEqual(tmpl8.render({ baz: [2] }), 'bop');
+ test.strictEqual(tmpl8.render({ bar: false }), 'bop');
+
+ test.done();
}
});
@@ -81,4 +95,4 @@ exports.for = testCase({
test.strictEqual(tmpl8.render({ bar: ['baz', 'bar', 'foo'] }), 'foobarbaz');
test.done();
}
-});
+});
Please sign in to comment.
Something went wrong with that request. Please try again.