Explorar el Código

Moving protocol inserting logic to utils.

Paweł Kwaśnik hace 5 años
padre
commit
e33ed7b2ed

+ 3 - 11
packages/ckeditor5-link/src/autolink.js

@@ -10,7 +10,7 @@
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import TextWatcher from '@ckeditor/ckeditor5-typing/src/textwatcher';
 import getLastTextLine from '@ckeditor/ckeditor5-typing/src/utils/getlasttextline';
-import { isEmail } from './utils';
+import { addUrlProtocolIfApplicable } from './utils';
 
 const MIN_LINK_LENGTH_WITH_SPACE_AT_END = 4; // Ie: "t.co " (length 5).
 
@@ -50,10 +50,6 @@ const URL_REG_EXP = new RegExp(
 
 const URL_GROUP_IN_MATCH = 2;
 
-// The regex checks for the protocol syntax ('xxxx://' or 'xxxx:')
-// or non-word characters at the beginning of the link ('/', '#' etc.).
-const PROTOCOL_REG_EXP = /^((\w+:(\/{2,})?)|(\W))/i;
-
 /**
  * The autolink plugin.
  *
@@ -224,12 +220,8 @@ export default class AutoLink extends Plugin {
 
 		// Enqueue change to make undo step.
 		model.enqueueChange( writer => {
-			const defaultProtocol = this.editor.config.get( 'link.defaultProtocol' );
-			const protocol = isEmail( url ) ? 'mailto:' : defaultProtocol;
-			const isProtocolNeeded = !!protocol && !PROTOCOL_REG_EXP.test( url );
-			const parsedValue = url && isProtocolNeeded ? protocol + url : url;
-
-			writer.setAttribute( 'linkHref', parsedValue, range );
+			const parsedUrl = addUrlProtocolIfApplicable( url, this.editor );
+			writer.setAttribute( 'linkHref', parsedUrl, range );
 		} );
 	}
 }

+ 3 - 10
packages/ckeditor5-link/src/linkui.js

@@ -9,7 +9,7 @@
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
-import { isEmail, isLinkElement, LINK_KEYSTROKE } from './utils';
+import { addUrlProtocolIfApplicable, isLinkElement, LINK_KEYSTROKE } from './utils';
 
 import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
 
@@ -21,9 +21,6 @@ import LinkActionsView from './ui/linkactionsview';
 
 import linkIcon from '../theme/icons/link.svg';
 
-// The regex checks for the protocol syntax ('xxxx://' or 'xxxx:')
-// or non-word characters at the beginning of the link ('/', '#' etc.).
-const protocolRegExp = /^((\w+:(\/{2,})?)|(\W))/i;
 const VISUAL_SELECTION_MARKER_NAME = 'link-ui';
 
 /**
@@ -177,12 +174,8 @@ export default class LinkUI extends Plugin {
 		// Execute link command after clicking the "Save" button.
 		this.listenTo( formView, 'submit', () => {
 			const { value } = formView.urlInputView.fieldView.element;
-
-			const protocol = isEmail( value ) ? 'mailto:' : defaultProtocol;
-			const isProtocolNeeded = !!protocol && !protocolRegExp.test( value );
-			const parsedValue = value && isProtocolNeeded ? protocol + value : value;
-
-			editor.execute( 'link', parsedValue, formView.getDecoratorSwitchesState() );
+			const parsedUrl = addUrlProtocolIfApplicable( value, editor );
+			editor.execute( 'link', parsedUrl, formView.getDecoratorSwitchesState() );
 			this._closeFormView();
 		} );
 

+ 27 - 2
packages/ckeditor5-link/src/utils.js

@@ -15,6 +15,10 @@ const SAFE_URL = /^(?:(?:https?|ftps?|mailto):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))
 // Simplified email test - should be run over previously found URL.
 const EMAIL_REG_EXP = /^[\S]+@((?![-_])(?:[-\w\u00a1-\uffff]{0,63}[^-_]\.))+(?:[a-z\u00a1-\uffff]{2,})$/i;
 
+// The regex checks for the protocol syntax ('xxxx://' or 'xxxx:')
+// or non-word characters at the beginning of the link ('/', '#' etc.).
+const PROTOCOL_REG_EXP = /^((\w+:(\/{2,})?)|(\W))/i;
+
 /**
  * A keystroke used by the {@link module:link/linkui~LinkUI link UI feature}.
  */
@@ -139,6 +143,27 @@ export function isImageAllowed( element, schema ) {
 	return element.is( 'element', 'image' ) && schema.checkAttribute( 'image', 'linkHref' );
 }
 
-export function isEmail( linkHref ) {
-	return EMAIL_REG_EXP.test( linkHref );
+/**
+ * Returns `true` if the specified `value` is an email.
+ *
+ * @params {String} value
+ * @returns {Boolean}
+ */
+export function isEmail( value ) {
+	return EMAIL_REG_EXP.test( value );
+}
+
+/**
+ * Returns an URL with protocol prefix (if applicable).
+ *
+ * @params {String} url
+ * @params {module:core/editor/editor~Editor} editor
+ * @returns {Boolean}
+ */
+export function addUrlProtocolIfApplicable( url, editor ) {
+	const defaultProtocol = editor.config.get( 'link.defaultProtocol' );
+	const protocol = isEmail( url ) ? 'mailto:' : defaultProtocol;
+	const isProtocolNeeded = !!protocol && !PROTOCOL_REG_EXP.test( url );
+
+	return url && isProtocolNeeded ? protocol + url : url;
 }