浏览代码

Merge pull request #7979 from ckeditor/i/273-env-blink

Feature (utils): Added a user-agent check for the Blink engine to the [`env`](https://ckeditor.com/docs/ckeditor5/latest/api/module_utils_env-env.html) module.
Aleksander Nowodzinski 5 年之前
父节点
当前提交
a5a4b933e8
共有 2 个文件被更改,包括 63 次插入1 次删除
  1. 20 0
      packages/ckeditor5-utils/src/env.js
  2. 43 1
      packages/ckeditor5-utils/tests/env.js

+ 20 - 0
packages/ckeditor5-utils/src/env.js

@@ -50,6 +50,14 @@ const env = {
 	isAndroid: isAndroid( userAgent ),
 
 	/**
+	 * Indicates that the application is running in a browser using the Blink engine.
+	 *
+	 * @static
+	 * @type {Boolean}
+	 */
+	isBlink: isBlink( userAgent ),
+
+	/**
 	 * Environment features information.
 	 *
 	 * @memberOf module:utils/env~env
@@ -110,6 +118,18 @@ export function isAndroid( userAgent ) {
 }
 
 /**
+ * Checks if User Agent represented by the string is Blink engine.
+ *
+ * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
+ * @returns {Boolean} Whether User Agent is Blink engine or not.
+ */
+export function isBlink( userAgent ) {
+	// The Edge browser before switching to the Blink engine used to report itself as Chrome (and "Edge/")
+	// but after switching to the Blink it replaced "Edge/" with "Edg/".
+	return userAgent.indexOf( 'chrome/' ) > -1 && userAgent.indexOf( 'edge/' ) < 0;
+}
+
+/**
  * Checks if the current environment supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
  * More information about unicode properties might be found
  * [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).

+ 43 - 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, { isMac, isGecko, isSafari, isAndroid, isRegExpUnicodePropertySupported } from '../src/env';
+import env, { isMac, isGecko, isSafari, isAndroid, isRegExpUnicodePropertySupported, isBlink } from '../src/env';
 
 function toLowerCase( str ) {
 	return str.toLowerCase();
@@ -38,6 +38,12 @@ describe( 'Env', () => {
 		} );
 	} );
 
+	describe( 'isBlink', () => {
+		it( 'is a boolean', () => {
+			expect( env.isBlink ).to.be.a( 'boolean' );
+		} );
+	} );
+
 	describe( 'features', () => {
 		it( 'is an object', () => {
 			expect( env.features ).to.be.an( 'object' );
@@ -147,6 +153,42 @@ describe( 'Env', () => {
 		/* eslint-enable max-len */
 	} );
 
+	describe( 'isBlink()', () => {
+		/* eslint-disable max-len */
+		it( 'returns true for Blink UA strings', () => {
+			expect( isBlink( 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.true;
+
+			expect( isBlink( toLowerCase(
+				'Mozilla/5.0 (Linux; Android 7.1; Mi A1 Build/N2G47H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Mobile Safari/537.36'
+			) ) ).to.be.true;
+
+			expect( isBlink( toLowerCase(
+				'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52'
+			) ) ).to.be.true;
+		} );
+
+		it( 'returns false for non-Blink UA strings', () => {
+			expect( isBlink( toLowerCase(
+				'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15'
+			) ) ).to.be.false;
+
+			expect( isBlink( toLowerCase(
+				'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1'
+			) ) ).to.be.false;
+
+			expect( isBlink( toLowerCase(
+				'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
+			) ) ).to.be.false;
+
+			expect( isBlink( toLowerCase(
+				'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134'
+			) ) ).to.be.false;
+		} );
+		/* eslint-enable max-len */
+	} );
+
 	describe( 'isRegExpUnicodePropertySupported()', () => {
 		it( 'should detect accessibility of unicode properties', () => {
 			// Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534)