imageuploadediting.js 15 KB

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