From 4e4a8400de724482dff26a09f1a492d5be9c4bb4 Mon Sep 17 00:00:00 2001 From: Christian Kotzbauer Date: Wed, 21 Nov 2018 08:17:58 +0100 Subject: [PATCH 1/2] fix: handle null file types in image check --- src/imageupload/utils.js | 4 ++++ tests/imageupload/utils.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/imageupload/utils.js b/src/imageupload/utils.js index 89943aa4..3a40216a 100644 --- a/src/imageupload/utils.js +++ b/src/imageupload/utils.js @@ -14,6 +14,10 @@ * @returns {Boolean} */ export function isImageType( file ) { + if ( !file || !file.type ) { + return false; + } + const types = /^image\/(jpeg|png|gif|bmp)$/; return types.test( file.type ); diff --git a/tests/imageupload/utils.js b/tests/imageupload/utils.js index 1af3d8a1..a890fcbc 100644 --- a/tests/imageupload/utils.js +++ b/tests/imageupload/utils.js @@ -27,5 +27,13 @@ describe( 'upload utils', () => { expect( isImageType( { type: 'audio/mp3' } ) ).to.be.false; expect( isImageType( { type: 'video/mpeg' } ) ).to.be.false; } ); + + it( 'should return false for null type', () => { + expect( isImageType( { type: null } ) ).to.be.false; + } ); + + it( 'should return false for null file', () => { + expect( isImageType( null ) ).to.be.false; + } ); } ); } ); From 18b39cfcc61ff98cfea0cd83822c402df54ca287 Mon Sep 17 00:00:00 2001 From: Christian Kotzbauer Date: Thu, 22 Nov 2018 10:45:32 +0100 Subject: [PATCH 2/2] fix: move null-check to dnd callback --- src/imageupload/imageuploadediting.js | 8 +++++++- src/imageupload/utils.js | 4 ---- tests/imageupload/imageuploadediting.js | 14 ++++++++++++++ tests/imageupload/utils.js | 8 -------- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/imageupload/imageuploadediting.js b/src/imageupload/imageuploadediting.js index f09c488d..6ed0dd8d 100644 --- a/src/imageupload/imageuploadediting.js +++ b/src/imageupload/imageuploadediting.js @@ -55,7 +55,13 @@ export default class ImageUploadEditing extends Plugin { return; } - const images = Array.from( data.dataTransfer.files ).filter( isImageType ); + const images = Array.from( data.dataTransfer.files ).filter( file => { + if ( !file ) { + return false; + } + + return isImageType( file ); + } ); const ranges = data.targetRanges.map( viewRange => editor.editing.mapper.toModelRange( viewRange ) ); diff --git a/src/imageupload/utils.js b/src/imageupload/utils.js index 3a40216a..89943aa4 100644 --- a/src/imageupload/utils.js +++ b/src/imageupload/utils.js @@ -14,10 +14,6 @@ * @returns {Boolean} */ export function isImageType( file ) { - if ( !file || !file.type ) { - return false; - } - const types = /^image\/(jpeg|png|gif|bmp)$/; return types.test( file.type ); diff --git a/tests/imageupload/imageuploadediting.js b/tests/imageupload/imageuploadediting.js index afe81bd2..6d647dc2 100644 --- a/tests/imageupload/imageuploadediting.js +++ b/tests/imageupload/imageuploadediting.js @@ -197,6 +197,20 @@ describe( 'ImageUploadEditing', () => { expect( getModelData( model ) ).to.equal( 'foo[]' ); } ); + it( 'should not insert image when file is null', () => { + const viewDocument = editor.editing.view.document; + const dataTransfer = new DataTransfer( { files: [ null ], types: [ 'Files' ] } ); + + setModelData( model, 'foo[]' ); + + const targetRange = doc.selection.getFirstRange(); + const targetViewRange = editor.editing.mapper.toViewRange( targetRange ); + + viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } ); + + expect( getModelData( model ) ).to.equal( 'foo[]' ); + } ); + it( 'should not insert image when there is non-empty HTML content pasted', () => { const fileMock = createNativeFileMock(); const dataTransfer = new DataTransfer( { diff --git a/tests/imageupload/utils.js b/tests/imageupload/utils.js index a890fcbc..1af3d8a1 100644 --- a/tests/imageupload/utils.js +++ b/tests/imageupload/utils.js @@ -27,13 +27,5 @@ describe( 'upload utils', () => { expect( isImageType( { type: 'audio/mp3' } ) ).to.be.false; expect( isImageType( { type: 'video/mpeg' } ) ).to.be.false; } ); - - it( 'should return false for null type', () => { - expect( isImageType( { type: null } ) ).to.be.false; - } ); - - it( 'should return false for null file', () => { - expect( isImageType( null ) ).to.be.false; - } ); } ); } );