Skip to content

Commit d354adf

Browse files
committed
fix($location): reset path, search, and hash when given an empty string URL
Currently, providing '' to $location#url will only reset the hash, but otherwise has no effect. This change brings the behaviour of $location#url more inline with window.location.href, which when assigned to an empty string loads the page's base href. Before: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com/path After: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com Fixes angular#10063
1 parent d81ff88 commit d354adf

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/ng/location.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ var locationPrototype = {
350350
return this.$$url;
351351

352352
var match = PATH_MATCH.exec(url);
353-
if (match[1]) this.path(decodeURIComponent(match[1]));
354-
if (match[2] || match[1]) this.search(match[3] || '');
353+
if (match[1] || url === '') this.path(decodeURIComponent(match[1]));
354+
if (match[2] || match[1] || url === '') this.search(match[3] || '');
355355
this.hash(match[5] || '');
356356

357357
return this;

test/ng/locationSpec.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ describe('$location', function() {
268268
});
269269

270270

271-
it('url() should change the path, search and hash', function() {
271+
iit('url() should change the path, search and hash', function() {
272272
url.url('/some/path?a=b&c=d#hhh');
273273
expect(url.url()).toBe('/some/path?a=b&c=d#hhh');
274274
expect(url.absUrl()).toBe('http://www.domain.com:9877/some/path?a=b&c=d#hhh');
@@ -278,7 +278,7 @@ describe('$location', function() {
278278
});
279279

280280

281-
it('url() should change only hash when no search and path specified', function() {
281+
iit('url() should change only hash when no search and path specified', function() {
282282
url.url('#some-hash');
283283

284284
expect(url.hash()).toBe('some-hash');
@@ -287,7 +287,7 @@ describe('$location', function() {
287287
});
288288

289289

290-
it('url() should change only search and hash when no path specified', function() {
290+
iit('url() should change only search and hash when no path specified', function() {
291291
url.url('?a=b');
292292

293293
expect(url.search()).toEqual({a: 'b'});
@@ -296,14 +296,22 @@ describe('$location', function() {
296296
});
297297

298298

299-
it('url() should reset search and hash when only path specified', function() {
299+
iit('url() should reset search and hash when only path specified', function() {
300300
url.url('/new/path');
301301

302302
expect(url.path()).toBe('/new/path');
303303
expect(url.search()).toEqual({});
304304
expect(url.hash()).toBe('');
305305
});
306306

307+
iit('url() should change path when empty string specified', function() {
308+
url.url('');
309+
310+
expect(url.path()).toBe('/');
311+
expect(url.search()).toEqual({});
312+
expect(url.hash()).toBe('');
313+
});
314+
307315

308316
it('replace should set $$replace flag and return itself', function() {
309317
expect(url.$$replace).toBe(false);

0 commit comments

Comments
 (0)