Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 591f641

Browse files
author
Piotr Jasiun
authored
Merge pull request #291 from ckeditor/t/ckeditor5-typing/167
Feature: Added `env.isAndroid`.
2 parents b8041e5 + 7b2b6d4 commit 591f641

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/env.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ const env = {
4747
* @static
4848
* @type {Boolean}
4949
*/
50-
isSafari: isSafari( userAgent )
50+
isSafari: isSafari( userAgent ),
51+
52+
/**
53+
* Indicates that the application is running on Android mobile device.
54+
*
55+
* @static
56+
* @type {Boolean}
57+
*/
58+
isAndroid: isAndroid( userAgent )
5159
};
5260

5361
export default env;
@@ -91,3 +99,13 @@ export function isGecko( userAgent ) {
9199
export function isSafari( userAgent ) {
92100
return userAgent.indexOf( ' applewebkit/' ) > -1 && userAgent.indexOf( 'chrome' ) === -1;
93101
}
102+
103+
/**
104+
* Checks if User Agent represented by the string is Android mobile device.
105+
*
106+
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
107+
* @returns {Boolean} Whether User Agent is Safari or not.
108+
*/
109+
export function isAndroid( userAgent ) {
110+
return userAgent.indexOf( 'android' ) > -1;
111+
}

tests/env.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
44
*/
55

6-
import env, { isEdge, isMac, isGecko, isSafari } from '../src/env';
6+
import env, { isEdge, isMac, isGecko, isSafari, isAndroid } from '../src/env';
77

88
function toLowerCase( str ) {
99
return str.toLowerCase();
@@ -38,6 +38,12 @@ describe( 'Env', () => {
3838
} );
3939
} );
4040

41+
describe( 'isAndroid', () => {
42+
it( 'is a boolean', () => {
43+
expect( env.isAndroid ).to.be.a( 'boolean' );
44+
} );
45+
} );
46+
4147
describe( 'isMac()', () => {
4248
it( 'returns true for macintosh UA strings', () => {
4349
expect( isMac( 'macintosh' ) ).to.be.true;
@@ -134,4 +140,33 @@ describe( 'Env', () => {
134140
} );
135141
/* eslint-enable max-len */
136142
} );
143+
144+
describe( 'isAndroid()', () => {
145+
/* eslint-disable max-len */
146+
it( 'returns true for Android UA strings', () => {
147+
// Strings taken from https://developer.chrome.com/multidevice/user-agent.
148+
expect( isAndroid( toLowerCase(
149+
'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>'
150+
) ) ).to.be.true;
151+
152+
expect( isAndroid( toLowerCase(
153+
'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev>(KHTML, like Gecko) Chrome/<Chrome Rev> Safari/<WebKit Rev>'
154+
) ) ).to.be.true;
155+
} );
156+
157+
it( 'returns false for non-Android UA strings', () => {
158+
expect( isAndroid( toLowerCase(
159+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
160+
) ) ).to.be.false;
161+
162+
expect( isAndroid( toLowerCase(
163+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
164+
) ) ).to.be.false;
165+
166+
expect( isAndroid( toLowerCase(
167+
'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
168+
) ) ).to.be.false;
169+
} );
170+
/* eslint-enable max-len */
171+
} );
137172
} );

0 commit comments

Comments
 (0)