/** * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; import ImageCaptionEditing from '../../src/imagecaption/imagecaptionediting'; import ImageEditing from '../../src/image/imageediting'; import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import ViewAttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement'; import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position'; import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element'; import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range'; 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 env from '@ckeditor/ckeditor5-utils/src/env'; describe( 'ImageCaptionEditing', () => { let editor, model, doc, view; beforeEach( () => { // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`. sinon.stub( env, 'isEdge' ).get( () => false ); return VirtualTestEditor .create( { plugins: [ ImageCaptionEditing, ImageEditing, UndoEditing, Paragraph ] } ) .then( newEditor => { editor = newEditor; model = editor.model; doc = model.document; view = editor.editing.view; model.schema.register( 'widget' ); model.schema.extend( 'widget', { allowIn: '$root' } ); model.schema.extend( 'caption', { allowIn: 'widget' } ); model.schema.extend( '$text', { allowIn: 'widget' } ); editor.conversion.elementToElement( { model: 'widget', view: 'widget' } ); } ); } ); it( 'should be loaded', () => { expect( editor.plugins.get( ImageCaptionEditing ) ).to.be.instanceOf( ImageCaptionEditing ); } ); it( 'should set proper schema rules', () => { expect( model.schema.checkChild( [ '$root', 'image' ], 'caption' ) ).to.be.true; expect( model.schema.checkChild( [ '$root', 'image', 'caption' ], '$text' ) ).to.be.true; expect( model.schema.isLimit( 'caption' ) ).to.be.true; expect( model.schema.checkChild( [ '$root', 'image', 'caption' ], 'caption' ) ).to.be.false; model.schema.extend( '$block', { allowAttributes: 'aligmnent' } ); expect( model.schema.checkAttribute( [ '$root', 'image', 'caption' ], 'alignment' ) ).to.be.false; } ); describe( 'data pipeline', () => { describe( 'view to model', () => { it( 'should convert figcaption inside image figure', () => { editor.setData( '
foo bar
' ); expect( getModelData( model, { withoutSelection: true } ) ) .to.equal( 'foo bar' ); } ); it( 'should add empty caption if there is no figcaption', () => { editor.setData( '
' ); expect( getModelData( model, { withoutSelection: true } ) ) .to.equal( '' ); } ); it( 'should not convert figcaption inside other elements than image', () => { editor.setData( '
foobar
' ); expect( getModelData( model, { withoutSelection: true } ) ) .to.equal( 'foobar' ); } ); } ); describe( 'model to view', () => { it( 'should convert caption element to figcaption', () => { setModelData( model, 'Foo bar baz.' ); expect( editor.getData() ).to.equal( '
Foo bar baz.
' ); } ); it( 'should not convert caption to figcaption if it\'s empty', () => { setModelData( model, '' ); expect( editor.getData() ).to.equal( '
' ); } ); it( 'should not convert caption from other elements', () => { setModelData( model, 'foo bar' ); expect( editor.getData() ).to.equal( 'foo bar' ); } ); } ); } ); describe( 'editing pipeline', () => { describe( 'model to view', () => { it( 'should convert caption element to figcaption contenteditable', () => { setModelData( model, 'Foo bar baz.' ); expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '
' + '' + '
' + 'Foo bar baz.' + '
' + '
' ); } ); it( 'should convert caption to element with proper CSS class if it\'s empty', () => { setModelData( model, 'foo' ); expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '

foo

' + '
' + '' + '
' + '
' + '
' ); } ); it( 'should not convert caption from other elements', () => { setModelData( model, 'foo bar' ); expect( getViewData( view, { withoutSelection: true } ) ).to.equal( 'foo bar' ); } ); it( 'should not convert when element is already consumed', () => { editor.editing.downcastDispatcher.on( 'insert:caption', ( evt, data, conversionApi ) => { conversionApi.consumable.consume( data.item, 'insert' ); const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent ); const viewElement = new ViewAttributeElement( 'span' ); const viewPosition = ViewPosition.createAt( imageFigure, 'end' ); conversionApi.mapper.bindElements( data.item, viewElement ); conversionApi.writer.insert( viewPosition, viewElement ); }, { priority: 'high' } ); setModelData( model, 'Foo bar baz.' ); expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '
Foo bar baz.
' ); } ); it( 'should show caption when something is inserted inside', () => { setModelData( model, 'foo' ); const image = doc.getRoot().getChild( 1 ); const caption = image.getChild( 0 ); model.change( writer => { writer.insertText( 'foo bar', caption ); } ); expect( getViewData( view ) ).to.equal( '

{}foo

' + '
' + '' + '
' + 'foo bar' + '
' + '
' ); } ); it( 'should hide when everything is removed from caption', () => { setModelData( model, 'foofoo bar baz' ); const image = doc.getRoot().getChild( 1 ); const caption = image.getChild( 0 ); model.change( writer => { writer.remove( ModelRange.createIn( caption ) ); } ); expect( getViewData( view ) ).to.equal( '

{}foo

' + '
' + '' + '
' + '
' + '
' ); } ); it( 'should show when not everything is removed from caption', () => { setModelData( model, 'foofoo bar baz' ); const image = doc.getRoot().getChild( 1 ); const caption = image.getChild( 0 ); model.change( writer => { writer.remove( ModelRange.createFromParentsAndOffsets( caption, 0, caption, 8 ) ); } ); expect( getViewData( view ) ).to.equal( '

{}foo

' + '
' + '' + '
baz
' + '
' ); } ); } ); } ); describe( 'inserting image to the document', () => { it( 'should add caption element if image does not have it', () => { model.change( writer => { writer.insertElement( 'image', { src: '', alt: '' }, doc.getRoot() ); } ); expect( getModelData( model ) ).to.equal( '[]' ); expect( getViewData( view ) ).to.equal( '[
' + '' + '
' + '
' + '
]' + '

' ); } ); it( 'should not add caption element if image already have it', () => { const caption = new ModelElement( 'caption', null, 'foo bar' ); const image = new ModelElement( 'image', { src: '', alt: '' }, caption ); model.change( writer => { writer.insert( image, doc.getRoot() ); } ); expect( getModelData( model ) ).to.equal( '[foo bar]' ); expect( getViewData( view ) ).to.equal( '[
' + '' + '
' + 'foo bar' + '
' + '
]' + '

' ); } ); it( 'should not add caption element twice', () => { const image = new ModelElement( 'image', { src: '', alt: '' } ); const caption = new ModelElement( 'caption' ); model.change( writer => { // Since we are adding an empty image, this should trigger caption fixer. writer.insert( image, doc.getRoot() ); // Add caption just after the image is inserted, in same batch. writer.insert( caption, image ); } ); // Check whether caption fixer added redundant caption. expect( getModelData( model ) ).to.equal( '[]' ); expect( getViewData( view ) ).to.equal( '[
' + '' + '
' + '
]' + '

' ); } ); it( 'should do nothing for other changes than insert', () => { setModelData( model, 'foo bar' ); const image = doc.getRoot().getChild( 0 ); model.change( writer => { writer.setAttribute( 'alt', 'alt text', image ); } ); expect( getModelData( model, { withoutSelection: true } ) ).to.equal( 'alt textfoo bar' ); } ); } ); describe( 'editing view', () => { it( 'image should have empty figcaption element when is selected', () => { setModelData( model, 'foo[]' ); expect( getViewData( view ) ).to.equal( '

foo

' + '[
' + '' + '
' + '
' + '
]' ); } ); it( 'image should have empty figcaption element with hidden class when not selected', () => { setModelData( model, '[]foo' ); expect( getViewData( view ) ).to.equal( '

{}foo

' + '
' + '' + '
' + '
' + '
' ); } ); it( 'should not add additional figcaption if one is already present', () => { setModelData( model, 'foo[foo bar]' ); expect( getViewData( view ) ).to.equal( '

foo

' + '[
' + '' + '
foo bar
' + '
]' ); } ); it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => { setModelData( model, 'foo[]' ); model.change( writer => { writer.setSelection( null ); } ); expect( getViewData( view ) ).to.equal( '

{}foo

' + '
' + '' + '
' + '
' + '
' ); } ); it( 'should not remove figcaption when selection is inside it even when it is empty', () => { setModelData( model, '[foo bar]' ); model.change( writer => { writer.remove( doc.selection.getFirstRange() ); } ); expect( getViewData( view ) ).to.equal( '
' + '' + '
' + '[]' + '
' + '
' ); } ); it( 'should not remove figcaption when selection is moved from it to its image', () => { setModelData( model, '[foo bar]' ); const image = doc.getRoot().getChild( 0 ); model.change( writer => { writer.remove( doc.selection.getFirstRange() ); writer.setSelection( ModelRange.createOn( image ) ); } ); expect( getViewData( view ) ).to.equal( '[
' + '' + '
' + '
]' ); } ); it( 'should not remove figcaption when selection is moved from it to other image', () => { setModelData( model, '[foo bar]' ); const image = doc.getRoot().getChild( 1 ); model.change( writer => { writer.setSelection( ModelRange.createOn( image ) ); } ); expect( getViewData( view ) ).to.equal( '
' + '' + '
foo bar
' + '
' + '[
' + '' + '
' + '
]' ); } ); describe( 'undo/redo integration', () => { it( 'should create view element after redo', () => { setModelData( model, 'foo[foo bar baz]' ); const modelRoot = doc.getRoot(); const modelImage = modelRoot.getChild( 1 ); const modelCaption = modelImage.getChild( 0 ); // Remove text and selection from caption. model.change( writer => { writer.remove( ModelRange.createIn( modelCaption ) ); writer.setSelection( null ); } ); // Check if there is no figcaption in the view. expect( getViewData( view ) ).to.equal( '

{}foo

' + '
' + '' + '
' + '
' + '
' ); editor.execute( 'undo' ); // Check if figcaption is back with contents. expect( getViewData( view ) ).to.equal( '

foo

' + '
' + '' + '
' + '{foo bar baz}' + '
' + '
' ); } ); it( 'undo should work after inserting the image', () => { setModelData( model, 'foo[]' ); model.change( writer => { const image = writer.createElement( 'image', { src: '/foo.png' } ); writer.insert( image, doc.getRoot() ); } ); expect( getModelData( model ) ).to.equal( 'foo[]' ); editor.execute( 'undo' ); expect( getModelData( model ) ).to.equal( 'foo[]' ); } ); } ); } ); } );