Skip to content
Browse files

Fix :else when condition was not in parenthesis

:else is implemented via "if ! condition", so in the following
expression:

:if 1+1==3
 ...
:else
 ...

the else part is compiled into "if ! 1+1 == 3". Since in JavaScript,
the not operator has higher precedence, this is interpreted as
"if (!1)+1 == 3", which is false. Therefore, the above expression
will output nothing unless the condition would have been written as
":if (1+1==3)"

This commit fixes :else and :elseif by always adding parenthesis like
it is done for if.
  • Loading branch information...
1 parent b4244f4 commit d33db3001a3a045c356309c5c707109aa436bcdc @brakhane brakhane committed Sep 20, 2012
Showing with 8 additions and 3 deletions.
  1. +3 −3 lib/haml.js
  2. +4 −0 test/else.haml
  3. +1 −0 test/else.html
View
6 lib/haml.js
@@ -329,7 +329,7 @@ var Haml;
ifArray = [],
ifStatement;
for (var i=0, l=conditionsArray.length; i<l; i++) {
- ifArray.push('! ' + conditionsArray[i]);
+ ifArray.push('! (' + conditionsArray[i]+')');
}
conditionsArray.push(condition);
ifArray.push(condition);
@@ -350,7 +350,7 @@ var Haml;
ifArray = [],
ifStatement;
for (var i=0, l=conditionsArray.length; i<l; i++) {
- ifArray.push('! ' + conditionsArray[i]);
+ ifArray.push('! (' + conditionsArray[i]+')');
}
ifStatement = 'if (' + ifArray.join(' && ') + ') { ';
return '(function () { ' +
@@ -700,4 +700,4 @@ var Haml;
// Hook into module system
if (typeof module !== 'undefined') {
module.exports = Haml;
-}
+}
View
4 test/else.haml
@@ -0,0 +1,4 @@
+:if 1 + 1 == 3
+ %p 1+1 = 3
+:else
+ %p 1+1 != 3
View
1 test/else.html
@@ -0,0 +1 @@
+<p>1+1 != 3</p>

0 comments on commit d33db30

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