浏览代码

Fix RegExp unicode property escapes feature detection for Edge.

Maciej Gołaszewski 6 年之前
父节点
当前提交
d397b5ab81
共有 2 个文件被更改,包括 19 次插入21 次删除
  1. 12 6
      packages/ckeditor5-mention/src/mentionui.js
  2. 7 15
      packages/ckeditor5-mention/tests/mentionui.js

+ 12 - 6
packages/ckeditor5-mention/src/mentionui.js

@@ -15,6 +15,7 @@ import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsid
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+import env from '@ckeditor/ckeditor5-utils/src/env';
 
 import TextWatcher from './textwatcher';
 
@@ -524,13 +525,18 @@ function getBalloonPanelPositions( positionName ) {
 function createRegExp( marker, minimumCharacters ) {
 	const numberOfCharacters = minimumCharacters == 0 ? '*' : `{${ minimumCharacters },}`;
 
-	try {
-		// Uses the ES2018 syntax. See ckeditor5-mention#44.
-		return new RegExp( buildPattern( '\\p{Ps}"\';', marker, numberOfCharacters ), 'u' );
-	} catch ( error ) {
-		// ES2018 RegExp Unicode property escapes are not supported - fallback to save character list.
-		return new RegExp( buildPattern( '\\(\\[{"\'', marker, numberOfCharacters ), 'u' );
+	if ( !env.isEdge ) {
+		// Unfortunately Edge does not throw on `/[\p{Ps}]/u` as it does on `/\p{Ps}/u (no square brackets in latter).
+		try {
+			// Uses the ES2018 syntax. See ckeditor5-mention#44.
+			return new RegExp( buildPattern( '\\p{Ps}"\'', marker, numberOfCharacters ), 'u' );
+		} catch ( error ) {
+			// It's OK we're return later.
+		}
 	}
+
+	// ES2018 RegExp Unicode property escapes are not supported - fallback to save character list.
+	return new RegExp( buildPattern( '\\(\\[{"\'', marker, numberOfCharacters ), 'u' );
 }
 
 // Creates a regex pattern for the marker.

+ 7 - 15
packages/ckeditor5-mention/tests/mentionui.js

@@ -435,25 +435,19 @@ describe( 'MentionUI', () => {
 			} );
 		} );
 
-		describe( 'ES2018 RegExp Unicode property escapes fallback', () => {
+		describe( 'ES2018 RegExp Unicode property escapes fallback', function() {
 			// Cache the original RegExp to restore it after the tests.
 			const RegExp = window.RegExp;
 
-			let callTimes;
-
 			beforeEach( () => {
-				callTimes = 0;
-
 				// 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( ...args ) {
-					callTimes++;
-
-					if ( callTimes == 1 ) {
+				function FakeRegExp( pattern, flags ) {
+					if ( pattern.includes( '\\p{Ps}' ) ) {
 						throw new SyntaxError( 'invalid identity escape in regular expression' );
 					}
 
-					return new RegExp( ...args );
+					return new RegExp( pattern, flags );
 				}
 
 				window.RegExp = FakeRegExp;
@@ -481,8 +475,6 @@ describe( 'MentionUI', () => {
 					.then( () => {
 						expect( panelView.isVisible ).to.be.false;
 						expect( editor.model.markers.has( 'mention' ) ).to.be.false;
-
-						expect( callTimes );
 					} );
 			} );
 		} );
@@ -557,7 +549,7 @@ describe( 'MentionUI', () => {
 			} );
 
 			// Opening parenthesis type characters that should be supported on all environments.
-			for ( const character of [ '(', '\'', '"', '[' ] ) {
+			for ( const character of [ '(', '\'', '"', '[', '{' ] ) {
 				testOpeningPunctuationCharacter( character );
 			}
 
@@ -911,8 +903,8 @@ describe( 'MentionUI', () => {
 
 				return waitForDebounce()
 					.then( () => {
-						expect( panelView.isVisible ).to.be.true;
-						expect( editor.model.markers.has( 'mention' ) ).to.be.true;
+						expect( panelView.isVisible, 'panel is visible' ).to.be.true;
+						expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
 						expect( mentionsView.items ).to.have.length( 5 );
 					} );
 			} );