From 187e0e209fe63592c93606a6e5f51dff358a30cc Mon Sep 17 00:00:00 2001 From: Dhruv Manek Date: Fri, 30 Sep 2011 16:44:08 -0700 Subject: [PATCH 1/3] fix(parser): Fix short circuit of logical AND and OR operators Closes #433 --- test/ParserSpec.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/ParserSpec.js b/test/ParserSpec.js index 4c3cb64b41e4..fb0f45220425 100644 --- a/test/ParserSpec.js +++ b/test/ParserSpec.js @@ -399,6 +399,22 @@ describe('parser', function() { expect(scope.obj[0].name).toEqual(1); }); + it('should short circuit AND operator', function(){ + var scope = angular.scope(); + scope.run = function(){ + throw "IT SHOULD NOT HAVE RUN"; + }; + expect(scope.$eval('false && run()')).toBe(false); + }); + + it('should short circuit OR operator', function(){ + var scope = angular.scope(); + scope.run = function(){ + throw "IT SHOULD NOT HAVE RUN"; + }; + expect(scope.$eval('true || run()')).toBe(true); + }); + describe('formatter', function(){ it('should return no argument function', function() { var noop = parser('noop').formatter()(); From a469ee302f86373e6e26baf038beec7ccb744345 Mon Sep 17 00:00:00 2001 From: Dhruv Manek Date: Fri, 30 Sep 2011 16:46:00 -0700 Subject: [PATCH 2/3] Revert "fix(parser): Fix short circuit of logical AND and OR operators" This reverts commit 187e0e209fe63592c93606a6e5f51dff358a30cc. --- test/ParserSpec.js | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/test/ParserSpec.js b/test/ParserSpec.js index fb0f45220425..4c3cb64b41e4 100644 --- a/test/ParserSpec.js +++ b/test/ParserSpec.js @@ -399,22 +399,6 @@ describe('parser', function() { expect(scope.obj[0].name).toEqual(1); }); - it('should short circuit AND operator', function(){ - var scope = angular.scope(); - scope.run = function(){ - throw "IT SHOULD NOT HAVE RUN"; - }; - expect(scope.$eval('false && run()')).toBe(false); - }); - - it('should short circuit OR operator', function(){ - var scope = angular.scope(); - scope.run = function(){ - throw "IT SHOULD NOT HAVE RUN"; - }; - expect(scope.$eval('true || run()')).toBe(true); - }); - describe('formatter', function(){ it('should return no argument function', function() { var noop = parser('noop').formatter()(); From b0e314b142def97840f42af3b5c64e51a021cc06 Mon Sep 17 00:00:00 2001 From: Dhruv Manek Date: Fri, 30 Sep 2011 16:55:01 -0700 Subject: [PATCH 3/3] fix(parser): Fix short circuit of logical AND and OR operators Closes #433 --- src/parser.js | 38 +++++++++++++++++++------------------- test/ParserSpec.js | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/parser.js b/src/parser.js index 5129f2f7ffad..f8978a0b3a89 100644 --- a/src/parser.js +++ b/src/parser.js @@ -5,25 +5,25 @@ var OPERATORS = { 'true':function(self){return true;}, 'false':function(self){return false;}, $undefined:noop, - '+':function(self, a,b){return (isDefined(a)?a:0)+(isDefined(b)?b:0);}, - '-':function(self, a,b){return (isDefined(a)?a:0)-(isDefined(b)?b:0);}, - '*':function(self, a,b){return a*b;}, - '/':function(self, a,b){return a/b;}, - '%':function(self, a,b){return a%b;}, - '^':function(self, a,b){return a^b;}, + '+':function(self, a,b){a=a(self); b=b(self); return (isDefined(a)?a:0)+(isDefined(b)?b:0);}, + '-':function(self, a,b){a=a(self); b=b(self); return (isDefined(a)?a:0)-(isDefined(b)?b:0);}, + '*':function(self, a,b){return a(self)*b(self);}, + '/':function(self, a,b){return a(self)/b(self);}, + '%':function(self, a,b){return a(self)%b(self);}, + '^':function(self, a,b){return a(self)^b(self);}, '=':noop, - '==':function(self, a,b){return a==b;}, - '!=':function(self, a,b){return a!=b;}, - '<':function(self, a,b){return a':function(self, a,b){return a>b;}, - '<=':function(self, a,b){return a<=b;}, - '>=':function(self, a,b){return a>=b;}, - '&&':function(self, a,b){return a&&b;}, - '||':function(self, a,b){return a||b;}, - '&':function(self, a,b){return a&b;}, + '==':function(self, a,b){return a(self)==b(self);}, + '!=':function(self, a,b){return a(self)!=b(self);}, + '<':function(self, a,b){return a(self)':function(self, a,b){return a(self)>b(self);}, + '<=':function(self, a,b){return a(self)<=b(self);}, + '>=':function(self, a,b){return a(self)>=b(self);}, + '&&':function(self, a,b){return a(self)&&b(self);}, + '||':function(self, a,b){return a(self)||b(self);}, + '&':function(self, a,b){return a(self)&b(self);}, // '|':function(self, a,b){return a|b;}, - '|':function(self, a,b){return b(self, a);}, - '!':function(self, a){return !a;} + '|':function(self, a,b){return b(self)(self, a(self));}, + '!':function(self, a){return !a(self);} }; var ESCAPE = {"n":"\n", "f":"\f", "r":"\r", "t":"\t", "v":"\v", "'":"'", '"':'"'}; @@ -308,13 +308,13 @@ function parser(text, json){ function unaryFn(fn, right) { return function(self) { - return fn(self, right(self)); + return fn(self, right); }; } function binaryFn(left, fn, right) { return function(self) { - return fn(self, left(self), right(self)); + return fn(self, left, right); }; } diff --git a/test/ParserSpec.js b/test/ParserSpec.js index 4c3cb64b41e4..fb0f45220425 100644 --- a/test/ParserSpec.js +++ b/test/ParserSpec.js @@ -399,6 +399,22 @@ describe('parser', function() { expect(scope.obj[0].name).toEqual(1); }); + it('should short circuit AND operator', function(){ + var scope = angular.scope(); + scope.run = function(){ + throw "IT SHOULD NOT HAVE RUN"; + }; + expect(scope.$eval('false && run()')).toBe(false); + }); + + it('should short circuit OR operator', function(){ + var scope = angular.scope(); + scope.run = function(){ + throw "IT SHOULD NOT HAVE RUN"; + }; + expect(scope.$eval('true || run()')).toBe(true); + }); + describe('formatter', function(){ it('should return no argument function', function() { var noop = parser('noop').formatter()();