From c12e8d4665b635ba6b09d12802efb88d38b7ad5c Mon Sep 17 00:00:00 2001 From: Jeff Cross Date: Thu, 4 Sep 2014 17:16:11 -0700 Subject: [PATCH] fix($location): don't call toString on null values --- src/ng/location.js | 4 ++-- test/ng/locationSpec.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/ng/location.js b/src/ng/location.js index 54b21aa0..4fa37b44 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -391,7 +391,7 @@ LocationHashbangInHtml5Url.prototype = * @return {string} path */ path: locationGetterSetter('$$path', function(path) { - path = path.toString(); + path = path ? path.toString() : ''; return path.charAt(0) == '/' ? path : '/' + path; }), @@ -488,7 +488,7 @@ LocationHashbangInHtml5Url.prototype = * @return {string} hash */ hash: locationGetterSetter('$$hash', function(hash) { - return hash.toString(); + return hash ? hash.toString() : ''; }), /** diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 3aec114b..b7c656c7 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -93,6 +93,14 @@ describe('$location', function() { expect(url.absUrl()).toBe('http://www.domain.com:9877/1?search=a&b=c&d#hash'); }); + + it('path() should set to empty path on null value', function () { + url.path('/foo'); + expect(url.path()).toBe('/foo'); + url.path(null); + expect(url.path()).toBe('/'); + }); + it('search() should accept string', function() { url.search('x=y&c'); expect(url.search()).toEqual({x: 'y', c: true}); @@ -184,6 +192,13 @@ describe('$location', function() { }); + it('hash() should accept null parameter', function() { + url.hash(null); + expect(url.hash()).toBe(''); + expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d'); + }); + + it('url() should change the path, search and hash', function() { url.url('/some/path?a=b&c=d#hhh'); expect(url.url()).toBe('/some/path?a=b&c=d#hhh');