/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import LinkCommand from '../src/linkcommand';
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
describe( 'LinkCommand', () => {
let editor, model, command;
beforeEach( () => {
return ModelTestEditor.create()
.then( newEditor => {
editor = newEditor;
model = editor.model;
command = new LinkCommand( editor );
model.schema.extend( '$text', {
allowIn: '$root',
allowAttributes: 'linkHref'
} );
model.schema.register( 'p', { inheritAllFrom: '$block' } );
} );
} );
afterEach( () => {
return editor.destroy();
} );
describe( 'isEnabled', () => {
// This test doesn't tests every possible case.
// refresh() uses `isAttributeAllowedInSelection` helper which is fully tested in his own test.
beforeEach( () => {
model.schema.register( 'x', { inheritAllFrom: '$block' } );
model.schema.on( 'checkAttribute', ( evt, args ) => {
if ( args[ 0 ].endsWith( 'x $text' ) && args[ 1 ] == 'linkHref' ) {
evt.stop();
evt.return = false;
}
}, { priority: 'high' } );
} );
describe( 'when selection is collapsed', () => {
it( 'should be true if characters with the attribute can be placed at caret position', () => {
setData( model, '
f[]oo
' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should be false if characters with the attribute cannot be placed at caret position', () => {
setData( model, 'fo[]o' );
expect( command.isEnabled ).to.be.false;
} );
} );
describe( 'when selection is not collapsed', () => {
it( 'should be true if there is at least one node in selection that can have the attribute', () => {
setData( model, '[foo]
' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should be false if there are no nodes in selection that can have the attribute', () => {
setData( model, '[foo]' );
expect( command.isEnabled ).to.be.false;
} );
} );
} );
describe( 'value', () => {
describe( 'collapsed selection', () => {
it( 'should be equal attribute value when selection is placed inside element with `linkHref` attribute', () => {
setData( model, '<$text linkHref="url">foo[]bar$text>' );
expect( command.value ).to.equal( 'url' );
} );
it( 'should be undefined when selection is placed inside element without `linkHref` attribute', () => {
setData( model, '<$text bold="true">foo[]bar$text>' );
expect( command.value ).to.be.undefined;
} );
} );
describe( 'non-collapsed selection', () => {
it( 'should be equal attribute value when selection contains only elements with `linkHref` attribute', () => {
setData( model, 'fo[<$text linkHref="url">ob$text>]ar' );
expect( command.value ).to.equal( 'url' );
} );
it( 'should be undefined when selection contains not only elements with `linkHref` attribute', () => {
setData( model, 'f[o<$text linkHref="url">ob$text>]ar' );
expect( command.value ).to.be.undefined;
} );
} );
} );
describe( 'execute()', () => {
describe( 'non-collapsed selection', () => {
it( 'should set `linkHref` attribute to selected text', () => {
setData( model, 'f[ooba]r' );
expect( command.value ).to.be.undefined;
command.execute( 'url' );
expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">ooba$text>]r' );
expect( command.value ).to.equal( 'url' );
} );
it( 'should set `linkHref` attribute to selected text when text already has attributes', () => {
setData( model, 'f[o<$text bold="true">oba]r$text>' );
expect( command.value ).to.be.undefined;
command.execute( 'url' );
expect( command.value ).to.equal( 'url' );
expect( getData( model ) ).to.equal(
'f[<$text linkHref="url">o$text>' +
'<$text bold="true" linkHref="url">oba$text>]' +
'<$text bold="true">r$text>'
);
} );
it( 'should overwrite existing `linkHref` attribute when selected text wraps text with `linkHref` attribute', () => {
setData( model, 'f[o<$text linkHref="other url">o$text>ba]r' );
expect( command.value ).to.be.undefined;
command.execute( 'url' );
expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">ooba$text>]r' );
expect( command.value ).to.equal( 'url' );
} );
it( 'should split text and overwrite attribute value when selection is inside text with `linkHref` attribute', () => {
setData( model, 'f<$text linkHref="other url">o[ob]a$text>r' );
expect( command.value ).to.equal( 'other url' );
command.execute( 'url' );
expect( getData( model ) ).to.equal(
'f' +
'<$text linkHref="other url">o$text>' +
'[<$text linkHref="url">ob$text>]' +
'<$text linkHref="other url">a$text>' +
'r'
);
expect( command.value ).to.equal( 'url' );
} );
it( 'should overwrite `linkHref` attribute of selected text only, ' +
'when selection start inside text with `linkHref` attribute',
() => {
setData( model, 'f<$text linkHref="other url">o[o$text>ba]r' );
expect( command.value ).to.equal( 'other url' );
command.execute( 'url' );
expect( getData( model ) ).to.equal( 'f<$text linkHref="other url">o$text>[<$text linkHref="url">oba$text>]r' );
expect( command.value ).to.equal( 'url' );
} );
it( 'should overwrite `linkHref` attribute of selected text only, when selection end inside text with `linkHref` ' +
'attribute', () => {
setData( model, 'f[o<$text linkHref="other url">ob]a$text>r' );
expect( command.value ).to.be.undefined;
command.execute( 'url' );
expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">oob$text>]<$text linkHref="other url">a$text>r' );
expect( command.value ).to.equal( 'url' );
} );
it( 'should set `linkHref` attribute to selected text when text is split by $block element', () => {
setData( model, 'f[oo
ba]r
' );
expect( command.value ).to.be.undefined;
command.execute( 'url' );
expect( getData( model ) )
.to.equal( 'f[<$text linkHref="url">oo$text>
<$text linkHref="url">ba$text>]r
' );
expect( command.value ).to.equal( 'url' );
} );
it( 'should set `linkHref` attribute only to allowed elements and omit disallowed', () => {
model.schema.register( 'img', { allowWhere: '$text' } );
setData( model, 'f[oo
ba]r
' );
expect( command.value ).to.be.undefined;
command.execute( 'url' );
expect( getData( model ) )
.to.equal( 'f[<$text linkHref="url">oo$text>
<$text linkHref="url">ba$text>]r
' );
expect( command.value ).to.equal( 'url' );
} );
} );
describe( 'collapsed selection', () => {
it( 'should insert text with `linkHref` attribute, text data equal to href and select new link', () => {
setData( model, 'foo[]bar' );
command.execute( 'url' );
expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url$text>]bar' );
} );
it( 'should insert text with `linkHref` attribute, and selection attributes', () => {
setData( model, '<$text bold="true">foo[]bar$text>', {
selectionAttributes: { bold: true }
} );
command.execute( 'url' );
expect( getData( model ) ).to.equal(
'<$text bold="true">foo$text>[<$text bold="true" linkHref="url">url$text>]<$text bold="true">bar$text>'
);
} );
it( 'should update `linkHref` attribute and select whole link when selection is inside text with `linkHref` attribute', () => {
setData( model, '<$text linkHref="other url">foo[]bar$text>' );
command.execute( 'url' );
expect( getData( model ) ).to.equal( '[<$text linkHref="url">foobar$text>]' );
} );
it( 'should not insert text with `linkHref` attribute when is not allowed in parent', () => {
model.schema.on( 'checkAttribute', ( evt, args ) => {
if ( args[ 0 ].endsWith( 'p $text' ) && args[ 1 ] == 'linkHref' ) {
evt.stop();
evt.return = false;
}
}, { priority: 'high' } );
setData( model, 'foo[]bar
' );
command.execute( 'url' );
expect( getData( model ) ).to.equal( 'foo[]bar
' );
} );
} );
} );
} );