imageuploadengine.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, setTimeout */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  8. import ImageEngine from '@ckeditor/ckeditor5-image/src/image/imageengine';
  9. import ImageUploadEngine from '../src/imageuploadengine';
  10. import ImageUploadCommand from '../src/imageuploadcommand';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
  13. import DataTransfer from '@ckeditor/ckeditor5-clipboard/src/datatransfer';
  14. import FileRepository from '../src/filerepository';
  15. import { AdapterMock, createNativeFileMock, NativeFileReaderMock } from './_utils/mocks';
  16. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  18. import { eventNameToConsumableType } from '@ckeditor/ckeditor5-engine/src/conversion/model-to-view-converters';
  19. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  20. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  22. describe( 'ImageUploadEngine', () => {
  23. // eslint-disable-next-line max-len
  24. const base64Sample = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';
  25. let editor, doc, fileRepository, viewDocument, nativeReaderMock, loader, adapterMock;
  26. testUtils.createSinonSandbox();
  27. class UploadAdapterPluginMock extends Plugin {
  28. init() {
  29. fileRepository = this.editor.plugins.get( FileRepository );
  30. fileRepository.createAdapter = newLoader => {
  31. loader = newLoader;
  32. adapterMock = new AdapterMock( loader );
  33. return adapterMock;
  34. };
  35. }
  36. }
  37. beforeEach( () => {
  38. testUtils.sinon.stub( window, 'FileReader' ).callsFake( () => {
  39. nativeReaderMock = new NativeFileReaderMock();
  40. return nativeReaderMock;
  41. } );
  42. return ClassicTestEditor
  43. .create( {
  44. plugins: [ ImageEngine, ImageUploadEngine, Paragraph, UndoEngine, UploadAdapterPluginMock ]
  45. } )
  46. .then( newEditor => {
  47. editor = newEditor;
  48. doc = editor.document;
  49. viewDocument = editor.editing.view;
  50. } );
  51. } );
  52. it( 'should register proper schema rules', () => {
  53. expect( doc.schema.check( { name: 'image', attributes: [ 'uploadId' ], inside: '$root' } ) ).to.be.true;
  54. } );
  55. it( 'should register imageUpload command', () => {
  56. expect( editor.commands.get( 'imageUpload' ) ).to.be.instanceOf( ImageUploadCommand );
  57. } );
  58. it( 'should execute imageUpload command when image is pasted', () => {
  59. const spy = sinon.spy( editor, 'execute' );
  60. const fileMock = createNativeFileMock();
  61. const dataTransfer = new DataTransfer( { files: [ fileMock ], types: [ 'Files' ] } );
  62. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  63. const targetRange = Range.createFromParentsAndOffsets( doc.getRoot(), 1, doc.getRoot(), 1 );
  64. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  65. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  66. sinon.assert.calledOnce( spy );
  67. sinon.assert.calledWith( spy, 'imageUpload' );
  68. const id = fileRepository.getLoader( fileMock ).id;
  69. expect( getModelData( doc ) ).to.equal(
  70. `<paragraph>foo</paragraph>[<image uploadId="${ id }" uploadStatus="reading"></image>]`
  71. );
  72. } );
  73. it( 'should execute imageUpload command with an optimized position when image is pasted', () => {
  74. const spy = sinon.spy( editor, 'execute' );
  75. const fileMock = createNativeFileMock();
  76. const dataTransfer = new DataTransfer( { files: [ fileMock ], types: [ 'Files' ] } );
  77. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  78. const paragraph = doc.getRoot().getChild( 0 );
  79. const targetRange = Range.createFromParentsAndOffsets( paragraph, 1, paragraph, 1 ); // f[]oo
  80. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  81. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  82. sinon.assert.calledOnce( spy );
  83. sinon.assert.calledWith( spy, 'imageUpload' );
  84. const id = fileRepository.getLoader( fileMock ).id;
  85. expect( getModelData( doc ) ).to.equal(
  86. `[<image uploadId="${ id }" uploadStatus="reading"></image>]<paragraph>foo</paragraph>`
  87. );
  88. } );
  89. it( 'should execute imageUpload command when multiple files image are pasted', () => {
  90. const spy = sinon.spy( editor, 'execute' );
  91. const files = [ createNativeFileMock(), createNativeFileMock() ];
  92. const dataTransfer = new DataTransfer( { files, types: [ 'Files' ] } );
  93. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  94. const targetRange = Range.createFromParentsAndOffsets( doc.getRoot(), 1, doc.getRoot(), 1 );
  95. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  96. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  97. sinon.assert.calledTwice( spy );
  98. sinon.assert.calledWith( spy, 'imageUpload' );
  99. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  100. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  101. expect( getModelData( doc ) ).to.equal(
  102. '<paragraph>foo</paragraph>' +
  103. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  104. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]`
  105. );
  106. } );
  107. it( 'should not execute imageUpload command when file is not an image', () => {
  108. const spy = sinon.spy( editor, 'execute' );
  109. const viewDocument = editor.editing.view;
  110. const fileMock = {
  111. type: 'media/mp3',
  112. size: 1024
  113. };
  114. const dataTransfer = new DataTransfer( { files: [ fileMock ], types: [ 'Files' ] } );
  115. setModelData( doc, '<paragraph>foo[]</paragraph>' );
  116. const targetRange = Range.createFromParentsAndOffsets( doc.getRoot(), 1, doc.getRoot(), 1 );
  117. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  118. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  119. sinon.assert.notCalled( spy );
  120. } );
  121. it( 'should not execute imageUpload command when there is non-empty HTML content pasted', () => {
  122. const spy = sinon.spy( editor, 'execute' );
  123. const fileMock = createNativeFileMock();
  124. const dataTransfer = new DataTransfer( {
  125. files: [ fileMock ],
  126. types: [ 'Files', 'text/html' ],
  127. getData: type => type === 'text/html' ? '<p>SomeData</p>' : ''
  128. } );
  129. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  130. const targetRange = Range.createFromParentsAndOffsets( doc.getRoot(), 1, doc.getRoot(), 1 );
  131. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  132. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  133. sinon.assert.notCalled( spy );
  134. } );
  135. // https://github.com/ckeditor/ckeditor5-upload/issues/70
  136. it( 'should not crash on browsers which do not implement DOMStringList as a child class of an Array', () => {
  137. const typesDomStringListMock = {
  138. length: 2,
  139. '0': 'text/html',
  140. '1': 'text/plain'
  141. };
  142. const dataTransfer = new DataTransfer( {
  143. types: typesDomStringListMock,
  144. getData: type => type === 'text/html' ? '<p>SomeData</p>' : 'SomeData'
  145. } );
  146. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  147. const targetRange = doc.selection.getFirstRange();
  148. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  149. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  150. // Well, there's no clipboard plugin, so nothing happens.
  151. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  152. } );
  153. it( 'should not convert image\'s uploadId attribute if is consumed already', () => {
  154. editor.editing.modelToView.on( 'addAttribute:uploadId:image', ( evt, data, consumable ) => {
  155. consumable.consume( data.item, eventNameToConsumableType( evt.name ) );
  156. }, { priority: 'high' } );
  157. setModelData( doc, '<image uploadId="1234"></image>' );
  158. expect( getViewData( viewDocument ) ).to.equal(
  159. '[<figure class="ck-widget image" contenteditable="false">' +
  160. '<img></img>' +
  161. '</figure>]' );
  162. } );
  163. it( 'should use read data once it is present', done => {
  164. const file = createNativeFileMock();
  165. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  166. editor.execute( 'imageUpload', { file } );
  167. doc.once( 'changesDone', () => {
  168. expect( getViewData( viewDocument ) ).to.equal(
  169. '[<figure class="ck-widget image" contenteditable="false">' +
  170. `<img src="${ base64Sample }"></img>` +
  171. '</figure>]' +
  172. '<p>foo bar</p>' );
  173. expect( loader.status ).to.equal( 'uploading' );
  174. done();
  175. } );
  176. expect( loader.status ).to.equal( 'reading' );
  177. nativeReaderMock.mockSuccess( base64Sample );
  178. } );
  179. it( 'should replace read data with server response once it is present', done => {
  180. const file = createNativeFileMock();
  181. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  182. editor.execute( 'imageUpload', { file } );
  183. doc.once( 'changesDone', () => {
  184. doc.once( 'changesDone', () => {
  185. expect( getViewData( viewDocument ) ).to.equal(
  186. '[<figure class="ck-widget image" contenteditable="false"><img src="image.png"></img></figure>]<p>foo bar</p>'
  187. );
  188. expect( loader.status ).to.equal( 'idle' );
  189. done();
  190. } );
  191. adapterMock.mockSuccess( { default: 'image.png' } );
  192. } );
  193. nativeReaderMock.mockSuccess( base64Sample );
  194. } );
  195. it( 'should fire notification event in case of error', done => {
  196. const notification = editor.plugins.get( Notification );
  197. const file = createNativeFileMock();
  198. notification.on( 'show:warning', ( evt, data ) => {
  199. expect( data.message ).to.equal( 'Reading error.' );
  200. expect( data.title ).to.equal( 'Upload failed' );
  201. evt.stop();
  202. done();
  203. }, { priority: 'high' } );
  204. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  205. editor.execute( 'imageUpload', { file } );
  206. nativeReaderMock.mockError( 'Reading error.' );
  207. } );
  208. it( 'should not fire notification on abort', done => {
  209. const notification = editor.plugins.get( Notification );
  210. const file = createNativeFileMock();
  211. const spy = testUtils.sinon.spy();
  212. notification.on( 'show:warning', evt => {
  213. spy();
  214. evt.stop();
  215. }, { priority: 'high' } );
  216. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  217. editor.execute( 'imageUpload', { file } );
  218. nativeReaderMock.abort();
  219. setTimeout( () => {
  220. sinon.assert.notCalled( spy );
  221. done();
  222. }, 0 );
  223. } );
  224. it( 'should do nothing if image does not have uploadId', () => {
  225. setModelData( doc, '<image src="image.png"></image>' );
  226. expect( getViewData( viewDocument ) ).to.equal(
  227. '[<figure class="ck-widget image" contenteditable="false"><img src="image.png"></img></figure>]'
  228. );
  229. } );
  230. it( 'should remove image in case of upload error', done => {
  231. const file = createNativeFileMock();
  232. const spy = testUtils.sinon.spy();
  233. const notification = editor.plugins.get( Notification );
  234. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  235. notification.on( 'show:warning', evt => {
  236. spy();
  237. evt.stop();
  238. }, { priority: 'high' } );
  239. editor.execute( 'imageUpload', { file } );
  240. doc.once( 'changesDone', () => {
  241. doc.once( 'changesDone', () => {
  242. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  243. sinon.assert.calledOnce( spy );
  244. done();
  245. } );
  246. } );
  247. nativeReaderMock.mockError( 'Upload error.' );
  248. } );
  249. it( 'should abort upload if image is removed', () => {
  250. const file = createNativeFileMock();
  251. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  252. editor.execute( 'imageUpload', { file } );
  253. const abortSpy = testUtils.sinon.spy( loader, 'abort' );
  254. expect( loader.status ).to.equal( 'reading' );
  255. nativeReaderMock.mockSuccess( base64Sample );
  256. const image = doc.getRoot().getChild( 0 );
  257. doc.enqueueChanges( () => {
  258. const batch = doc.batch();
  259. batch.remove( image );
  260. } );
  261. expect( loader.status ).to.equal( 'aborted' );
  262. sinon.assert.calledOnce( abortSpy );
  263. } );
  264. it( 'image should be permanently removed if it is removed by user during upload', done => {
  265. const file = createNativeFileMock();
  266. const notification = editor.plugins.get( Notification );
  267. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  268. // Prevent popping up alert window.
  269. notification.on( 'show:warning', evt => {
  270. evt.stop();
  271. }, { priority: 'high' } );
  272. editor.execute( 'imageUpload', { file } );
  273. doc.once( 'changesDone', () => {
  274. // This is called after "manual" remove.
  275. doc.once( 'changesDone', () => {
  276. // This is called after attributes are removed.
  277. let undone = false;
  278. doc.once( 'changesDone', () => {
  279. if ( !undone ) {
  280. undone = true;
  281. // This is called after abort remove.
  282. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  283. editor.execute( 'undo' );
  284. // Expect that the image has not been brought back.
  285. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  286. done();
  287. }
  288. } );
  289. } );
  290. } );
  291. const image = doc.getRoot().getChild( 0 );
  292. doc.enqueueChanges( () => {
  293. const batch = doc.batch();
  294. batch.remove( image );
  295. } );
  296. } );
  297. it( 'should create responsive image if server return multiple images', done => {
  298. const file = createNativeFileMock();
  299. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  300. editor.execute( 'imageUpload', { file } );
  301. doc.once( 'changesDone', () => {
  302. doc.once( 'changesDone', () => {
  303. expect( getViewData( viewDocument ) ).to.equal(
  304. '[<figure class="ck-widget image" contenteditable="false">' +
  305. '<img sizes="100vw" src="image.png" srcset="image-500.png 500w, image-800.png 800w" width="800"></img>' +
  306. '</figure>]<p>foo bar</p>'
  307. );
  308. expect( loader.status ).to.equal( 'idle' );
  309. done();
  310. } );
  311. adapterMock.mockSuccess( { default: 'image.png', 500: 'image-500.png', 800: 'image-800.png' } );
  312. } );
  313. nativeReaderMock.mockSuccess( base64Sample );
  314. } );
  315. it( 'should prevent from browser redirecting when an image is dropped on another image', () => {
  316. const spy = testUtils.sinon.spy();
  317. editor.editing.view.fire( 'dragover', {
  318. preventDefault: spy
  319. } );
  320. expect( spy.calledOnce ).to.equal( true );
  321. } );
  322. } );