Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 21c0f6b

Browse files
authored
Merge pull request #292 from ckeditor/t/ckeditor5-word-count/5
Feature: Add feature detection of Unicode properties entities' support.
2 parents 21f1c4e + 22001a0 commit 21c0f6b

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/env.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,24 @@ const env = {
5555
* @static
5656
* @type {Boolean}
5757
*/
58-
isAndroid: isAndroid( userAgent )
58+
isAndroid: isAndroid( userAgent ),
59+
60+
/**
61+
* Environment features information.
62+
*
63+
* @memberOf module:utils/env~env
64+
* @namespace
65+
*/
66+
features: {
67+
/**
68+
* Indicates that the environment supports ES2018 Unicode property escapes — like `\p{P}` or `\p{L}`.
69+
* More information about unicode properties might be found
70+
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
71+
*
72+
* @type {Boolean}
73+
*/
74+
isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
75+
}
5976
};
6077

6178
export default env;
@@ -109,3 +126,26 @@ export function isSafari( userAgent ) {
109126
export function isAndroid( userAgent ) {
110127
return userAgent.indexOf( 'android' ) > -1;
111128
}
129+
130+
/**
131+
* Checks if the current environment supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
132+
* More information about unicode properties might be found
133+
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
134+
*
135+
* @returns {Boolean}
136+
*/
137+
export function isRegExpUnicodePropertySupported() {
138+
let isSupported = false;
139+
140+
// Feature detection for Unicode properties. Added in ES2018. Currently Firefox and Edge do not support it.
141+
// See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.
142+
143+
try {
144+
// Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534).
145+
isSupported = 'ć'.search( new RegExp( '[\\p{L}]', 'u' ) ) === 0;
146+
} catch ( error ) {
147+
// Firefox throws a SyntaxError when the group is unsupported.
148+
}
149+
150+
return isSupported;
151+
}

tests/env.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
44
*/
55

6-
import env, { isEdge, isMac, isGecko, isSafari, isAndroid } from '../src/env';
6+
import env, { isEdge, isMac, isGecko, isSafari, isAndroid, isRegExpUnicodePropertySupported } from '../src/env';
77

88
function toLowerCase( str ) {
99
return str.toLowerCase();
@@ -44,6 +44,18 @@ describe( 'Env', () => {
4444
} );
4545
} );
4646

47+
describe( 'features', () => {
48+
it( 'is an object', () => {
49+
expect( env.features ).to.be.an( 'object' );
50+
} );
51+
52+
describe( 'isRegExpUnicodePropertySupported', () => {
53+
it( 'is a boolean', () => {
54+
expect( env.features.isRegExpUnicodePropertySupported ).to.be.a( 'boolean' );
55+
} );
56+
} );
57+
} );
58+
4759
describe( 'isMac()', () => {
4860
it( 'returns true for macintosh UA strings', () => {
4961
expect( isMac( 'macintosh' ) ).to.be.true;
@@ -169,4 +181,17 @@ describe( 'Env', () => {
169181
} );
170182
/* eslint-enable max-len */
171183
} );
184+
185+
describe( 'isRegExpUnicodePropertySupported()', () => {
186+
it( 'should detect accessibility of unicode properties', () => {
187+
// Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534)
188+
const testFn = () => ( new RegExp( '\\p{L}', 'u' ) ).test( 'ć' );
189+
190+
if ( isRegExpUnicodePropertySupported() ) {
191+
expect( testFn() ).to.be.true;
192+
} else {
193+
expect( testFn ).to.throw();
194+
}
195+
} );
196+
} );
172197
} );

0 commit comments

Comments
 (0)