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

Commit 5279de0

Browse files
vojtajinamhevery
authored andcommitted
fix($location): links without path segment should not change the path
Closes #648
1 parent 8fe77b6 commit 5279de0

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/service/location.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ LocationUrl.prototype = LocationHashbangUrl.prototype = {
238238
return this.$$url;
239239

240240
var match = PATH_MATCH.exec(url);
241-
this.path(decodeURIComponent(match[1] || '')).search(match[3] || '')
242-
.hash(match[5] || '', replace);
241+
if (match[1]) this.path(decodeURIComponent(match[1]));
242+
if (match[2] || match[1]) this.search(match[3] || '');
243+
this.hash(match[5] || '', replace);
243244

244245
return this;
245246
},

test/service/locationSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ describe('$location', function() {
9595
});
9696

9797

98+
it('url() should change only hash when no search and path specified', function() {
99+
url.url('#some-hash');
100+
101+
expect(url.hash()).toBe('some-hash');
102+
expect(url.url()).toBe('/path/b?search=a&b=c&d#some-hash');
103+
expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#some-hash');
104+
});
105+
106+
107+
it('url() should change only search and hash when no path specified', function() {
108+
url.url('?a=b');
109+
110+
expect(url.search()).toEqual({a: 'b'});
111+
expect(url.hash()).toBe('');
112+
expect(url.path()).toBe('/path/b');
113+
});
114+
115+
116+
it('url() should reset search and hash when only path specified', function() {
117+
url.url('/new/path');
118+
119+
expect(url.path()).toBe('/new/path');
120+
expect(url.search()).toEqual({});
121+
expect(url.hash()).toBe('');
122+
});
123+
124+
98125
it('replace should set $$replace flag and return itself', function() {
99126
expect(url.$$replace).toBe(false);
100127

0 commit comments

Comments
 (0)