Skip to content

Commit

Permalink
Fixed equality op coercion issue. Closes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Dec 29, 2010
1 parent 896baa6 commit 0bbe983
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/nodes/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Color.prototype.coerce = function(other){
var n = other.val;
return new Color(n,n,n,1);
} else {
Node.prototype.coerce.call(this, other);
return Node.prototype.coerce.call(this, other);
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/nodes/hsla.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ HSLA.prototype.coerce = function(other){
if (other instanceof nodes.Color) {
return other.hsl;
} else {
Node.prototype.coerce.call(this, other);
return Node.prototype.coerce.call(this, other);
}
};

Expand Down
1 change: 1 addition & 0 deletions lib/nodes/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,6 @@ Node.prototype.operate = function(op, right){
*/

Node.prototype.coerce = function(other){
if (other.nodeName == this.nodeName) return other;
throw new Error('cannot coerce ' + other + ' to ' + this.nodeName);
};
6 changes: 5 additions & 1 deletion lib/nodes/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ String.prototype.toBoolean = function(){
*/

String.prototype.coerce = function(other){
return new String(other.toString());
if (other instanceof String) {
return other;
} else {
return new String(other.toString());
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/nodes/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,6 @@ Unit.prototype.coerce = function(other){
}
return new nodes.Unit(b.val, a.type);
} else {
Node.prototype.coerce.call(this, other);
return Node.prototype.coerce.call(this, other);
}
};
4 changes: 1 addition & 3 deletions lib/visitor/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@ Evaluator.prototype.visitBinOp = function(binop){
// Coercion
var ignore = ['||', '&&', 'is a'];
if (!~ignore.indexOf(op)) {
if (right.nodeName != left.nodeName) {
right = left.coerce(right);
}
right = left.coerce(right);
}

// Operate
Expand Down
3 changes: 3 additions & 0 deletions test/integration/tests/operators.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ body {
foo: wahoo;
foo: nope;
foo: true;
foo: true;
foo: false;
foo: "got 15px";
}
9 changes: 9 additions & 0 deletions test/integration/tests/operators.in
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ body
// true
foo (@foo is defined ? yes : nope) == yes

// true
foo 1000ms == 1s

// false
foo 1ms == 1s

// "got 15px"
foo "got " + 15px

0 comments on commit 0bbe983

Please sign in to comment.