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

fix($location/$browser): prevent infinite digests when only modifying the hash of a URL #10308

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ function Browser(window, document, $log, $sniffer) {
}
}

function getHash(url) {
var index = url.indexOf('#');
return index === -1 ? '' : url.substr(index + 1);
}

/**
* @private
* Note: this method is used only by scenario runner
Expand Down Expand Up @@ -190,8 +195,10 @@ function Browser(window, document, $log, $sniffer) {
}
if (replace) {
location.replace(url);
} else {
} else if (!sameBase) {
location.href = url;
} else {
location.hash = getHash(url);
}
}
return self;
Expand Down
27 changes: 18 additions & 9 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,25 @@ function LocationHashbangUrl(appBase, hashPrefix) {
*/
this.$$parse = function(url) {
var withoutBaseUrl = beginsWith(appBase, url) || beginsWith(appBaseNoFile, url);
var withoutHashUrl = withoutBaseUrl.charAt(0) == '#'
? beginsWith(hashPrefix, withoutBaseUrl)
: (this.$$html5)
? withoutBaseUrl
: '';

if (!isString(withoutHashUrl)) {
throw $locationMinErr('ihshprfx', 'Invalid url "{0}", missing hash prefix "{1}".', url,
hashPrefix);
var withoutHashUrl;

if (withoutBaseUrl.charAt(0) === '#') {

// The rest of the url starts with a hash so we have
// got either a hashbang path or a plain hash fragment
withoutHashUrl = beginsWith(hashPrefix, withoutBaseUrl);
if (isUndefined(withoutHashUrl)) {
// There was no hashbang prefix so we just have a hash fragment
withoutHashUrl = withoutBaseUrl;
}

} else {
// There was no hashbang path nor hash fragment:
// If we are in HTML5 mode we use what is left as the path;
// Otherwise we ignore what is left
withoutHashUrl = this.$$html5 ? withoutBaseUrl : '';
}

parseAppUrl(withoutHashUrl, this);

this.$$path = removeWindowsDriveName(this.$$path, withoutHashUrl, appBase);
Expand Down
21 changes: 21 additions & 0 deletions test/ng/browserSpecs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

/* global getHash:true, stripHash:true */

var historyEntriesLength;
var sniffer = {};

Expand Down Expand Up @@ -51,6 +53,12 @@ function MockWindow(options) {
mockWindow.history.state = null;
historyEntriesLength++;
},
get hash() {
return getHash(locationHref);
},
set hash(value) {
locationHref = stripHash(locationHref) + '#' + value;
},
replace: function(url) {
locationHref = url;
mockWindow.history.state = null;
Expand Down Expand Up @@ -550,6 +558,17 @@ describe('browser', function() {
expect(locationReplace).not.toHaveBeenCalled();
});

it("should retain the # character when the only change is clearing the hash fragment, to prevent page reload", function() {
sniffer.history = true;

browser.url('http://server/#123');
expect(fakeWindow.location.href).toEqual('http://server/#123');

browser.url('http://server/');
expect(fakeWindow.location.href).toEqual('http://server/#');

});

it('should use location.replace when history.replaceState not available', function() {
sniffer.history = false;
browser.url('http://new.org', true);
Expand All @@ -561,6 +580,7 @@ describe('browser', function() {
expect(fakeWindow.location.href).toEqual('http://server/');
});


it('should use location.replace and not use replaceState when the url only changed in the hash fragment to please IE10/11', function() {
sniffer.history = true;
browser.url('http://server/#123', true);
Expand All @@ -572,6 +592,7 @@ describe('browser', function() {
expect(fakeWindow.location.href).toEqual('http://server/');
});


it('should return $browser to allow chaining', function() {
expect(browser.url('http://any.com')).toBe(browser);
});
Expand Down
22 changes: 18 additions & 4 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,24 @@ describe('$location', function() {
});


it('should throw error when invalid hashbang prefix given', function() {
expect(function() {
url.$$parse('http://www.server.org:1234/base#/path');
}).toThrowMinErr('$location', 'ihshprfx', 'Invalid url "http://www.server.org:1234/base#/path", missing hash prefix "#!".');
it('should insert default hashbang if a hash is given with no hashbang prefix', function() {

url.$$parse('http://www.server.org:1234/base#/path');
expect(url.absUrl()).toBe('http://www.server.org:1234/base#!#%2Fpath');
expect(url.hash()).toBe('/path');
expect(url.path()).toBe('');

url.$$parse('http://www.server.org:1234/base#');
expect(url.absUrl()).toBe('http://www.server.org:1234/base');
expect(url.hash()).toBe('');
expect(url.path()).toBe('');
});

it('should ignore extra path segments if no hashbang is given', function() {
url.$$parse('http://www.server.org:1234/base/extra/path');
expect(url.absUrl()).toBe('http://www.server.org:1234/base');
expect(url.path()).toBe('');
expect(url.hash()).toBe('');
});


Expand Down