Skip to content

Commit c1bd679

Browse files
authored
Merge pull request #7479 from plotly/cam/7476/add-iswebkitwebview-check
fix: Add method to check for WebKit WebView user agent string
2 parents 4bd8b85 + d927fb6 commit c1bd679

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

draftlogs/7479_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add method to check for WebKit WebView user agent string [[#7479](https://github.com/plotly/plotly.js/pull/7479)]

src/lib/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,12 @@ lib.isIOS = function() {
765765
return IS_IOS_REGEX.test(window.navigator.userAgent);
766766
};
767767

768+
// The WKWebView user agent string doesn't include 'Safari', so we need a separate test
769+
// for a UA string like this:
770+
// Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko)
771+
const IS_MAC_WKWEBVIEW_REGEX = /Macintosh.+AppleWebKit.+Gecko\)$/;
772+
lib.isMacWKWebView = () => IS_MAC_WKWEBVIEW_REGEX.test(window.navigator.userAgent);
773+
768774
var FIREFOX_VERSION_REGEX = /Firefox\/(\d+)\.\d+/;
769775
lib.getFirefoxVersion = function() {
770776
var match = FIREFOX_VERSION_REGEX.exec(window.navigator.userAgent);

src/lib/supports_pixelated_image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function supportsPixelatedImage() {
1919
_supportsPixelated = false;
2020

2121
// @see https://github.com/plotly/plotly.js/issues/6604
22-
var unsupportedBrowser = Lib.isSafari() || Lib.isIOS();
22+
var unsupportedBrowser = Lib.isSafari() || Lib.isMacWKWebView() || Lib.isIOS();
2323

2424
if(window.navigator.userAgent && !unsupportedBrowser) {
2525
var declarations = Array.from(constants.CSS_DECLARATIONS).reverse();

test/jasmine/tests/lib_test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2956,6 +2956,67 @@ describe('Test lib.js:', function() {
29562956
});
29572957
});
29582958
});
2959+
2960+
describe("User agent", () => {
2961+
const userAgentStrings = {
2962+
iOSSafari: "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Mobile/15E148 Safari/604.1",
2963+
iOSChrome: "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/138.0.7204.156 Mobile/15E148 Safari/604.1",
2964+
macChrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
2965+
macSafari: "Mozilla/5.0 (Macintosh; Intel Mac OS X 15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15",
2966+
macWKWebView: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko)",
2967+
winFirefox: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0"
2968+
}
2969+
2970+
describe('isIOS', () => {
2971+
[userAgentStrings.iOSChrome, userAgentStrings.iOSSafari].forEach(uaString => {
2972+
it('matches an iOS user agent string', () => {
2973+
spyOnProperty(navigator, 'userAgent').and.returnValue(uaString)
2974+
expect(Lib.isIOS()).toBe(true)
2975+
})
2976+
})
2977+
2978+
it("doesn't match a non-iOS user agent string", () => {
2979+
spyOnProperty(navigator, 'userAgent').and.returnValue(userAgentStrings.macSafari)
2980+
expect(Lib.isIOS()).toBe(false)
2981+
})
2982+
})
2983+
2984+
describe('isSafari', () => {
2985+
it('matches a Safari user agent string', () => {
2986+
spyOnProperty(navigator, 'userAgent').and.returnValue(userAgentStrings.macSafari)
2987+
expect(Lib.isSafari()).toBe(true)
2988+
})
2989+
2990+
it("doesn't match a non-Safari user agent string", () => {
2991+
spyOnProperty(navigator, 'userAgent').and.returnValue(userAgentStrings.macChrome)
2992+
expect(Lib.isSafari()).toBe(false)
2993+
})
2994+
})
2995+
2996+
describe('isMacWKWebView', () => {
2997+
it('matches a Safari user agent string', () => {
2998+
spyOnProperty(navigator, 'userAgent').and.returnValue(userAgentStrings.macWKWebView)
2999+
expect(Lib.isMacWKWebView()).toBe(true)
3000+
})
3001+
3002+
it("doesn't match a non-Safari user agent string", () => {
3003+
spyOnProperty(navigator, 'userAgent').and.returnValue(userAgentStrings.macSafari)
3004+
expect(Lib.isMacWKWebView()).toBe(false)
3005+
})
3006+
})
3007+
3008+
describe('getFirefoxVersion', () => {
3009+
it('gets the Firefox version from the user agent string', () => {
3010+
spyOnProperty(navigator, 'userAgent').and.returnValue(userAgentStrings.winFirefox)
3011+
expect(Lib.getFirefoxVersion()).toBe(140)
3012+
})
3013+
3014+
it("returns null for a non-Firefox user agent string", () => {
3015+
spyOnProperty(navigator, 'userAgent').and.returnValue(userAgentStrings.macSafari)
3016+
expect(Lib.getFirefoxVersion()).toBe(null)
3017+
})
3018+
})
3019+
})
29593020
});
29603021

29613022
describe('Queue', function() {

0 commit comments

Comments
 (0)