/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /* global document */ import Link from '../src/link'; import Image from '@ckeditor/ckeditor5-image/src/image'; import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; describe( 'Link', () => { let editor, doc, element, linkCommand, unlinkCommand; beforeEach( () => { element = document.createElement( 'div' ); document.body.appendChild( element ); return ClassicTestEditor.create( element, { plugins: [ Paragraph, Image, ImageCaption, Link ] } ) .then( newEditor => { editor = newEditor; doc = editor.document; linkCommand = editor.commands.get( 'link' ); unlinkCommand = editor.commands.get( 'unlink' ); } ); } ); afterEach( () => { element.remove(); return editor.destroy(); } ); describe( 'compatibility with images', () => { it( 'does not link a caption–less image', () => { element = document.createElement( 'div' ); document.body.appendChild( element ); return ClassicTestEditor.create( element, { plugins: [ Paragraph, Image, Link ] } ) .then( newEditor => { editor = newEditor; doc = editor.document; linkCommand = editor.commands.get( 'link' ); unlinkCommand = editor.commands.get( 'unlink' ); setModelData( doc, 'fo[o' + '' + 'b]ar' ); editor.execute( 'link', 'url' ); expect( getModelData( doc ) ).to.equal( 'fo[<$text linkHref="url">o' + '' + '<$text linkHref="url">b]ar' ); expect( linkCommand.isEnabled ).to.be.true; expect( linkCommand.value ).to.equal( 'url' ); expect( unlinkCommand.isEnabled ).to.be.true; element.remove(); return editor.destroy(); } ); } ); it( 'links the image caption text if selection contains more than an image', () => { setModelData( doc, 'fo[o' + '' + 'abc' + '' + 'baz' + '' + 'abc' + '' + 'b]ar' ); editor.execute( 'link', 'url' ); expect( getModelData( doc ) ).to.equal( 'fo[<$text linkHref="url">o' + '' + '<$text linkHref="url">abc' + '' + '<$text linkHref="url">baz' + '' + '<$text linkHref="url">abc' + '' + '<$text linkHref="url">b]ar' ); expect( linkCommand.isEnabled ).to.be.true; expect( linkCommand.value ).to.equal( 'url' ); expect( unlinkCommand.isEnabled ).to.be.true; } ); it( 'links the image caption text if selection is in the image caption', () => { setModelData( doc, 'foo' + '' + 'a[b]c' + '' + 'bar' ); editor.execute( 'link', 'url' ); expect( getModelData( doc ) ).to.equal( 'foo' + '' + 'a[<$text linkHref="url">b]c' + '' + 'bar' ); expect( linkCommand.isEnabled ).to.be.true; expect( linkCommand.value ).to.equal( 'url' ); expect( unlinkCommand.isEnabled ).to.be.true; } ); // https://github.com/ckeditor/ckeditor5-link/issues/85 it( 'is disabled when only the image is the only selected element', () => { setModelData( doc, '[]' ); expect( linkCommand.isEnabled ).to.be.false; setModelData( doc, '[abc]' ); expect( linkCommand.isEnabled ).to.be.false; setModelData( doc, '[' + '<$text linkHref="url">abc' + ']' ); expect( linkCommand.isEnabled ).to.be.false; expect( unlinkCommand.isEnabled ).to.be.false; } ); } ); } );