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

Commit 7812dfc

Browse files
NevilleSpkozlowski-opensource
authored andcommitted
fix($location): allow empty string URLs to reset path, search, and hash
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 #10063 Closes #10064
1 parent 00b623e commit 7812dfc

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
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

+8
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ describe('$location', function() {
304304
expect(url.hash()).toBe('');
305305
});
306306

307+
it('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)