From adb5c6d6cc76b928436743707727ab0974d6810b Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Sat, 19 Apr 2014 10:50:54 +0200 Subject: [PATCH] fix($location): allow numeric location setter arguments Fixes #7054 --- src/ng/location.js | 12 ++++++++---- test/ng/locationSpec.js | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/ng/location.js b/src/ng/location.js index 2afd68818f60..5c1f87cd81a7 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -413,10 +413,11 @@ LocationHashbangInHtml5Url.prototype = * Note: Path should always begin with forward slash (/), this method will add the forward slash * if it is missing. * - * @param {string=} path New path + * @param {(string|number)=} path New path * @return {string} path */ path: locationGetterSetter('$$path', function(path) { + path = path.toString(); return path.charAt(0) == '/' ? path : '/' + path; }), @@ -471,7 +472,8 @@ LocationHashbangInHtml5Url.prototype = case 0: return this.$$search; case 1: - if (isString(search)) { + if (isString(search) || isNumber(search)) { + search = search.toString(); this.$$search = parseKeyValue(search); } else if (isObject(search)) { // remove object undefined or null properties @@ -508,10 +510,12 @@ LocationHashbangInHtml5Url.prototype = * * Change hash fragment when called with parameter and return `$location`. * - * @param {string=} hash New hash fragment + * @param {(string|number)=} hash New hash fragment * @return {string} hash */ - hash: locationGetterSetter('$$hash', identity), + hash: locationGetterSetter('$$hash', function(hash) { + return hash.toString(); + }), /** * @ngdoc method diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 1475f9f9e063..ccdd3dec09e7 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -136,6 +136,11 @@ describe('$location', function() { expect(url.absUrl()).toBe('http://www.domain.com:9877/new/path?search=a&b=c&d#hash'); }); + it('path() should not break on numeric values', function() { + url.path(1); + expect(url.path()).toBe('/1'); + expect(url.absUrl()).toBe('http://www.domain.com:9877/1?search=a&b=c&d#hash'); + }); it('search() should accept string', function() { url.search('x=y&c'); @@ -176,6 +181,13 @@ describe('$location', function() { }); + it('search() should accept numeric keys', function() { + url.search({1: 'one', 2: 'two'}); + expect(url.search()).toEqual({'1': 'one', '2': 'two'}); + expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?1=one&2=two#hash'); + }); + + it('search() should handle multiple value', function() { url.search('a&b'); expect(url.search()).toEqual({a: true, b: true}); @@ -192,6 +204,8 @@ describe('$location', function() { it('search() should handle single value', function() { url.search('ignore'); expect(url.search()).toEqual({ignore: true}); + url.search(1); + expect(url.search()).toEqual({1: true}); }); @@ -212,6 +226,13 @@ describe('$location', function() { }); + it('hash() should accept numeric parameter', function() { + url.hash(5); + expect(url.hash()).toBe('5'); + expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#5'); + }); + + 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');