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-ui/144
Browse files Browse the repository at this point in the history
  • Loading branch information
oleq committed Nov 29, 2017
2 parents 9413ca1 + 5bdadce commit 90d8e49
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/linkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/

import Command from '@ckeditor/ckeditor5-core/src/command';
import Text from '@ckeditor/ckeditor5-engine/src/model/text';
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
import findLinkRange from './findlinkrange';
import toMap from '@ckeditor/ckeditor5-utils/src/tomap';

/**
* The link command. It is used by the {@link module:link/link~Link link feature}.
Expand Down Expand Up @@ -69,16 +69,20 @@ export default class LinkCommand extends Command {
// Then update `linkHref` value.
const linkRange = findLinkRange( selection.getFirstPosition(), selection.getAttribute( 'linkHref' ) );

batch.setAttribute( linkRange, 'linkHref', href );
batch.setAttribute( 'linkHref', href, linkRange );

// Create new range wrapping changed link.
selection.setRanges( [ linkRange ] );
}
// If not then insert text node with `linkHref` attribute in place of caret.
else {
const node = new Text( href, { linkHref: href } );
const attributes = toMap( doc.selection.getAttributes() );

batch.insert( position, node );
attributes.set( 'linkHref', href );

const node = batch.createText( href, attributes );

batch.insert( node, position );

// Create new range wrapping created node.
selection.setRanges( [ Range.createOn( node ) ] );
Expand All @@ -89,7 +93,7 @@ export default class LinkCommand extends Command {
const ranges = doc.schema.getValidRanges( selection.getRanges(), 'linkHref' );

for ( const range of ranges ) {
batch.setAttribute( range, 'linkHref', href );
batch.setAttribute( 'linkHref', href, range );
}
}
} );
Expand Down
2 changes: 1 addition & 1 deletion src/unlinkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class UnlinkCommand extends Command {

// Remove `linkHref` attribute from specified ranges.
for ( const range of rangesToUnlink ) {
batch.removeAttribute( range, 'linkHref' );
batch.removeAttribute( 'linkHref', range );
}
} );
}
Expand Down
10 changes: 10 additions & 0 deletions tests/linkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ describe( 'LinkCommand', () => {
expect( getData( document ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
} );

it( 'should insert text with `linkHref` attribute, and selection attributes', () => {
setData( document, '<$text bold="true">foo[]bar</$text>', {
selectionAttributes: { bold: true }
} );

command.execute( 'url' );

expect( getData( document ) ).to.equal( '<$text bold="true">foo[<$text linkHref="url">url</$text>]bar</$text>' );
} );

it( 'should update `linkHref` attribute and select whole link when selection is inside text with `linkHref` attribute', () => {
setData( document, '<$text linkHref="other url">foo[]bar</$text>' );

Expand Down

0 comments on commit 90d8e49

Please sign in to comment.