8
0
Просмотр исходного кода

Feature: Added `env.isAndroid`.

Szymon Cofalik 6 лет назад
Родитель
Сommit
63041aa71c
2 измененных файлов с 55 добавлено и 2 удалено
  1. 19 1
      packages/ckeditor5-utils/src/env.js
  2. 36 1
      packages/ckeditor5-utils/tests/env.js

+ 19 - 1
packages/ckeditor5-utils/src/env.js

@@ -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;
@@ -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;
+}

+ 36 - 1
packages/ckeditor5-utils/tests/env.js

@@ -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();
@@ -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;
@@ -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 */
+	} );
 } );