| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- /**
- * @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 ImageStyleEngine from '../../src/imagestyle/imagestyleengine';
- import ImageEngine from '../../src/image/imageengine';
- import ImageStyleCommand from '../../src/imagestyle/imagestylecommand';
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- import log from '@ckeditor/ckeditor5-utils/src/log';
- 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 fullWidthIcon from '@ckeditor/ckeditor5-core/theme/icons/object-full-width.svg';
- import leftIcon from '@ckeditor/ckeditor5-core/theme/icons/object-left.svg';
- import centerIcon from '@ckeditor/ckeditor5-core/theme/icons/object-center.svg';
- import rightIcon from '@ckeditor/ckeditor5-core/theme/icons/object-right.svg';
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- testUtils.createSinonSandbox();
- describe( 'ImageStyleEngine', () => {
- let editor, plugin, document, viewDocument;
- afterEach( () => {
- editor.destroy();
- } );
- describe( 'plugin', () => {
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ],
- } )
- .then( newEditor => {
- editor = newEditor;
- } );
- } );
- 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 );
- } );
- } );
- describe( 'init', () => {
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ],
- image: {
- styles: [
- { name: 'fullStyle', title: 'foo', icon: 'object-center', isDefault: true },
- { name: 'sideStyle', title: 'bar', icon: 'object-right', className: 'side-class' },
- { name: 'dummyStyle', title: 'baz', icon: 'object-dummy', className: 'dummy-class' },
- ]
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- document = editor.document;
- viewDocument = editor.editing.view;
- } );
- } );
- it( 'should define image.styles config', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ]
- } )
- .then( newEditor => {
- editor = newEditor;
- expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleFull', 'imageStyleSide' ] );
- } );
- } );
- 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 separate command for each style', () => {
- expect( editor.commands.get( 'fullStyle' ) ).to.be.instanceOf( ImageStyleCommand );
- expect( editor.commands.get( 'sideStyle' ) ).to.be.instanceOf( ImageStyleCommand );
- expect( editor.commands.get( 'dummyStyle' ) ).to.be.instanceOf( ImageStyleCommand );
- } );
- it( 'should convert from view to model', () => {
- editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
- expect( getModelData( document, { withoutSelection: true } ) )
- .to.equal( '<image imageStyle="sideStyle" src="foo.png"></image>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image side-class" contenteditable="false">' +
- '<img src="foo.png"></img>' +
- '</figure>' );
- } );
- it( 'should not convert from view to model if class is not defined', () => {
- editor.setData( '<figure class="image foo-bar"><img src="foo.png" /></figure>' );
- expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- it( 'should not convert from view to model when not in image figure', () => {
- editor.setData( '<figure class="side-class"></figure>' );
- expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' );
- expect( getViewData( viewDocument, { 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( '<figure class="image side-class"><img src="foo.png" /></figure>' );
- expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- it( 'should convert model to view: adding attribute', () => {
- setModelData( document, '<image src="foo.png"></image>' );
- const image = document.getRoot().getChild( 0 );
- const batch = document.batch();
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', 'sideStyle', image );
- } );
- expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- it( 'should convert model to view: removing attribute', () => {
- setModelData( document, '<image src="foo.png" imageStyle="sideStyle"></image>' );
- const image = document.getRoot().getChild( 0 );
- const batch = document.batch();
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', null, image );
- } );
- expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- it( 'should convert model to view: change attribute', () => {
- setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
- const image = document.getRoot().getChild( 0 );
- const batch = document.batch();
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', 'sideStyle', image );
- } );
- expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
- // https://github.com/ckeditor/ckeditor5-image/issues/132
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', 'dummyStyle', image );
- } );
- expect( editor.getData() ).to.equal( '<figure class="image dummy-class"><img src="foo.png"></figure>' );
- // https://github.com/ckeditor/ckeditor5-image/issues/132
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget dummy-class image" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- 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, '<image src="foo.png"></image>' );
- const image = document.getRoot().getChild( 0 );
- const batch = document.batch();
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', 'sideStyle', image );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- 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, '<image src="foo.png" imageStyle="sideStyle"></image>' );
- const image = document.getRoot().getChild( 0 );
- const batch = document.batch();
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', null, image );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- 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, '<image src="foo.png" imageStyle="dummyStyle"></image>' );
- const image = document.getRoot().getChild( 0 );
- const batch = document.batch();
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', 'sideStyle', image );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget dummy-class image" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- it( 'should not convert from model to view if style is not present: adding attribute', () => {
- setModelData( document, '<image src="foo.png"></image>' );
- const image = document.getRoot().getChild( 0 );
- const batch = document.batch();
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', 'foo', image );
- } );
- expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- it( 'should not convert from model to view if style is not present: change attribute', () => {
- setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
- const image = document.getRoot().getChild( 0 );
- const batch = document.batch();
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', 'foo', image );
- } );
- expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- it( 'should not convert from model to view if style is not present: remove attribute', () => {
- setModelData( document, '<image src="foo.png" imageStyle="foo"></image>' );
- const image = document.getRoot().getChild( 0 );
- const batch = document.batch();
- document.enqueueChanges( () => {
- batch.setAttribute( 'imageStyle', null, image );
- } );
- expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
- );
- } );
- } );
- describe( 'imageStyles()', () => {
- it( 'should fall back to defaults when no image.styles', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ]
- } )
- .then( newEditor => {
- editor = newEditor;
- expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleFull', 'imageStyleSide' ] );
- } );
- } );
- it( 'should not alter the image.styles config', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ],
- image: {
- styles: [
- 'imageStyleSide'
- ]
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleSide' ] );
- } );
- } );
- it( 'should not alter object definitions in the image.styles config', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ],
- image: {
- styles: [
- { name: 'imageStyleSide' }
- ]
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ { name: 'imageStyleSide' } ] );
- } );
- } );
- it( 'should cache the styles', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ]
- } )
- .then( newEditor => {
- editor = newEditor;
- plugin = editor.plugins.get( ImageStyleEngine );
- expect( plugin.imageStyles ).to.equal( plugin.imageStyles );
- } );
- } );
- describe( 'object format', () => {
- beforeEach( () => {
- class TranslationMock extends Plugin {
- init() {
- sinon.stub( this.editor, 't' ).returns( 'Default translation' );
- }
- }
- return VirtualTestEditor
- .create( {
- plugins: [ TranslationMock, ImageStyleEngine ],
- image: {
- styles: [
- // Custom user styles.
- { name: 'foo', title: 'foo', icon: 'custom', isDefault: true, className: 'foo-class' },
- { name: 'bar', title: 'bar', icon: 'right', className: 'bar-class' },
- { name: 'baz', title: 'Side image', icon: 'custom', className: 'baz-class' },
- // Customized default styles.
- { name: 'imageStyleFull', icon: 'left', title: 'Custom title' }
- ]
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- plugin = editor.plugins.get( ImageStyleEngine );
- } );
- } );
- it( 'should pass through if #name not found in default styles', () => {
- expect( plugin.imageStyles[ 0 ] ).to.deep.equal( {
- name: 'foo',
- title: 'foo',
- icon: 'custom',
- isDefault: true,
- className: 'foo-class'
- } );
- } );
- it( 'should use one of default icons if #icon matches', () => {
- expect( plugin.imageStyles[ 1 ].icon ).to.equal( ImageStyleEngine.defaultIcons.right );
- } );
- it( 'should use one of default translations if #title matches', () => {
- expect( plugin.imageStyles[ 2 ].title ).to.deep.equal( 'Default translation' );
- } );
- it( 'should extend one of default styles if #name matches', () => {
- expect( plugin.imageStyles[ 3 ] ).to.deep.equal( {
- name: 'imageStyleFull',
- title: 'Custom title',
- icon: ImageStyleEngine.defaultIcons.left,
- isDefault: true
- } );
- } );
- } );
- describe( 'string format', () => {
- it( 'should use one of default styles if #name matches', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ],
- image: {
- styles: [ 'imageStyleFull' ]
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- plugin = editor.plugins.get( ImageStyleEngine );
- expect( plugin.imageStyles[ 0 ] ).to.deep.equal( ImageStyleEngine.defaultStyles.imageStyleFull );
- } );
- } );
- it( 'should warn if a #name not found in default styles', () => {
- testUtils.sinon.stub( log, 'warn' );
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ],
- image: {
- styles: [ 'foo' ]
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- plugin = editor.plugins.get( ImageStyleEngine );
- expect( plugin.imageStyles[ 0 ] ).to.deep.equal( {
- name: 'foo'
- } );
- sinon.assert.calledOnce( log.warn );
- sinon.assert.calledWithExactly( log.warn,
- sinon.match( /^image-style-not-found/ ),
- { name: 'foo' }
- );
- } );
- } );
- } );
- } );
- describe( 'localizedDefaultStylesTitles()', () => {
- it( 'should return localized titles of default styles', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageStyleEngine ]
- } )
- .then( newEditor => {
- editor = newEditor;
- plugin = editor.plugins.get( ImageStyleEngine );
- expect( plugin.localizedDefaultStylesTitles ).to.deep.equal( {
- 'Full size image': 'Full size image',
- 'Side image': 'Side image',
- 'Left aligned image': 'Left aligned image',
- 'Centered image': 'Centered image',
- 'Right aligned image': 'Right aligned image'
- } );
- } );
- } );
- } );
- describe( 'defaultStyles', () => {
- it( 'should be defined', () => {
- expect( ImageStyleEngine.defaultStyles ).to.deep.equal( {
- imageStyleFull: {
- name: 'imageStyleFull',
- title: 'Full size image',
- icon: fullWidthIcon,
- isDefault: true
- },
- imageStyleSide: {
- name: 'imageStyleSide',
- title: 'Side image',
- icon: rightIcon,
- className: 'image-style-side'
- },
- imageStyleAlignLeft: {
- name: 'imageStyleAlignLeft',
- title: 'Left aligned image',
- icon: leftIcon,
- className: 'image-style-align-left'
- },
- imageStyleAlignCenter: {
- name: 'imageStyleAlignCenter',
- title: 'Centered image',
- icon: centerIcon,
- className: 'image-style-align-center'
- },
- imageStyleAlignRight: {
- name: 'imageStyleAlignRight',
- title: 'Right aligned image',
- icon: rightIcon,
- className: 'image-style-align-right'
- }
- } );
- } );
- } );
- describe( 'defaultIcons', () => {
- it( 'should be defined', () => {
- expect( ImageStyleEngine.defaultIcons ).to.deep.equal( {
- full: fullWidthIcon,
- left: leftIcon,
- right: rightIcon,
- center: centerIcon,
- } );
- } );
- } );
- } );
|