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

Commit 2ece4d0

Browse files
committed
fix($browser): detect changes to the browser url that happened in sync
Closes #6976.
1 parent 1812af5 commit 2ece4d0

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

src/ng/browser.js

+7
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ function Browser(window, document, $log, $sniffer) {
234234
return callback;
235235
};
236236

237+
/**
238+
* Checks whether the url has changed outside of Angular.
239+
* Needs to be exported to be able to check for changes that have been done in sync,
240+
* as hashchange/popstate events fire in async.
241+
*/
242+
self.$$checkUrlChange = fireUrlChange;
243+
237244
//////////////////////////////////////////////////////////////
238245
// Misc API
239246
//////////////////////////////////////////////////////////////

src/ng/rootScope.js

+2
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,8 @@ function $RootScopeProvider(){
608608
logIdx, logMsg, asyncTask;
609609

610610
beginPhase('$digest');
611+
// Check for changes to browser url that happened in sync before the call to $digest
612+
$browser.$$checkUrlChange();
611613

612614
lastDirtyWatch = null;
613615

src/ngMock/angular-mocks.js

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ angular.mock.$Browser = function() {
5656
return listener;
5757
};
5858

59+
self.$$checkUrlChange = angular.noop;
60+
5961
self.cookieHash = {};
6062
self.lastCookieHash = {};
6163
self.deferredFns = [];

test/ng/browserSpecs.js

+32-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function MockWindow() {
3636
};
3737

3838
this.location = {
39-
href: 'http://server',
39+
href: 'http://server/',
4040
replace: noop
4141
};
4242

@@ -414,7 +414,7 @@ describe('browser', function() {
414414

415415
expect(replaceState).not.toHaveBeenCalled();
416416
expect(locationReplace).not.toHaveBeenCalled();
417-
expect(fakeWindow.location.href).toEqual('http://server');
417+
expect(fakeWindow.location.href).toEqual('http://server/');
418418
});
419419

420420
it('should use history.replaceState when available', function() {
@@ -426,7 +426,7 @@ describe('browser', function() {
426426

427427
expect(pushState).not.toHaveBeenCalled();
428428
expect(locationReplace).not.toHaveBeenCalled();
429-
expect(fakeWindow.location.href).toEqual('http://server');
429+
expect(fakeWindow.location.href).toEqual('http://server/');
430430
});
431431

432432
it('should set location.href when pushState not available', function() {
@@ -448,7 +448,7 @@ describe('browser', function() {
448448

449449
expect(pushState).not.toHaveBeenCalled();
450450
expect(replaceState).not.toHaveBeenCalled();
451-
expect(fakeWindow.location.href).toEqual('http://server');
451+
expect(fakeWindow.location.href).toEqual('http://server/');
452452
});
453453

454454
it('should return $browser to allow chaining', function() {
@@ -615,4 +615,32 @@ describe('browser', function() {
615615
expect(browser.baseHref()).toEqual('/base/path/');
616616
});
617617
});
618+
619+
describe('integration tests with $location', function() {
620+
621+
beforeEach(module(function($provide, $locationProvider) {
622+
spyOn(fakeWindow.history, 'pushState').andCallFake(function(stateObj, title, newUrl) {
623+
fakeWindow.location.href = newUrl;
624+
});
625+
$provide.value('$browser', browser);
626+
browser.pollFns = [];
627+
628+
$locationProvider.html5Mode(true);
629+
}));
630+
631+
it('should update $location when it was changed outside of Angular in sync '+
632+
'before $digest was called', function() {
633+
inject(function($rootScope, $location) {
634+
fakeWindow.history.pushState(null, '', 'http://server/someTestHash');
635+
636+
// Verify that infinite digest reported in #6976 no longer occurs
637+
expect(function() {
638+
$rootScope.$digest();
639+
}).not.toThrow();
640+
641+
expect($location.path()).toBe('/someTestHash');
642+
});
643+
});
644+
});
645+
618646
});

0 commit comments

Comments
 (0)