/** * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; import LinkCommand from '../src/linkcommand'; import ManualDecorator from '../src/utils/manualdecorator'; 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', 'bold' ] } ); 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.addAttributeCheck( ( ctx, attributeName ) => { if ( ctx.endsWith( 'x $text' ) && attributeName == 'linkHref' ) { return false; } } ); } ); 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, '[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, '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[ooba]r
f[<$text linkHref="url">oo$text><$text linkHref="url">ba$text>]r
foo[]bar
' ); command.execute( 'url' ); expect( getData( model ) ).to.equal( 'foo[]bar
' ); } ); it( 'should not insert text node if link is empty', () => { setData( model, 'foo[]bar
' ); command.execute( '' ); expect( getData( model ) ).to.equal( 'foo[]bar
' ); } ); } ); } ); describe( 'manual decorators', () => { beforeEach( () => { editor.destroy(); return ModelTestEditor.create() .then( newEditor => { editor = newEditor; model = editor.model; command = new LinkCommand( editor ); command.manualDecorators.add( new ManualDecorator( { id: 'linkManualDecorator0', label: 'Foo', attributes: { class: 'Foo' } } ) ); command.manualDecorators.add( new ManualDecorator( { id: 'linkManualDecorator1', label: 'Bar', attributes: { target: '_blank' } } ) ); model.schema.extend( '$text', { allowIn: '$root', allowAttributes: [ 'linkHref', 'linkManualDecorator0', 'linkManualDecorator1' ] } ); model.schema.register( 'p', { inheritAllFrom: '$block' } ); } ); } ); afterEach( () => { return editor.destroy(); } ); describe( 'collapsed selection', () => { it( 'should insert additional attributes to link when it is created', () => { setData( model, 'foo[]bar' ); command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } ); expect( getData( model ) ).to .equal( 'foo[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">url$text>]bar' ); } ); it( 'should add additional attributes to link when link is modified', () => { setData( model, 'f<$text linkHref="url">o[]oba$text>r' ); command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } ); expect( getData( model ) ).to .equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba$text>]r' ); } ); it( 'should remove additional attributes to link if those are falsy', () => { setData( model, 'foo<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true" >u[]rl$text>bar' ); command.execute( 'url', { linkManualDecorator0: false, linkManualDecorator1: false } ); expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url$text>]bar' ); } ); } ); describe( 'range selection', () => { it( 'should insert additional attributes to link when it is created', () => { setData( model, 'f[ooba]r' ); command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } ); expect( getData( model ) ).to .equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba$text>]r' ); } ); it( 'should add additional attributes to link when link is modified', () => { setData( model, 'f[<$text linkHref="foo">ooba$text>]r' ); command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } ); expect( getData( model ) ).to .equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba$text>]r' ); } ); it( 'should remove additional attributes to link if those are falsy', () => { setData( model, 'foo[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true" >url$text>]bar' ); command.execute( 'url', { linkManualDecorator0: false, linkManualDecorator1: false } ); expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url$text>]bar' ); } ); } ); } ); } );