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

fix($browser/$location): add a workaround of iOS9 regression in UIWeb… #12814

Closed
wants to merge 2 commits 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
37 changes: 36 additions & 1 deletion src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,41 @@ function Browser(window, document, $log, $sniffer) {
function $BrowserProvider() {
this.$get = ['$window', '$log', '$sniffer', '$document',
function($window, $log, $sniffer, $document) {
return new Browser($window, $document, $log, $sniffer);
var browser = new Browser($window, $document, $log, $sniffer);

if (isIOS9UIWebView($window.navigator.userAgent)) {
browser = applyIOS9Shim(browser);
}

return browser;
}];
}


function isIOS9UIWebView(userAgent) {
return /(iPhone|iPad|iPod) OS 9_\d/.test(userAgent) && !/Version\/9\./.test(userAgent);
}


function applyIOS9Shim(browser) {
var pendingLocationUrl = null;
var patchedBrowser = Object.create(browser);

patchedBrowser.url = function() {
if (arguments.length) {
pendingLocationUrl = arguments[0];
return browser.url.apply(patchedBrowser, arguments);
}

return pendingLocationUrl || browser.url();
};

jqLite(window).on('popstate', clearPendingLocationUrl);
jqLite(window).on('hashchange', clearPendingLocationUrl);

function clearPendingLocationUrl() {
pendingLocationUrl = null;
}

return patchedBrowser;
}
19 changes: 18 additions & 1 deletion test/ng/browserSpecs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

/* global getHash:true, stripHash:true */
/* global getHash:true, stripHash:true, isIOS9UIWebView:true */

var historyEntriesLength;
var sniffer = {};
Expand Down Expand Up @@ -852,4 +852,21 @@ describe('browser', function() {

});


describe('iOS9 UIWebView workarounds', function() {

describe('userAgent sniffing', function() {

it('should identify iOS9 + UIWebView', function() {
var userAgentString = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13A4325c (358330912)';
expect(isIOS9UIWebView(userAgentString)).toBe(true);
});

it('should ignore iOS9 + WKWebView', function() {
var userAgentString = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13A4325c Safari/601.1';
expect(isIOS9UIWebView(userAgentString)).toBe(false);
});
});
});

});
5 changes: 4 additions & 1 deletion test/ng/rafSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ describe('$$rAF', function() {
location: window.location,
history: window.history,
webkitRequestAnimationFrame: jasmine.createSpy('$window.webkitRequestAnimationFrame'),
webkitCancelRequestAnimationFrame: jasmine.createSpy('$window.webkitCancelRequestAnimationFrame')
webkitCancelRequestAnimationFrame: jasmine.createSpy('$window.webkitCancelRequestAnimationFrame'),
navigator: {
userAgent: 'mock navigator'
}
});
}]);

Expand Down