Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Fix: Upload command should not crash when upload adapter is not speci…
Browse files Browse the repository at this point in the history
…fied (instead, FileRepository logs an error). Closes #59.
  • Loading branch information
Reinmar committed Sep 29, 2017
1 parent 17f1574 commit 14b738b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/imageuploadcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ export default class ImageUploadCommand extends Command {
const fileRepository = editor.plugins.get( FileRepository );

doc.enqueueChanges( () => {
const loader = fileRepository.createLoader( file );

// Do not throw when upload adapter is not set. FileRepository will log an error anyway.
if ( !loader ) {
return;
}

const imageElement = new ModelElement( 'image', {
uploadId: fileRepository.createLoader( file ).id
uploadId: loader.id
} );

let insertAtSelection;
Expand Down
2 changes: 1 addition & 1 deletion src/imageuploadengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class ImageUploadEngine extends Plugin {
}
} );

// Prevents from browser redirecting to drag-end-dropped image.
// Prevents from browser redirecting to the dropped image.
editor.editing.view.on( 'dragover', ( evt, data ) => {
data.preventDefault();
} );
Expand Down
23 changes: 23 additions & 0 deletions tests/imageuploadcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';

import log from '@ckeditor/ckeditor5-utils/src/log';

import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

describe( 'ImageUploadCommand', () => {
let editor, command, doc, fileRepository;

testUtils.createSinonSandbox();

class UploadAdapterPluginMock extends Plugin {
init() {
fileRepository = this.editor.plugins.get( FileRepository );
Expand Down Expand Up @@ -105,5 +111,22 @@ describe( 'ImageUploadCommand', () => {

expect( getModelData( doc ) ).to.equal( '<other>[]</other>' );
} );

it( 'should not throw when upload adapter is not set (FileRepository will log an error anyway)', () => {
const file = createNativeFileMock();

fileRepository.createAdapter = undefined;

const logStub = testUtils.sinon.stub( log, 'error' );

setModelData( doc, '<paragraph>fo[]o</paragraph>' );

expect( () => {
command.execute( { file } );
} ).to.not.throw();

expect( getModelData( doc ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
expect( logStub.calledOnce ).to.be.true;
} );
} );
} );

0 comments on commit 14b738b

Please sign in to comment.