Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into t/ckeditor5/488
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/link.js
#	tests/link.js
  • Loading branch information
scofalik committed Feb 27, 2018
2 parents cca7360 + 985bb40 commit eff1854
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/linkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
4 changes: 4 additions & 0 deletions src/linkediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 changes: 8 additions & 0 deletions tests/linkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 23 additions & 0 deletions tests/linkediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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' );
Expand Down

0 comments on commit eff1854

Please sign in to comment.