Sfoglia il codice sorgente

Move feature detection to env.js. Move unit test with them.

Mateusz Samsel 6 anni fa
parent
commit
824c18719d

+ 39 - 1
packages/ckeditor5-utils/src/env.js

@@ -55,7 +55,23 @@ const env = {
 	 * @static
 	 * @static
 	 * @type {Boolean}
 	 * @type {Boolean}
 	 */
 	 */
-	isAndroid: isAndroid( userAgent )
+	isAndroid: isAndroid( userAgent ),
+
+	/**
+	 * Environment feature checks. Indicates if specific functionalities are available.
+	 *
+	 * @memberOf module:utils/env~env
+	 * @namespace
+	 */
+	features: {
+		/**
+		 * Indicates whether the current browser supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
+		 * More information about unicode properties might be found [here](https://www.unicode.org/reports/tr44/#GC_Values_Table).
+		 *
+		 * @type {Boolean}
+		 */
+		isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
+	}
 };
 };
 
 
 export default env;
 export default env;
@@ -109,3 +125,25 @@ export function isSafari( userAgent ) {
 export function isAndroid( userAgent ) {
 export function isAndroid( userAgent ) {
 	return userAgent.indexOf( 'android' ) > -1;
 	return userAgent.indexOf( 'android' ) > -1;
 }
 }
+
+/**
+ * Checks if current environment supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
+ * More information about unicode properties might be found [here](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;
+}

+ 0 - 38
packages/ckeditor5-utils/src/featuredetection/regexp.js

@@ -1,38 +0,0 @@
-/**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-/**
- * @module utils/featuredetection/regexp
- */
-
-/**
- * Holds feature detection related to regular expressions used by the editor.
- *
- * @protected
- * @namespace
- */
-export default {
-	/**
-	 * Indicates whether the current browser supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
-	 * More information about unicode properties might be found [here](https://www.unicode.org/reports/tr44/#GC_Values_Table).
-	 *
-	 * @type {Boolean}
-	 */
-	isUnicodePropertySupported: ( function() {
-		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;
-	}() )
-};

+ 26 - 1
packages/ckeditor5-utils/tests/env.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  * 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 ) {
 function toLowerCase( str ) {
 	return str.toLowerCase();
 	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()', () => {
 	describe( 'isMac()', () => {
 		it( 'returns true for macintosh UA strings', () => {
 		it( 'returns true for macintosh UA strings', () => {
 			expect( isMac( 'macintosh' ) ).to.be.true;
 			expect( isMac( 'macintosh' ) ).to.be.true;
@@ -169,4 +181,17 @@ describe( 'Env', () => {
 		} );
 		} );
 		/* eslint-enable max-len */
 		/* 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 - 21
packages/ckeditor5-utils/tests/featuredetection/regexp.js

@@ -1,21 +0,0 @@
-/**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-import regExpFeatureDetection from '../../src/featuredetection/regexp';
-
-describe( 'featuredetection', () => {
-	describe( 'isUnicodePropertySupported', () => {
-		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 ( regExpFeatureDetection.isUnicodePropertySupported ) {
-				expect( testFn() ).to.be.true;
-			} else {
-				expect( testFn ).to.throw();
-			}
-		} );
-	} );
-} );