8
0
Просмотр исходного кода

Internal: Restructured the code to achieve 100% code coverage.

Marek Lewandowski 6 лет назад
Родитель
Сommit
17bf2c4250

+ 35 - 0
packages/ckeditor5-mention/src/featuredetection.js

@@ -0,0 +1,35 @@
+/**
+ * @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 mention/featuredetection
+ */
+
+/**
+ * Holds feature detection resolutions used by the mention plugin.
+ *
+ * @protected
+ * @namespace
+ */
+export default {
+	/**
+	 * Indicates whether the current browser supports ES2018 Unicode punctuation groups `\p{P}`.
+	 *
+	 * @type {Boolean}
+	 */
+	isPunctuationGroupSupported: ( function() {
+		let punctuationSupported = false;
+		// Feature detection for Unicode punctuation groups. It's added in ES2018. Currently Firefox and Edge does not support it.
+		// See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.
+
+		try {
+			punctuationSupported = '.'.search( new RegExp( '[\\p{P}]', 'u' ) ) === 0;
+		} catch ( error ) {
+			// Firefox throws a SyntaxError when the group is unsupported.
+		}
+
+		return punctuationSupported;
+	}() )
+};

+ 2 - 15
packages/ckeditor5-mention/src/mentionui.js

@@ -12,6 +12,7 @@ import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
 import Collection from '@ckeditor/ckeditor5-utils/src/collection';
 import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
+import featureDetection from './featuredetection';
 import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
@@ -533,20 +534,6 @@ function getBalloonPanelPositions( preferredPosition ) {
 	];
 }
 
-const isPunctuationGroupSupported = ( function() {
-	let punctuationSupported = false;
-	// Feature detection for Unicode punctuation groups. It's added in ES2018. Currently Firefox and Edge does not support it.
-	// See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.
-
-	try {
-		punctuationSupported = '.'.search( new RegExp( '[\\p{P}]', 'u' ) ) === 0;
-	} catch ( error ) {
-		// It's OK we're fallback to non ES2018 RegExp later.
-	}
-
-	return punctuationSupported;
-}() );
-
 // Creates a regex for marker.
 //
 // @param {String} marker
@@ -554,7 +541,7 @@ const isPunctuationGroupSupported = ( function() {
 // @returns {String}
 function createRegExp( marker, minimumCharacters ) {
 	const numberOfCharacters = minimumCharacters == 0 ? '*' : `{${ minimumCharacters },}`;
-	const patternBase = isPunctuationGroupSupported ? '\\p{Ps}\\p{Pi}"\'' : '\\(\\[{"\'';
+	const patternBase = featureDetection.isPunctuationGroupSupported ? '\\p{Ps}\\p{Pi}"\'' : '\\(\\[{"\'';
 
 	return new RegExp( buildPattern( patternBase, marker, numberOfCharacters ), 'u' );
 }

+ 10 - 65
packages/ckeditor5-mention/tests/mentionui.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
-/* global document, setTimeout, Event, window */
+/* global document, setTimeout, Event */
 
 import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
@@ -19,6 +19,7 @@ import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextu
 import env from '@ckeditor/ckeditor5-utils/src/env';
 
 import MentionUI from '../src/mentionui';
+import featureDetection from '../src/featuredetection';
 import MentionEditing from '../src/mentionediting';
 import MentionsView from '../src/ui/mentionsview';
 
@@ -444,78 +445,22 @@ describe( 'MentionUI', () => {
 
 		describe( 'ES2018 RegExp Unicode property escapes fallback', () => {
 			// Cache the original RegExp to restore it after the tests.
-			const RegExp = window.RegExp;
+			const originalPunctationSupport = featureDetection.isPunctuationGroupSupported;
 
-			beforeEach( () => {
-				// The FakeRegExp throws on first call - it simulates syntax error of ES2018 syntax usage
-				// on browsers other then Chrome. See ckeditor5-mention#44.
-				function FakeRegExp( pattern, flags ) {
-					if ( pattern.includes( '\\p{Ps}' ) ) {
-						throw new SyntaxError( 'invalid identity escape in regular expression' );
-					}
-
-					return new RegExp( pattern, flags );
-				}
-
-				window.RegExp = FakeRegExp;
+			before( () => {
+				featureDetection.isPunctuationGroupSupported = false;
+			} );
 
+			beforeEach( () => {
 				return createClassicTestEditor( staticConfig );
 			} );
 
-			afterEach( () => {
+			after( () => {
 				// Restore the original RegExp.
-				window.RegExp = RegExp;
-			} );
-
-			it( 'should fallback to old method if browser does not support unicode property escapes', () => {
-				setData( model, '<paragraph>[] foo</paragraph>' );
-
-				model.change( writer => {
-					writer.insertText( '〈', doc.selection.getFirstPosition() );
-				} );
-
-				model.change( writer => {
-					writer.insertText( '@', doc.selection.getFirstPosition() );
-				} );
-
-				return waitForDebounce()
-					.then( () => {
-						expect( panelView.isVisible ).to.be.false;
-						expect( editor.model.markers.has( 'mention' ) ).to.be.false;
-					} );
-			} );
-
-			it( 'should fallback to old method if browser does not support unicode property escapes (on Edge)', () => {
-				// Stub the isEdge for covarage tests in other browsers.
-				testUtils.sinon.stub( env, 'isEdge' ).get( () => true );
-
-				setData( model, '<paragraph>[] foo</paragraph>' );
-
-				model.change( writer => {
-					writer.insertText( '〈', doc.selection.getFirstPosition() );
-				} );
-
-				model.change( writer => {
-					writer.insertText( '@', doc.selection.getFirstPosition() );
-				} );
-
-				return waitForDebounce()
-					.then( () => {
-						expect( panelView.isVisible ).to.be.false;
-						expect( editor.model.markers.has( 'mention' ) ).to.be.false;
-					} );
-			} );
-		} );
-
-		describe( 'ES2018 RegExp Unicode property escapes fallback on Edge', () => {
-			beforeEach( () => {
-				// Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
-				testUtils.sinon.stub( env, 'isEdge' ).get( () => true );
-
-				return createClassicTestEditor( staticConfig );
+				featureDetection.isPunctuationGroupSupported = originalPunctationSupport;
 			} );
 
-			it( 'should fallback to old method if browser does not support unicode property escapes (on Edge)', () => {
+			it( 'should fallback to simpler RegExp if browser does not support unicode property escapes', () => {
 				setData( model, '<paragraph>[] foo</paragraph>' );
 
 				model.change( writer => {