/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; import ImageEngine from '../../src/image/imageengine'; 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 buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter'; import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter'; import { isImageWidget } from '../../src/image/utils'; describe( 'ImageEngine', () => { let editor, document, viewDocument; beforeEach( () => { return VirtualTestEditor.create( { plugins: [ ImageEngine ] } ) .then( newEditor => { editor = newEditor; document = editor.document; viewDocument = editor.editing.view; } ); } ); it( 'should be loaded', () => { expect( editor.plugins.get( ImageEngine ) ).to.be.instanceOf( ImageEngine ); } ); it( 'should set proper schema rules', () => { expect( document.schema.check( { name: 'image', attributes: [ 'src', 'alt' ], inside: '$root' } ) ).to.be.true; expect( document.schema.objects.has( 'image' ) ).to.be.true; } ); describe( 'conversion in data pipeline', () => { describe( 'model to view', () => { it( 'should convert', () => { setModelData( document, 'alt text' ); expect( editor.getData() ).to.equal( '
alt text
' ); } ); it( 'should convert without alt attribute', () => { setModelData( document, '' ); expect( editor.getData() ).to.equal( '
' ); } ); } ); describe( 'view to model', () => { it( 'should convert image figure', () => { editor.setData( '
alt text
' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( 'alt text' ); } ); it( 'should not convert if there is no image class', () => { editor.setData( '
My quote
' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( '' ); } ); it( 'should not convert if there is no img inside #1', () => { editor.setData( '
' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( '' ); } ); it( 'should not convert if there is no img inside #2', () => { editor.setData( '
test
' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( '' ); } ); it( 'should convert without alt attribute', () => { editor.setData( '
' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( '' ); } ); it( 'should not convert without src attribute', () => { editor.setData( '
alt text
' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( '' ); } ); it( 'should not convert in wrong context', () => { const data = editor.data; const editing = editor.editing; document.schema.registerItem( 'div', '$block' ); document.schema.disallow( { name: 'image', inside: 'div', attributes: [ 'src' ] } ); buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' ); buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' ); editor.setData( '
alt text
' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( '
' ); } ); it( 'should not convert if img is already consumed', () => { editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => { const img = data.input.getChild( 0 ); consumable.consume( img, { name: true } ); }, { priority: 'high' } ); editor.setData( '
alt text
' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( '' ); } ); it( 'should not convert if figure is already consumed', () => { editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => { const figure = data.input; consumable.consume( figure, { name: true, class: 'image' } ); }, { priority: 'high' } ); editor.setData( '
alt text
' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( '' ); } ); it( 'should dispatch conversion for nested elements', () => { const conversionSpy = sinon.spy(); editor.data.viewToModel.on( 'element:figcaption', conversionSpy ); editor.setData( '
alt text
' ); sinon.assert.calledOnce( conversionSpy ); } ); it( 'should convert bare img element', () => { editor.setData( 'alt text' ); expect( getModelData( document, { withoutSelection: true } ) ) .to.equal( 'alt text' ); } ); it( 'should not convert alt attribute on non-img element', () => { const data = editor.data; const editing = editor.editing; document.schema.registerItem( 'div', '$block' ); document.schema.allow( { name: 'div', attributes: 'alt', inside: '$root' } ); buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' ); buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' ); editor.setData( '
' ); expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '
' ); } ); it( 'should handle figure with two images', () => { document.schema.allow( { name: '$text', inside: 'image' } ); editor.setData( '
abc
' ); expect( getModelData( document, { withoutSelection: true } ) ).to.equal( 'abc' ); } ); } ); } ); describe( 'conversion in editing pipeline', () => { describe( 'model to view', () => { it( 'should convert', () => { setModelData( document, 'alt text' ); expect( getViewData( viewDocument, { withoutSelection: true } ) ) .to.equal( '
alt text
' ); } ); it( 'converted element should be widgetized', () => { setModelData( document, 'alt text' ); const figure = viewDocument.getRoot().getChild( 0 ); expect( figure.name ).to.equal( 'figure' ); expect( isImageWidget( figure ) ).to.be.true; } ); it( 'should convert attribute change', () => { setModelData( document, 'alt text' ); const image = document.getRoot().getChild( 0 ); document.enqueueChanges( () => { const batch = document.batch(); batch.setAttribute( image, 'alt', 'new text' ); } ); expect( getViewData( viewDocument, { withoutSelection: true } ) ) .to.equal( '
new text
' ); } ); it( 'should convert attribute removal', () => { setModelData( document, 'alt text' ); const image = document.getRoot().getChild( 0 ); document.enqueueChanges( () => { const batch = document.batch(); batch.removeAttribute( image, 'alt' ); } ); expect( getViewData( viewDocument, { withoutSelection: true } ) ) .to.equal( '
' ); } ); it( 'should not convert change if is already consumed', () => { setModelData( document, 'alt text' ); const image = document.getRoot().getChild( 0 ); editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => { consumable.consume( data.item, 'removeAttribute:alt' ); }, { priority: 'high' } ); document.enqueueChanges( () => { const batch = document.batch(); batch.removeAttribute( image, 'alt' ); } ); expect( getViewData( viewDocument, { withoutSelection: true } ) ) .to.equal( '
alt text
' ); } ); } ); } ); } );