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

Commit

Permalink
Merge pull request #291 from ckeditor/t/ckeditor5-typing/167
Browse files Browse the repository at this point in the history
Feature: Added `env.isAndroid`.
  • Loading branch information
Piotr Jasiun authored Jun 28, 2019
2 parents b8041e5 + 7b2b6d4 commit 591f641
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ const env = {
* @static
* @type {Boolean}
*/
isSafari: isSafari( userAgent )
isSafari: isSafari( userAgent ),

/**
* Indicates that the application is running on Android mobile device.
*
* @static
* @type {Boolean}
*/
isAndroid: isAndroid( userAgent )
};

export default env;
Expand Down Expand Up @@ -91,3 +99,13 @@ export function isGecko( userAgent ) {
export function isSafari( userAgent ) {
return userAgent.indexOf( ' applewebkit/' ) > -1 && userAgent.indexOf( 'chrome' ) === -1;
}

/**
* Checks if User Agent represented by the string is Android mobile device.
*
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
* @returns {Boolean} Whether User Agent is Safari or not.
*/
export function isAndroid( userAgent ) {
return userAgent.indexOf( 'android' ) > -1;
}
37 changes: 36 additions & 1 deletion tests/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

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

function toLowerCase( str ) {
return str.toLowerCase();
Expand Down Expand Up @@ -38,6 +38,12 @@ describe( 'Env', () => {
} );
} );

describe( 'isAndroid', () => {
it( 'is a boolean', () => {
expect( env.isAndroid ).to.be.a( 'boolean' );
} );
} );

describe( 'isMac()', () => {
it( 'returns true for macintosh UA strings', () => {
expect( isMac( 'macintosh' ) ).to.be.true;
Expand Down Expand Up @@ -134,4 +140,33 @@ describe( 'Env', () => {
} );
/* eslint-enable max-len */
} );

describe( 'isAndroid()', () => {
/* eslint-disable max-len */
it( 'returns true for Android UA strings', () => {
// Strings taken from https://developer.chrome.com/multidevice/user-agent.
expect( isAndroid( toLowerCase(
'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>'
) ) ).to.be.true;

expect( isAndroid( toLowerCase(
'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev>(KHTML, like Gecko) Chrome/<Chrome Rev> Safari/<WebKit Rev>'
) ) ).to.be.true;
} );

it( 'returns false for non-Android UA strings', () => {
expect( isAndroid( toLowerCase(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
) ) ).to.be.false;

expect( isAndroid( toLowerCase(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
) ) ).to.be.false;

expect( isAndroid( toLowerCase(
'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
) ) ).to.be.false;
} );
/* eslint-enable max-len */
} );
} );

0 comments on commit 591f641

Please sign in to comment.