Explorar o código

Added proper file type filtering and missing test.

Piotrek Koszuliński %!s(int64=8) %!d(string=hai) anos
pai
achega
a892697053

+ 4 - 2
packages/ckeditor5-upload/src/imageuploadbutton.js

@@ -11,7 +11,7 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import ImageUploadEngine from './imageuploadengine';
 import FileDialogButtonView from './ui/filedialogbuttonview';
 import imageIcon from '@ckeditor/ckeditor5-core/theme/icons/image.svg';
-import { findOptimalInsertionPosition } from './utils';
+import { isImageType, findOptimalInsertionPosition } from './utils';
 
 /**
  * Image upload button plugin.
@@ -53,7 +53,9 @@ export default class ImageUploadButton extends Plugin {
 				for ( const file of files ) {
 					const insertAt = findOptimalInsertionPosition( editor.document.selection );
 
-					editor.execute( 'imageUpload', { file, insertAt } );
+					if ( isImageType( file ) ) {
+						editor.execute( 'imageUpload', { file, insertAt } );
+					}
 				}
 			} );
 

+ 28 - 0
packages/ckeditor5-upload/tests/imageuploadbutton.js

@@ -84,6 +84,22 @@ describe( 'ImageUploadButton', () => {
 		expect( executeStub.firstCall.args[ 1 ].file ).to.equal( files[ 0 ] );
 	} );
 
+	it( 'should optimize the insertion position', () => {
+		const button = editor.ui.componentFactory.create( 'insertImage' );
+		const files = [ createNativeFileMock() ];
+
+		setModelData( doc, '<paragraph>f[]oo</paragraph>' );
+
+		button.fire( 'done', files );
+
+		const id = fileRepository.getLoader( files[ 0 ] ).id;
+
+		expect( getModelData( doc ) ).to.equal(
+			`[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
+			'<paragraph>foo</paragraph>'
+		);
+	} );
+
 	it( 'should correctly insert multiple files', () => {
 		const button = editor.ui.componentFactory.create( 'insertImage' );
 		const files = [ createNativeFileMock(), createNativeFileMock() ];
@@ -102,5 +118,17 @@ describe( 'ImageUploadButton', () => {
 			'<paragraph>bar</paragraph>'
 		);
 	} );
+
+	it( 'should not execute imageUpload if the file is not an image', () => {
+		const executeStub = sinon.stub( editor, 'execute' );
+		const button = editor.ui.componentFactory.create( 'insertImage' );
+		const file = {
+			type: 'media/mp3',
+			size: 1024
+		};
+
+		button.fire( 'done', [ file ] );
+		sinon.assert.notCalled( executeStub );
+	} );
 } );