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

Commit 705c9d9

Browse files
committed
fix(location): fix parameter handling on search()
1 parent 61906d3 commit 705c9d9

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

docs/content/error/location/wpt.ngdoc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@ngdoc error
2+
@name $location:wpt
3+
@fullName Wrong parameter type
4+
@description

src/ng/location.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -353,17 +353,24 @@ LocationHashbangInHtml5Url.prototype =
353353
* @return {string} search
354354
*/
355355
search: function(search, paramValue) {
356-
if (isUndefined(search))
357-
return this.$$search;
358-
359-
if (isDefined(paramValue)) {
360-
if (paramValue === null) {
361-
delete this.$$search[search];
362-
} else {
363-
this.$$search[search] = paramValue;
364-
}
365-
} else {
366-
this.$$search = isString(search) ? parseKeyValue(search) : search;
356+
switch (arguments.length) {
357+
case 0:
358+
return this.$$search;
359+
case 1:
360+
if (isString(search)) {
361+
this.$$search = parseKeyValue(search);
362+
} else if (isObject(search)) {
363+
this.$$search = search;
364+
} else {
365+
throw $locationMinErr('wpt', 'First parameter of function must be string or an object.');
366+
}
367+
break;
368+
default:
369+
if (paramValue == undefined || paramValue == null) {
370+
delete this.$$search[search];
371+
} else {
372+
this.$$search[search] = paramValue;
373+
}
367374
}
368375

369376
this.$$compose();

test/ng/locationSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,35 @@ describe('$location', function() {
6666
});
6767

6868

69+
it('search() should handle multiple value', function() {
70+
url.search('a&b');
71+
expect(url.search()).toEqual({a: true, b: true});
72+
73+
url.search('a', null);
74+
75+
expect(url.search()).toEqual({b: true});
76+
77+
url.search('b', undefined);
78+
expect(url.search()).toEqual({});
79+
});
80+
81+
82+
it('search() should handle single value', function() {
83+
url.search('ignore');
84+
expect(url.search()).toEqual({ignore: true});
85+
});
86+
87+
88+
it('search() should throw error an incorrect argument', function() {
89+
expect(function() {
90+
url.search(null);
91+
}).toThrow('[$location:wpt] First parameter of function must be string or an object.');
92+
expect(function() {
93+
url.search(undefined);
94+
}).toThrow('[$location:wpt] First parameter of function must be string or an object.');
95+
});
96+
97+
6998
it('hash() should change hash fragment', function() {
7099
url.hash('new-hash');
71100
expect(url.hash()).toBe('new-hash');

0 commit comments

Comments
 (0)