/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js'; import ImageStyleEngine from 'ckeditor5/image/imagestyle/imagestyleengine.js'; import ImageEngine from 'ckeditor5/image/imageengine.js'; import ImageStyleCommand from 'ckeditor5/image/imagestyle/imagestylecommand.js'; import { getData as getModelData, setData as setModelData } from 'ckeditor5/engine/dev-utils/model.js'; import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js'; describe( 'ImageStyleEngine', () => { let editor, document, viewDocument; beforeEach( () => { return VirtualTestEditor.create( { plugins: [ ImageStyleEngine ], image: { styles: { imageStyleDummy: { title: 'Dummy style', icon: 'dummy-icon', value: 'dummy', className: 'image-style-dummy' } } } } ) .then( newEditor => { editor = newEditor; document = editor.document; viewDocument = editor.editing.view; } ); } ); it( 'should be loaded', () => { expect( editor.plugins.get( ImageStyleEngine ) ).to.be.instanceOf( ImageStyleEngine ); } ); it( 'should load image engine', () => { expect( editor.plugins.get( ImageEngine ) ).to.be.instanceOf( ImageEngine ); } ); it( 'should set schema rules for image style', () => { const schema = document.schema; expect( schema.check( { name: 'image', attributes: [ 'imageStyle', 'src' ], inside: '$root' } ) ).to.be.true; } ); it( 'should register command', () => { expect( editor.commands.has( 'imagestyle' ) ).to.be.true; const command = editor.commands.get( 'imagestyle' ); expect( command ).to.be.instanceOf( ImageStyleCommand ); } ); it( 'should convert from view to model', () => { editor.setData( '
' ); expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' ); } ); it( 'should not convert from view to model if class is not defined', () => { editor.setData( '
' ); expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' ); } ); it( 'should not convert from view to model when not in image figure', () => { editor.setData( '
' ); expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' ); } ); it( 'should not convert from view to model if schema prevents it', () => { document.schema.disallow( { name: 'image', attributes: 'imageStyle' } ); editor.setData( '
' ); expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' ); } ); it( 'should convert model to view: adding attribute', () => { setModelData( document, '' ); const image = document.getRoot().getChild( 0 ); const batch = document.batch(); document.enqueueChanges( () => { batch.setAttribute( image, 'imageStyle', 'side' ); } ); expect( editor.getData() ).to.equal( '
' ); } ); it( 'should convert model to view: removing attribute', () => { setModelData( document, '' ); const image = document.getRoot().getChild( 0 ); const batch = document.batch(); document.enqueueChanges( () => { batch.setAttribute( image, 'imageStyle', null ); } ); expect( editor.getData() ).to.equal( '
' ); } ); it( 'should convert model to view: change attribute', () => { setModelData( document, '' ); const image = document.getRoot().getChild( 0 ); const batch = document.batch(); document.enqueueChanges( () => { batch.setAttribute( image, 'imageStyle', 'side' ); } ); expect( editor.getData() ).to.equal( '
' ); } ); it( 'should not convert from model to view if already consumed: adding attribute', () => { editor.editing.modelToView.on( 'addAttribute:imageStyle', ( evt, data, consumable ) => { consumable.consume( data.item, 'addAttribute:imageStyle' ); }, { priority: 'high' } ); setModelData( document, '' ); const image = document.getRoot().getChild( 0 ); const batch = document.batch(); document.enqueueChanges( () => { batch.setAttribute( image, 'imageStyle', 'side' ); } ); expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '
' ); } ); it( 'should not convert from model to view if already consumed: removing attribute', () => { editor.editing.modelToView.on( 'removeAttribute:imageStyle', ( evt, data, consumable ) => { consumable.consume( data.item, 'removeAttribute:imageStyle' ); }, { priority: 'high' } ); setModelData( document, '' ); const image = document.getRoot().getChild( 0 ); const batch = document.batch(); document.enqueueChanges( () => { batch.setAttribute( image, 'imageStyle', null ); } ); expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '
' ); } ); it( 'should not convert from model to view if already consumed: change attribute', () => { editor.editing.modelToView.on( 'changeAttribute:imageStyle', ( evt, data, consumable ) => { consumable.consume( data.item, 'changeAttribute:imageStyle' ); }, { priority: 'high' } ); setModelData( document, '' ); const image = document.getRoot().getChild( 0 ); const batch = document.batch(); document.enqueueChanges( () => { batch.setAttribute( image, 'imageStyle', 'side' ); } ); expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '
' ); } ); it( 'should not convert from model to view if style is not present: adding attribute', () => { setModelData( document, '' ); const image = document.getRoot().getChild( 0 ); const batch = document.batch(); document.enqueueChanges( () => { batch.setAttribute( image, 'imageStyle', 'foo' ); } ); expect( editor.getData() ).to.equal( '
' ); } ); it( 'should not convert from model to view if style is not present: change attribute', () => { setModelData( document, '' ); const image = document.getRoot().getChild( 0 ); const batch = document.batch(); document.enqueueChanges( () => { batch.setAttribute( image, 'imageStyle', 'foo' ); } ); expect( editor.getData() ).to.equal( '
' ); } ); it( 'should not convert from model to view if style is not present: remove attribute', () => { setModelData( document, '' ); const image = document.getRoot().getChild( 0 ); const batch = document.batch(); document.enqueueChanges( () => { batch.setAttribute( image, 'imageStyle', null ); } ); expect( editor.getData() ).to.equal( '
' ); } ); } );