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

Commit 6cab4ad

Browse files
committed
fixup 1
1 parent 87ace90 commit 6cab4ad

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

src/ng/browser.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ function Browser(window, document, $log, $sniffer) {
8787
var cachedState, lastHistoryState,
8888
lastBrowserUrl = location.href,
8989
baseElement = document.find('base'),
90-
pendingLocation = null;
90+
pendingLocation = null,
91+
getCurrentState = !$sniffer.history ? noop : function getCurrentState() {
92+
try {
93+
return history.state;
94+
} catch (e) {
95+
// MSIE can reportedly throw when there is no state (UNCONFIRMED).
96+
}
97+
};
9198

9299
cacheState();
93100
lastHistoryState = cachedState;
@@ -195,14 +202,6 @@ function Browser(window, document, $log, $sniffer) {
195202
fireUrlChange();
196203
}
197204

198-
function getCurrentState() {
199-
try {
200-
return history.state;
201-
} catch (e) {
202-
// MSIE can reportedly throw when there is no state (UNCONFIRMED).
203-
}
204-
}
205-
206205
// This variable should be used *only* inside the cacheState function.
207206
var lastCachedState = null;
208207
function cacheState() {

src/ng/sniffer.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
function $SnifferProvider() {
1818
this.$get = ['$window', '$document', function($window, $document) {
1919
var eventSupport = {},
20+
// Chrome Packaged Apps are not allowed to access `history.pushState`. They can be detected by
21+
// the presence of `chrome.app.runtime` (see https://developer.chrome.com/apps/api_index)
2022
isChromePackagedApp = $window.chrome && $window.chrome.app && $window.chrome.app.runtime,
21-
hasHistoryPushState = $window.history && $window.history.pushState,
23+
hasHistoryPushState = !isChromePackagedApp && $window.history && $window.history.pushState,
2224
android =
2325
toInt((/android (\d+)/.exec(lowercase(($window.navigator || {}).userAgent)) || [])[1]),
2426
boxee = /Boxee/i.test(($window.navigator || {}).userAgent),
@@ -59,13 +61,11 @@ function $SnifferProvider() {
5961
// http://code.google.com/p/android/issues/detail?id=17471
6062
// https://github.com/angular/angular.js/issues/904
6163

62-
// Chrome Packaged Apps are not allowed to access `history.pushState`. They can be detected by
63-
// the presence of `chrome.app.runtime` (see https://developer.chrome.com/apps/api_index)
6464
// older webkit browser (533.9) on Boxee box has exactly the same problem as Android has
6565
// so let's not use the history API also
6666
// We are purposefully using `!(android < 4)` to cover the case when `android` is undefined
6767
// jshint -W018
68-
history: !!(!isChromePackagedApp && hasHistoryPushState && !(android < 4) && !boxee),
68+
history: !!(hasHistoryPushState && !(android < 4) && !boxee),
6969
// jshint +W018
7070
hasEvent: function(event) {
7171
// IE9 implements 'input' event it's so fubared that we rather pretend that it doesn't have

test/ng/browserSpecs.js

+23
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,32 @@ describe('browser', function() {
537537
currentHref = fakeWindow.location.href;
538538
});
539539

540+
it('should not access `history.state` when `$sniffer.history` is false', function() {
541+
// In the context of a Chrome Packaged App, although `history.state` is present, accessing it
542+
// is not allowed and logs an error in the console. We should not try to access
543+
// `history.state` in contexts where `$sniffer.history` is false.
544+
545+
var historyStateAccessed = false;
546+
var mockSniffer = {histroy: false};
547+
var mockWindow = new MockWindow();
548+
549+
var _state = mockWindow.history.state;
550+
Object.defineProperty(mockWindow.history, 'state', {
551+
get: function() {
552+
historyStateAccessed = true;
553+
return _state;
554+
}
555+
});
556+
557+
var browser = new Browser(mockWindow, fakeDocument, fakeLog, mockSniffer);
558+
559+
expect(historyStateAccessed).toBe(false);
560+
});
561+
540562
describe('in IE', runTests({msie: true}));
541563
describe('not in IE', runTests({msie: false}));
542564

565+
543566
function runTests(options) {
544567
return function() {
545568
beforeEach(function() {

test/ng/snifferSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ describe('$sniffer', function() {
6969
return mockWindow;
7070
}
7171
});
72+
73+
74+
it('should not try to access `history.pushState` in Chrome Packaged Apps', function() {
75+
var pushStateAccessCount = 0;
76+
77+
var mockHistory = Object.create(Object.prototype, {
78+
pushState: {get: function() { pushStateAccessCount++; return noop; }},
79+
});
80+
var mockWindow = {
81+
chrome: {
82+
app: {
83+
runtime: {}
84+
}
85+
},
86+
history: mockHistory
87+
};
88+
89+
sniffer(mockWindow);
90+
91+
expect(pushStateAccessCount).toBe(0);
92+
});
7293
});
7394

7495

0 commit comments

Comments
 (0)