| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* globals console */
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- import ImageUploadCommand from '../../src/imageupload/imageuploadcommand';
- import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
- import { createNativeFileMock, UploadAdapterMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
- import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import Image from '../../src/image/imageediting';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- describe( 'ImageUploadCommand', () => {
- let editor, command, model, fileRepository;
- class UploadAdapterPluginMock extends Plugin {
- init() {
- fileRepository = this.editor.plugins.get( FileRepository );
- fileRepository.createUploadAdapter = loader => {
- return new UploadAdapterMock( loader );
- };
- }
- }
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ FileRepository, Image, Paragraph, UploadAdapterPluginMock ]
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- command = new ImageUploadCommand( editor );
- const schema = model.schema;
- schema.extend( 'image', { allowAttributes: 'uploadId' } );
- } );
- } );
- afterEach( () => {
- sinon.restore();
- return editor.destroy();
- } );
- describe( 'isEnabled', () => {
- it( 'should be true when the selection directly in the root', () => {
- model.enqueueChange( 'transparent', () => {
- setModelData( model, '[]' );
- command.refresh();
- expect( command.isEnabled ).to.be.true;
- } );
- } );
- it( 'should be true when the selection is in empty block', () => {
- setModelData( model, '<paragraph>[]</paragraph>' );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be true when the selection directly in a paragraph', () => {
- setModelData( model, '<paragraph>foo[]</paragraph>' );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be true when the selection directly in a block', () => {
- model.schema.register( 'block', { inheritAllFrom: '$block' } );
- model.schema.extend( '$text', { allowIn: 'block' } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
- setModelData( model, '<block>foo[]</block>' );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be true when the selection is on other image', () => {
- setModelData( model, '[<image></image>]' );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be false when the selection is inside other image', () => {
- model.schema.register( 'caption', {
- allowIn: 'image',
- allowContentOf: '$block',
- isLimit: true
- } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'caption', view: 'figcaption' } );
- setModelData( model, '<image><caption>[]</caption></image>' );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be false when the selection is on other object', () => {
- model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'object', view: 'object' } );
- setModelData( model, '[<object></object>]' );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be true when the selection is inside block element inside isLimit element which allows image', () => {
- model.schema.register( 'table', { allowWhere: '$block', isLimit: true, isObject: true, isBlock: true } );
- model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
- model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true, isSelectable: true } );
- model.schema.extend( '$block', { allowIn: 'tableCell' } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'table', view: 'table' } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableRow', view: 'tableRow' } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableCell', view: 'tableCell' } );
- setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
- } );
- it( 'should be false when schema disallows image', () => {
- model.schema.register( 'block', { inheritAllFrom: '$block' } );
- model.schema.extend( 'paragraph', { allowIn: 'block' } );
- // Block image in block.
- model.schema.addChildCheck( ( context, childDefinition ) => {
- if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
- return false;
- }
- } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
- setModelData( model, '<block><paragraph>[]</paragraph></block>' );
- expect( command.isEnabled ).to.be.false;
- } );
- } );
- describe( 'execute()', () => {
- it( 'should insert image at selection position as other widgets', () => {
- const file = createNativeFileMock();
- setModelData( model, '<paragraph>f[o]o</paragraph>' );
- command.execute( { file } );
- const id = fileRepository.getLoader( file ).id;
- expect( getModelData( model ) )
- .to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
- } );
- it( 'should use parent batch', () => {
- const file = createNativeFileMock();
- setModelData( model, '<paragraph>[]foo</paragraph>' );
- model.change( writer => {
- expect( writer.batch.operations ).to.length( 0 );
- command.execute( { file } );
- expect( writer.batch.operations ).to.length.above( 0 );
- } );
- } );
- it( 'should not insert image nor crash when image could not be inserted', () => {
- const file = createNativeFileMock();
- model.schema.register( 'other', {
- allowIn: '$root',
- isLimit: true
- } );
- model.schema.extend( '$text', { allowIn: 'other' } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'other', view: 'p' } );
- setModelData( model, '<other>[]</other>' );
- command.execute( { file } );
- expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
- } );
- it( 'should not throw when upload adapter is not set (FileRepository will log an warn anyway)', () => {
- const file = createNativeFileMock();
- fileRepository.createUploadAdapter = undefined;
- const consoleWarnStub = sinon.stub( console, 'warn' );
- setModelData( model, '<paragraph>fo[]o</paragraph>' );
- expect( () => {
- command.execute( { file } );
- } ).to.not.throw();
- expect( getModelData( model ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
- sinon.assert.calledOnce( consoleWarnStub );
- } );
- } );
- } );
|