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 #292 from ckeditor/t/ckeditor5-word-count/5
Browse files Browse the repository at this point in the history
Feature: Add feature detection of Unicode properties entities' support.
  • Loading branch information
jodator authored Jul 23, 2019
2 parents 21f1c4e + 22001a0 commit 21c0f6b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
42 changes: 41 additions & 1 deletion src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,24 @@ const env = {
* @static
* @type {Boolean}
*/
isAndroid: isAndroid( userAgent )
isAndroid: isAndroid( userAgent ),

/**
* Environment features information.
*
* @memberOf module:utils/env~env
* @namespace
*/
features: {
/**
* Indicates that the environment supports ES2018 Unicode property escapes — 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).
*
* @type {Boolean}
*/
isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
}
};

export default env;
Expand Down Expand Up @@ -109,3 +126,26 @@ export function isSafari( userAgent ) {
export function isAndroid( userAgent ) {
return userAgent.indexOf( 'android' ) > -1;
}

/**
* 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).
*
* @returns {Boolean}
*/
export function isRegExpUnicodePropertySupported() {
let isSupported = false;

// Feature detection for Unicode properties. Added in ES2018. Currently Firefox and Edge do not support it.
// See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.

try {
// Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534).
isSupported = 'ć'.search( new RegExp( '[\\p{L}]', 'u' ) ) === 0;
} catch ( error ) {
// Firefox throws a SyntaxError when the group is unsupported.
}

return isSupported;
}
27 changes: 26 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, isAndroid } from '../src/env';
import env, { isEdge, isMac, isGecko, isSafari, isAndroid, isRegExpUnicodePropertySupported } from '../src/env';

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

describe( 'features', () => {
it( 'is an object', () => {
expect( env.features ).to.be.an( 'object' );
} );

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

describe( 'isMac()', () => {
it( 'returns true for macintosh UA strings', () => {
expect( isMac( 'macintosh' ) ).to.be.true;
Expand Down Expand Up @@ -169,4 +181,17 @@ describe( 'Env', () => {
} );
/* 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)
const testFn = () => ( new RegExp( '\\p{L}', 'u' ) ).test( 'ć' );

if ( isRegExpUnicodePropertySupported() ) {
expect( testFn() ).to.be.true;
} else {
expect( testFn ).to.throw();
}
} );
} );
} );

0 comments on commit 21c0f6b

Please sign in to comment.