瀏覽代碼

Merge branch 'master' into t/ckeditor5/488

# Conflicts:
#	src/link.js
#	tests/link.js
Szymon Cofalik 7 年之前
父節點
當前提交
8f561f93d5

+ 3 - 1
packages/ckeditor5-link/src/linkcommand.js

@@ -73,7 +73,9 @@ export default class LinkCommand extends Command {
 					writer.setSelection( linkRange );
 				}
 				// If not then insert text node with `linkHref` attribute in place of caret.
-				else {
+				// However, since selection in collapsed, attribute value will be used as data for text node.
+				// So, if `href` is empty, do not create text node.
+				else if ( href !== '' ) {
 					const attributes = toMap( selection.getAttributes() );
 
 					attributes.set( 'linkHref', href );

+ 4 - 0
packages/ckeditor5-link/src/linkediting.js

@@ -13,6 +13,7 @@ import { upcastElementToAttribute } from '@ckeditor/ckeditor5-engine/src/convers
 import LinkCommand from './linkcommand';
 import UnlinkCommand from './unlinkcommand';
 import { createLinkElement } from './utils';
+import bindTwoStepCaretToAttribute from '@ckeditor/ckeditor5-engine/src/utils/bindtwostepcarettoattribute';
 
 /**
  * The link engine feature.
@@ -51,5 +52,8 @@ export default class LinkEditing extends Plugin {
 		// Create linking commands.
 		editor.commands.add( 'link', new LinkCommand( editor ) );
 		editor.commands.add( 'unlink', new UnlinkCommand( editor ) );
+
+		// Enable two-step caret movement for `linkHref` attribute.
+		bindTwoStepCaretToAttribute( editor.editing.view, editor.model, this, 'linkHref' );
 	}
 }

+ 8 - 0
packages/ckeditor5-link/tests/linkcommand.js

@@ -249,6 +249,14 @@ describe( 'LinkCommand', () => {
 
 				expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
 			} );
+
+			it( 'should not insert text node if link is empty', () => {
+				setData( model, '<p>foo[]bar</p>' );
+
+				command.execute( '' );
+
+				expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
+			} );
 		} );
 	} );
 } );

+ 23 - 0
packages/ckeditor5-link/tests/linkediting.js

@@ -12,9 +12,12 @@ import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 import { isLinkElement } from '../src/utils';
+import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 
 import { downcastAttributeToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
 
+/* global document */
+
 describe( 'LinkEditing', () => {
 	let editor, model;
 
@@ -40,6 +43,26 @@ describe( 'LinkEditing', () => {
 		expect( model.schema.checkAttribute( [ '$block' ], 'linkHref' ) ).to.be.false;
 	} );
 
+	it( 'should bind two-step caret movement to `linkHref` attribute', () => {
+		// Let's check only the minimum to not duplicated `bindTwoStepCaretToAttribute()` tests.
+		// Testing minimum is better then testing using spies that might give false positive results.
+
+		// Put selection before the link element.
+		setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
+
+		// The selection's gravity is not overridden because selection land here not as a result of `keydown`.
+		expect( editor.model.document.selection.isGravityOverridden ).to.false;
+
+		// So let's simulate `keydown` event.
+		editor.editing.view.document.fire( 'keydown', {
+			keyCode: keyCodes.arrowright,
+			preventDefault: () => {},
+			domTarget: document.body
+		} );
+
+		expect( editor.model.document.selection.isGravityOverridden ).to.true;
+	} );
+
 	describe( 'command', () => {
 		it( 'should register link command', () => {
 			const command = editor.commands.get( 'link' );