From 26d43cacdc106765bd928d41600352198f887aef Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Wed, 18 Dec 2013 23:52:31 -0500 Subject: fix($parse): return 'undefined' if a middle key's value is null Prior to this fix, $parse/$eval would return 'null' if a middle key in an expression's value is null, when it should be expected to be undefined. This patch tries to remedy this by returning undefined for middle values in expressions, when fetching a child of that null value. For example: ```js // Given the following object: $scope.a = { b: null }; // $scope.$eval('a.b.c') returns undefined, whereas previously it would return null ``` Closes #5480 --- test/ng/parseSpec.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'test/ng') diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js index 5e985f1f..4599c033 100644 --- a/test/ng/parseSpec.js +++ b/test/ng/parseSpec.js @@ -977,6 +977,55 @@ describe('parser', function() { expect($parse('"name" + id').constant).toBe(false); })); }); + + describe('nulls in expressions', function() { + // simpleGetterFn1 + it('should return null for `a` where `a` is null', inject(function($rootScope) { + $rootScope.a = null; + expect($rootScope.$eval('a')).toBe(null); + })); + + it('should return undefined for `a` where `a` is undefined', inject(function($rootScope) { + expect($rootScope.$eval('a')).toBeUndefined(); + })); + + // simpleGetterFn2 + it('should return undefined for properties of `null` constant', inject(function($rootScope) { + expect($rootScope.$eval('null.a')).toBeUndefined(); + })); + + it('should return undefined for properties of `null` values', inject(function($rootScope) { + $rootScope.a = null; + expect($rootScope.$eval('a.b')).toBeUndefined(); + })); + + it('should return null for `a.b` where `b` is null', inject(function($rootScope) { + $rootScope.a = { b: null }; + expect($rootScope.$eval('a.b')).toBe(null); + })); + + // cspSafeGetter && pathKeys.length < 6 || pathKeys.length > 2 + it('should return null for `a.b.c.d.e` where `e` is null', inject(function($rootScope) { + $rootScope.a = { b: { c: { d: { e: null } } } }; + expect($rootScope.$eval('a.b.c.d.e')).toBe(null); + })); + + it('should return undefined for `a.b.c.d.e` where `d` is null', inject(function($rootScope) { + $rootScope.a = { b: { c: { d: null } } }; + expect($rootScope.$eval('a.b.c.d.e')).toBeUndefined(); + })); + + // cspSafeGetter || pathKeys.length > 6 + it('should return null for `a.b.c.d.e.f.g` where `g` is null', inject(function($rootScope) { + $rootScope.a = { b: { c: { d: { e: { f: { g: null } } } } } }; + expect($rootScope.$eval('a.b.c.d.e.f.g')).toBe(null); + })); + + it('should return undefined for `a.b.c.d.e.f.g` where `f` is null', inject(function($rootScope) { + $rootScope.a = { b: { c: { d: { e: { f: null } } } } }; + expect($rootScope.$eval('a.b.c.d.e.f.g')).toBeUndefined(); + })); + }); }); }); }); -- cgit v1.2.3