Browse Source

Remove DEFAULT_PROTOCOL and fix the script for better handling link values.

panr 5 years ago
parent
commit
3926be33a4

+ 8 - 5
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 { isLinkElement, DEFAULT_PROTOCOL } from './utils';
+import { isLinkElement } from './utils';
 import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
 
 import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler';
@@ -143,7 +143,7 @@ export default class LinkUI extends Plugin {
 	_createFormView() {
 		const editor = this.editor;
 		const linkCommand = editor.commands.get( 'link' );
-		const defaultProtocol = editor.config.get( 'link.defaultProtocol' ) || DEFAULT_PROTOCOL;
+		const defaultProtocol = editor.config.get( 'link.defaultProtocol' );
 
 		const formView = new LinkFormView( editor.locale, linkCommand, defaultProtocol );
 
@@ -156,13 +156,16 @@ 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 hasProtocol = ( /\w+:\/\//gmi ).test( value );
+
+			const defaultProtocol = editor.config.get( 'link.defaultProtocol' );
+
+			const isProtocolNeeded = !!defaultProtocol && !( /^((\w+:(\/{2,})?)|(\W))/gmi ).test( value );
 			const isEmail = ( /[\w-]+@[\w-]+\.+[\w-]+/gmi ).test( value );
 
 			// It's good to check config second time on submit for more dynamic cases
 			// when protocol might change after the element rendered.
-			const protocol = isEmail && 'mailto:' || editor.config.get( 'link.defaultProtocol' ) || defaultProtocol;
-			const parsedValue = !hasProtocol && value ? protocol + value : value;
+			const protocol = isEmail && 'mailto:' || defaultProtocol;
+			const parsedValue = value && isProtocolNeeded ? protocol + value : value;
 
 			editor.execute( 'link', parsedValue, formView.getDecoratorSwitchesState() );
 			this._closeFormView();

+ 2 - 4
packages/ckeditor5-link/src/ui/linkformview.js

@@ -21,8 +21,6 @@ import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
 import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
 import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
 
-import { DEFAULT_PROTOCOL } from '../utils';
-
 import checkIcon from '@ckeditor/ckeditor5-core/theme/icons/check.svg';
 import cancelIcon from '@ckeditor/ckeditor5-core/theme/icons/cancel.svg';
 import '../../theme/linkform.css';
@@ -210,10 +208,10 @@ export default class LinkFormView extends View {
 	 * Creates a labeled input view.
 	 *
 	 * @private
-	 * @param {module:link/utils~DefaultProtocol} [protocol=http://] A value of a protocol to be displayed in the input's placeholder.
+	 * @param {String} [protocol=http://] A value of a protocol to be displayed in the input's placeholder.
 	 * @returns {module:ui/labeledfield/labeledfieldview~LabeledFieldView} Labeled field view instance.
 	 */
-	_createUrlInput( protocol = DEFAULT_PROTOCOL ) {
+	_createUrlInput( protocol = 'https://' ) {
 		const t = this.locale.t;
 		const labeledInput = new LabeledFieldView( this.locale, createLabeledInputText );
 

+ 0 - 7
packages/ckeditor5-link/src/utils.js

@@ -13,13 +13,6 @@ const ATTRIBUTE_WHITESPACES = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u20
 const SAFE_URL = /^(?:(?:https?|ftps?|mailto):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))/i;
 
 /**
- * A default link protocol value.
- * @typedef {String} module:link/utils~DefaultProtocol
- * @default 'http://'
- */
-export const DEFAULT_PROTOCOL = 'http://';
-
-/**
  * Returns `true` if a given view node is the link element.
  *
  * @param {module:engine/view/node~Node} node

+ 61 - 4
packages/ckeditor5-link/tests/linkui.js

@@ -21,8 +21,6 @@ import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextu
 import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
 import View from '@ckeditor/ckeditor5-ui/src/view';
 
-import { DEFAULT_PROTOCOL } from '../src/utils';
-
 import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
 
 describe( 'LinkUI', () => {
@@ -932,14 +930,61 @@ describe( 'LinkUI', () => {
 					} );
 			} );
 
-			it( 'should use `http://` as a default link protocol without any configuration', () => {
+			it( 'should not add a protocol without the configuration', () => {
+				formView.urlInputView.fieldView.value = 'ckeditor.com';
+				formView.fire( 'submit' );
+
+				expect( formView.urlInputView.fieldView.value ).to.equal( 'ckeditor.com' );
+			} );
+
+			it( 'should not add a protocol to the local links even when `config.link.defaultProtocol` configured', () => {
+				editor.config.set( 'link.defaultProtocol', 'http://' );
+
+				formView.urlInputView.fieldView.value = '#test';
+				formView.fire( 'submit' );
+
+				expect( formView.urlInputView.fieldView.value ).to.equal( '#test' );
+			} );
+
+			it( 'should not add a protocol to the relative links even when `config.link.defaultProtocol` configured', () => {
+				editor.config.set( 'link.defaultProtocol', 'http://' );
+
+				formView.urlInputView.fieldView.value = '/test.html';
+				formView.fire( 'submit' );
+
+				expect( formView.urlInputView.fieldView.value ).to.equal( '/test.html' );
+			} );
+
+			it( 'should not add a protocol when given provided wihitn the value even when `config.link.defaultProtocol` configured', () => {
+				editor.config.set( 'link.defaultProtocol', 'http://' );
+
+				formView.urlInputView.fieldView.value = 'http://example.com';
+				formView.fire( 'submit' );
+
+				expect( formView.urlInputView.fieldView.value ).to.equal( 'http://example.com' );
+			} );
+
+			it( 'should use the "http://" protocol when it\'s configured', () => {
+				editor.config.set( 'link.defaultProtocol', 'http://' );
+
 				formView.urlInputView.fieldView.value = 'ckeditor.com';
 				formView.fire( 'submit' );
 
 				expect( formView.urlInputView.fieldView.value ).to.equal( 'http://ckeditor.com' );
 			} );
 
+			it( 'should use the "http://" protocol when it\'s configured and form input value contains "www."', () => {
+				editor.config.set( 'link.defaultProtocol', 'http://' );
+
+				formView.urlInputView.fieldView.value = 'www.ckeditor.com';
+				formView.fire( 'submit' );
+
+				expect( formView.urlInputView.fieldView.value ).to.equal( 'http://www.ckeditor.com' );
+			} );
+
 			it( 'should propagate the protocol to the link\'s `linkHref` attribute in model', () => {
+				editor.config.set( 'link.defaultProtocol', 'http://' );
+
 				setModelData( editor.model, '[ckeditor.com]' );
 
 				formView.urlInputView.fieldView.value = 'ckeditor.com';
@@ -951,6 +996,8 @@ describe( 'LinkUI', () => {
 			} );
 
 			it( 'should detect an email on submitting the form and add "mailto:" protocol automatically to the provided value', () => {
+				editor.config.set( 'link.defaultProtocol', 'http://' );
+
 				setModelData( editor.model, '[email@example.com]' );
 
 				formView.urlInputView.fieldView.value = 'email@example.com';
@@ -961,6 +1008,16 @@ describe( 'LinkUI', () => {
 					'[<$text linkHref="mailto:email@example.com">email@example.com</$text>]'
 				);
 			} );
+
+			it( 'should not add an email protocol when given provided wihitn the value' +
+				'even when `config.link.defaultProtocol` configured', () => {
+				editor.config.set( 'link.defaultProtocol', 'mailto:' );
+
+				formView.urlInputView.fieldView.value = 'mailto:test@example.com';
+				formView.fire( 'submit' );
+
+				expect( formView.urlInputView.fieldView.value ).to.equal( 'mailto:test@example.com' );
+			} );
 		} );
 
 		describe( 'binding', () => {
@@ -1120,7 +1177,7 @@ describe( 'LinkUI', () => {
 					formView.fire( 'submit' );
 
 					expect( executeSpy.calledOnce ).to.be.true;
-					expect( executeSpy.calledWithExactly( 'link', DEFAULT_PROTOCOL + 'url', { linkIsFoo: true } ) ).to.be.true;
+					expect( executeSpy.calledWithExactly( 'link', 'url', { linkIsFoo: true } ) ).to.be.true;
 				} );
 
 				it( 'should reset switch state when form view is closed', () => {

+ 1 - 3
packages/ckeditor5-link/tests/ui/linkformview.js

@@ -21,8 +21,6 @@ import Link from '../../src/link';
 import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 
-import { DEFAULT_PROTOCOL } from '../../src/utils';
-
 describe( 'LinkFormView', () => {
 	let view;
 
@@ -85,7 +83,7 @@ describe( 'LinkFormView', () => {
 
 		describe( 'url input view', () => {
 			it( 'has placeholder', () => {
-				expect( view.urlInputView.fieldView.placeholder ).to.equal( DEFAULT_PROTOCOL + 'example.com' );
+				expect( view.urlInputView.fieldView.placeholder ).to.equal( 'https://example.com' );
 			} );
 		} );