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 on empty hash changes #9903

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
@@ -1,6 +1,11 @@
'use strict';
/* global stripHash: true */

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

/**
* ! This is a private undocumented service !
*
Expand Down Expand Up @@ -191,8 +196,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
9 changes: 7 additions & 2 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ function stripHash(url) {
return index == -1 ? url : url.substr(0, index);
}

function trimEmptyHash(url) {
return url.replace(/#$/,'');
}


function stripFile(url) {
return url.substr(0, stripHash(url).lastIndexOf('/') + 1);
Expand Down Expand Up @@ -847,10 +851,11 @@ function $LocationProvider() {

// update browser
$rootScope.$watch(function $locationWatch() {
var oldUrl = $browser.url();
var oldUrl = trimEmptyHash($browser.url());
var newUrl = trimEmptyHash($location.absUrl());
var oldState = $browser.state();
var currentReplace = $location.$$replace;
var urlOrStateChanged = oldUrl !== $location.absUrl() ||
var urlOrStateChanged = oldUrl !== newUrl ||
($location.$$html5 && $sniffer.history && oldState !== $location.$$state);

if (initializing || urlOrStateChanged) {
Expand Down
8 changes: 8 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