Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit adb5c6d

Browse files
pkozlowski-opensourcejeffbcross
authored andcommitted
fix($location): allow numeric location setter arguments
Fixes #7054
1 parent b01a03c commit adb5c6d

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/ng/location.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,11 @@ LocationHashbangInHtml5Url.prototype =
413413
* Note: Path should always begin with forward slash (/), this method will add the forward slash
414414
* if it is missing.
415415
*
416-
* @param {string=} path New path
416+
* @param {(string|number)=} path New path
417417
* @return {string} path
418418
*/
419419
path: locationGetterSetter('$$path', function(path) {
420+
path = path.toString();
420421
return path.charAt(0) == '/' ? path : '/' + path;
421422
}),
422423

@@ -471,7 +472,8 @@ LocationHashbangInHtml5Url.prototype =
471472
case 0:
472473
return this.$$search;
473474
case 1:
474-
if (isString(search)) {
475+
if (isString(search) || isNumber(search)) {
476+
search = search.toString();
475477
this.$$search = parseKeyValue(search);
476478
} else if (isObject(search)) {
477479
// remove object undefined or null properties
@@ -508,10 +510,12 @@ LocationHashbangInHtml5Url.prototype =
508510
*
509511
* Change hash fragment when called with parameter and return `$location`.
510512
*
511-
* @param {string=} hash New hash fragment
513+
* @param {(string|number)=} hash New hash fragment
512514
* @return {string} hash
513515
*/
514-
hash: locationGetterSetter('$$hash', identity),
516+
hash: locationGetterSetter('$$hash', function(hash) {
517+
return hash.toString();
518+
}),
515519

516520
/**
517521
* @ngdoc method

test/ng/locationSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ describe('$location', function() {
136136
expect(url.absUrl()).toBe('http://www.domain.com:9877/new/path?search=a&b=c&d#hash');
137137
});
138138

139+
it('path() should not break on numeric values', function() {
140+
url.path(1);
141+
expect(url.path()).toBe('/1');
142+
expect(url.absUrl()).toBe('http://www.domain.com:9877/1?search=a&b=c&d#hash');
143+
});
139144

140145
it('search() should accept string', function() {
141146
url.search('x=y&c');
@@ -176,6 +181,13 @@ describe('$location', function() {
176181
});
177182

178183

184+
it('search() should accept numeric keys', function() {
185+
url.search({1: 'one', 2: 'two'});
186+
expect(url.search()).toEqual({'1': 'one', '2': 'two'});
187+
expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?1=one&2=two#hash');
188+
});
189+
190+
179191
it('search() should handle multiple value', function() {
180192
url.search('a&b');
181193
expect(url.search()).toEqual({a: true, b: true});
@@ -192,6 +204,8 @@ describe('$location', function() {
192204
it('search() should handle single value', function() {
193205
url.search('ignore');
194206
expect(url.search()).toEqual({ignore: true});
207+
url.search(1);
208+
expect(url.search()).toEqual({1: true});
195209
});
196210

197211

@@ -212,6 +226,13 @@ describe('$location', function() {
212226
});
213227

214228

229+
it('hash() should accept numeric parameter', function() {
230+
url.hash(5);
231+
expect(url.hash()).toBe('5');
232+
expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#5');
233+
});
234+
235+
215236
it('url() should change the path, search and hash', function() {
216237
url.url('/some/path?a=b&c=d#hhh');
217238
expect(url.url()).toBe('/some/path?a=b&c=d#hhh');

0 commit comments

Comments
 (0)