diff --git a/src/env.js b/src/env.js index d968d16..1cb4350 100644 --- a/src/env.js +++ b/src/env.js @@ -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; @@ -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; +} diff --git a/tests/env.js b/tests/env.js index fb666ef..74f6220 100644 --- a/tests/env.js +++ b/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, isAndroid } from '../src/env'; +import env, { isEdge, isMac, isGecko, isSafari, isAndroid, isRegExpUnicodePropertySupported } from '../src/env'; function toLowerCase( str ) { return str.toLowerCase(); @@ -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; @@ -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(); + } + } ); + } ); } );