Bläddra i källkod

Refactor auto link URL retrieval.

Maciej Gołaszewski 5 år sedan
förälder
incheckning
2cdaba8da8
1 ändrade filer med 20 tillägg och 14 borttagningar
  1. 20 14
      packages/ckeditor5-link/src/autolink.js

+ 20 - 14
packages/ckeditor5-link/src/autolink.js

@@ -24,15 +24,11 @@ const urlRegExp = new RegExp(
 		'((?:\\/[+~%/.\\w\\-_]*)?\\??(?:[-+=&;%@.\\w_]*)#?(?:[.!/\\\\\\w]*))?' +
 		'((?:\\/[+~%/.\\w\\-_]*)?\\??(?:[-+=&;%@.\\w_]*)#?(?:[.!/\\\\\\w]*))?' +
 	')$', 'i' );
 	')$', 'i' );
 
 
+const URL_GROUP_IN_MATCH = 2;
+
 // Simplified email test - should be run over previously found URL.
 // Simplified email test - should be run over previously found URL.
 const emailRegExp = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i;
 const emailRegExp = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i;
 
 
-const URL_POSITION_IN_MATCH = 2;
-
-function isEmail( linkHref ) {
-	return emailRegExp.exec( linkHref );
-}
-
 /**
 /**
  * The auto link plugin.
  * The auto link plugin.
  *
  *
@@ -59,23 +55,23 @@ export default class AutoLink extends Plugin {
 			}
 			}
 
 
 			// 2. Check text before "space" or "enter".
 			// 2. Check text before "space" or "enter".
-			const match = urlRegExp.exec( text.substr( 0, text.length - 1 ) );
+			const url = getUrlAtTextEnd( text.substr( 0, text.length - 1 ) );
 
 
-			if ( match ) {
-				return { match };
+			if ( url ) {
+				return { url };
 			}
 			}
 		} );
 		} );
 
 
 		const input = editor.plugins.get( 'Input' );
 		const input = editor.plugins.get( 'Input' );
 
 
 		watcher.on( 'matched:data', ( evt, data ) => {
 		watcher.on( 'matched:data', ( evt, data ) => {
-			const { batch, range, match } = data;
+			const { batch, range, url } = data;
 
 
 			if ( !input.isInput( batch ) ) {
 			if ( !input.isInput( batch ) ) {
 				return;
 				return;
 			}
 			}
 
 
-			this._applyAutoLink( match[ URL_POSITION_IN_MATCH ], range );
+			this._applyAutoLink( url, range );
 		} );
 		} );
 
 
 		// todo: watcher.bind();
 		// todo: watcher.bind();
@@ -127,10 +123,10 @@ export default class AutoLink extends Plugin {
 	_checkAndApplyAutoLinkOnRange( rangeToCheck ) {
 	_checkAndApplyAutoLinkOnRange( rangeToCheck ) {
 		const { text, range } = getLastTextLine( rangeToCheck, this.editor.model );
 		const { text, range } = getLastTextLine( rangeToCheck, this.editor.model );
 
 
-		const match = urlRegExp.exec( text );
+		const url = getUrlAtTextEnd( text );
 
 
-		if ( match ) {
-			this._applyAutoLink( match[ URL_POSITION_IN_MATCH ], range, 0 );
+		if ( url ) {
+			this._applyAutoLink( url, range, 0 );
 		}
 		}
 	}
 	}
 
 
@@ -152,3 +148,13 @@ export default class AutoLink extends Plugin {
 function isSingleSpaceAtTheEnd( text ) {
 function isSingleSpaceAtTheEnd( text ) {
 	return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ' ' && text[ text.length - 2 ] !== ' ';
 	return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ' ' && text[ text.length - 2 ] !== ' ';
 }
 }
+
+function getUrlAtTextEnd( text ) {
+	const match = urlRegExp.exec( text );
+
+	return match ? match[ URL_GROUP_IN_MATCH ] : null;
+}
+
+function isEmail( linkHref ) {
+	return emailRegExp.exec( linkHref );
+}