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

Commit

Permalink
Fixing issue #98 (infinite loop when location hash set empty)
Browse files Browse the repository at this point in the history
Added tests and fixed the issue.

Closes #98
  • Loading branch information
vojtajina authored and IgorMinar committed Nov 1, 2010
1 parent ba5f8ee commit 99f2505
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/services.js
Expand Up @@ -16,20 +16,24 @@ angularServiceInject("$document", function(window){
angularServiceInject("$location", function(browser) {
var scope = this,
location = {toString:toString, update:update, updateHash: updateHash},
lastLocationHref = browser.getUrl(),
lastBrowserUrl = browser.getUrl(),
lastLocationHref,
lastLocationHash;

browser.addPollFn(function(){
if (lastLocationHref !== browser.getUrl()) {
update(lastLocationHref = browser.getUrl());
browser.addPollFn(function() {
if (lastBrowserUrl != browser.getUrl()) {
update(lastBrowserUrl = browser.getUrl());
lastLocationHref = location.href;
lastLocationHash = location.hash;
scope.$eval();
}
});

this.$onEval(PRIORITY_FIRST, updateBrowser);
this.$onEval(PRIORITY_LAST, updateBrowser);

update(lastLocationHref);
update(lastBrowserUrl);
lastLocationHref = location.href;
lastLocationHash = location.hash;

return location;
Expand Down Expand Up @@ -137,8 +141,9 @@ angularServiceInject("$location", function(browser) {
function updateBrowser() {
updateLocation();

if (location.href != lastLocationHref) {
browser.setUrl(lastLocationHref = location.href);
if (location.href != lastLocationHref) {
browser.setUrl(lastBrowserUrl = location.href);
lastLocationHref = location.href;
lastLocationHash = location.hash;
}
}
Expand Down Expand Up @@ -180,7 +185,7 @@ angularServiceInject("$location", function(browser) {
var match = URL_MATCH.exec(href);

if (match) {
loc.href = href.replace('#$', '');
loc.href = href.replace(/#$/, '');
loc.protocol = match[1];
loc.host = match[3] || '';
loc.port = match[5] || DEFAULT_PORTS[loc.protocol] || _null;
Expand Down
12 changes: 12 additions & 0 deletions test/servicesSpec.js
Expand Up @@ -207,6 +207,18 @@ describe("service", function(){
expect(scope.$location.hashSearch).toEqual({a: 'b'});
expect(scope.$location.hashPath).toEqual('path');
});

it('should remove # if hash is empty', function() {
scope.$location.update('http://www.angularjs.org/index.php#');
expect(scope.$location.href).toEqual('http://www.angularjs.org/index.php');
});

it('should not change browser\'s url with empty hash', function() {
$browser.setUrl('http://www.angularjs.org/index.php#');
spyOn($browser, 'setUrl');
$browser.poll();
expect($browser.setUrl).not.toHaveBeenCalled();
});
});

describe("$invalidWidgets", function(){
Expand Down

0 comments on commit 99f2505

Please sign in to comment.