Преглед на файлове

Add feature detection utils with unicode properties support.

Mateusz Samsel преди 6 години
родител
ревизия
07dd5bd200
променени са 2 файла, в които са добавени 56 реда и са изтрити 0 реда
  1. 36 0
      packages/ckeditor5-utils/src/featuredetection.js
  2. 20 0
      packages/ckeditor5-utils/tests/featuredetection.js

+ 36 - 0
packages/ckeditor5-utils/src/featuredetection.js

@@ -0,0 +1,36 @@
+/**
+ * @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
+ */
+
+/**
+ * Holds feature detection used by the editor.
+ *
+ * @protected
+ * @namespace
+ */
+export default {
+	/**
+	 * Indicates whether the current browser supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
+	 *
+	 * @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 {
+			isSupported = 'ć'.search( new RegExp( '[\\p{L}]', 'u' ) ) === 0;
+		} catch ( error ) {
+			// Firefox throws a SyntaxError when the group is unsupported.
+		}
+
+		return isSupported;
+	}() )
+};

+ 20 - 0
packages/ckeditor5-utils/tests/featuredetection.js

@@ -0,0 +1,20 @@
+/**
+ * @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 featureDetection from '../src/featuredetection';
+
+describe( 'featuredetection', () => {
+	describe( 'isUnicodePropertySupported', () => {
+		it( 'should detect accessibility of unicode properties', () => {
+			const testFn = () => ( new RegExp( '\\p{L}', 'u' ) ).test( 'ć' );
+
+			if ( featureDetection.isUnicodePropertySupported ) {
+				expect( testFn() ).to.be.true;
+			} else {
+				expect( testFn ).to.throw();
+			}
+		} );
+	} );
+} );