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

[nomerge ]fix($browser): detect changes to the browser url that happened in sync #8785

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
7 changes: 7 additions & 0 deletions src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ function Browser(window, document, $log, $sniffer) {
return callback;
};

/**
* Checks whether the url has changed outside of Angular.
* Needs to be exported to be able to check for changes that have been done in sync,
* as hashchange/popstate events fire in async.
*/
self.$$checkUrlChange = fireUrlChange;

//////////////////////////////////////////////////////////////
// Misc API
//////////////////////////////////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ function $RootScopeProvider(){
logIdx, logMsg, asyncTask;

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

lastDirtyWatch = null;

Expand Down
2 changes: 2 additions & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ angular.mock.$Browser = function() {
return listener;
};

self.$$checkUrlChange = angular.noop;

self.cookieHash = {};
self.lastCookieHash = {};
self.deferredFns = [];
Expand Down
37 changes: 33 additions & 4 deletions test/ng/browserSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function MockWindow() {
};

this.location = {
href: 'http://server',
href: 'http://server/',
replace: noop
};

Expand Down Expand Up @@ -419,7 +419,7 @@ describe('browser', function() {

expect(replaceState).not.toHaveBeenCalled();
expect(locationReplace).not.toHaveBeenCalled();
expect(fakeWindow.location.href).toEqual('http://server');
expect(fakeWindow.location.href).toEqual('http://server/');
});

it('should use history.replaceState when available', function() {
Expand All @@ -431,7 +431,7 @@ describe('browser', function() {

expect(pushState).not.toHaveBeenCalled();
expect(locationReplace).not.toHaveBeenCalled();
expect(fakeWindow.location.href).toEqual('http://server');
expect(fakeWindow.location.href).toEqual('http://server/');
});

it('should set location.href when pushState not available', function() {
Expand All @@ -453,7 +453,7 @@ describe('browser', function() {

expect(pushState).not.toHaveBeenCalled();
expect(replaceState).not.toHaveBeenCalled();
expect(fakeWindow.location.href).toEqual('http://server');
expect(fakeWindow.location.href).toEqual('http://server/');
});

it('should return $browser to allow chaining', function() {
Expand Down Expand Up @@ -587,6 +587,7 @@ describe('browser', function() {
fakeWindow.fire('hashchange');
expect(callback).not.toHaveBeenCalled();
});

});


Expand Down Expand Up @@ -620,4 +621,32 @@ describe('browser', function() {
expect(browser.baseHref()).toEqual('/base/path/');
});
});

describe('integration tests with $location', function() {

beforeEach(module(function($provide, $locationProvider) {
spyOn(fakeWindow.history, 'pushState').andCallFake(function(stateObj, title, newUrl) {
fakeWindow.location.href = newUrl;
})
$provide.value('$browser', browser);
browser.pollFns = [];

$locationProvider.html5Mode(true);
}));

it('should update $location when it was changed outside of Angular in sync '+
'before $digest was called', function() {
inject(function($rootScope, $location) {
fakeWindow.history.pushState(null, '', 'http://server/someTestHash');

// Verify that infinite digest reported in #6976 no longer occurs
expect(function() {
$rootScope.$digest();
}).not.toThrow();

expect($location.path()).toBe('/someTestHash');
});
});
});

});