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 #278 from ckeditor/t/ckeditor5/1463
Browse files Browse the repository at this point in the history
Feature: Added `isSafari` property and `isSafari()` helper to the `env` module. See: ckeditor/ckeditor5#1463.
  • Loading branch information
oleq authored Mar 28, 2019
2 parents 1968d0c + 4c961b8 commit f1ba6ae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ const env = {
* @static
* @member {Boolean} module:utils/env~env#isEdge
*/
isGecko: isGecko( userAgent )
isGecko: isGecko( userAgent ),

/**
* Indicates that the application is running in Safari.
*
* @static
* @member {Boolean} module:utils/env~env#isSafari
*/
isSafari: isSafari( userAgent )
};

export default env;
Expand Down Expand Up @@ -73,3 +81,13 @@ export function isEdge( userAgent ) {
export function isGecko( userAgent ) {
return !!userAgent.match( /gecko\/\d+/ );
}

/**
* Checks if User Agent represented by the string is Safari.
*
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
* @returns {Boolean} Whether User Agent is Safari or not.
*/
export function isSafari( userAgent ) {
return userAgent.indexOf( ' applewebkit/' ) > -1 && userAgent.indexOf( 'chrome' ) === -1;
}
39 changes: 35 additions & 4 deletions tests/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
* For licensing, see LICENSE.md.
*/

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

function toLowerCase( str ) {
return str.toLowerCase();
}

describe( 'Env', () => {
beforeEach( () => {
} );

it( 'is an object', () => {
expect( env ).to.be.an( 'object' );
} );
Expand All @@ -35,6 +32,12 @@ describe( 'Env', () => {
} );
} );

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

describe( 'isMac()', () => {
it( 'returns true for macintosh UA strings', () => {
expect( isMac( 'macintosh' ) ).to.be.true;
Expand Down Expand Up @@ -103,4 +106,32 @@ describe( 'Env', () => {
) ) ).to.be.false;
} );
} );

describe( 'isSafari()', () => {
/* eslint-disable max-len */
it( 'returns true for Safari UA strings', () => {
expect( isSafari( 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.true;

expect( isSafari( 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.true;
} );

it( 'returns false for non-Safari UA strings', () => {
expect( isSafari( 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( isSafari( toLowerCase(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
) ) ).to.be.false;

expect( isSafari( 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.false;
} );
/* eslint-enable max-len */
} );
} );

0 comments on commit f1ba6ae

Please sign in to comment.