Skip to content

Commit

Permalink
Merge pull request #7979 from ckeditor/i/273-env-blink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oleq authored Sep 1, 2020
2 parents 1c0b5c9 + 2ddf6a5 commit a5a4b93
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/ckeditor5-utils/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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.
*
Expand Down Expand Up @@ -109,6 +117,18 @@ export function isAndroid( userAgent ) {
return userAgent.indexOf( 'android' ) > -1;
}

/**
* 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
Expand Down
44 changes: 43 additions & 1 deletion packages/ckeditor5-utils/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, { 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();
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a5a4b93

Please sign in to comment.