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

fix($location): strip off empty hash segments when comparing #10748

Closed
wants to merge 1 commit into from
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: 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(/(#.+)|#$/, '$1');
}


function stripFile(url) {
return url.substr(0, stripHash(url).lastIndexOf('/') + 1);
Expand Down Expand Up @@ -724,10 +728,11 @@ function $LocationProvider(){
// update browser
var changeCounter = 0;
$rootScope.$watch(function $locationWatch() {
var oldUrl = $browser.url();
var oldUrl = trimEmptyHash($browser.url());
var newUrl = trimEmptyHash($location.absUrl());
var currentReplace = $location.$$replace;

if (!changeCounter || oldUrl != $location.absUrl()) {
if (!changeCounter || oldUrl != newUrl) {
changeCounter++;
$rootScope.$evalAsync(function() {
if ($rootScope.$broadcast('$locationChangeStart', $location.absUrl(), oldUrl).
Expand Down
14 changes: 14 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,20 @@ describe('$location', function() {
};
}

describe('location watch', function() {
beforeEach(initService({supportHistory: true}));
beforeEach(inject(initBrowser({url:'http://new.com/a/b#'})));

it('should not update browser if only the empty hash fragment is cleared by updating the search', inject(function($rootScope, $browser, $location) {
$rootScope.$digest();

$browser.url('http://new.com/a/b#');
var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
$rootScope.$digest();
expect($browserUrl).not.toHaveBeenCalled();
}));
});

describe('wiring', function() {

beforeEach(initService({html5Mode:false,hashPrefix: '!',supportHistory: true}));
Expand Down