autoimage.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global setTimeout */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  9. import Link from '@ckeditor/ckeditor5-link/src/link';
  10. import Table from '@ckeditor/ckeditor5-table/src/table';
  11. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  12. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  13. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  14. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import Image from '../src/image';
  16. import ImageCaption from '../src/imagecaption';
  17. import AutoImage from '../src/autoimage';
  18. describe( 'AutoImage - integration', () => {
  19. let editorElement, editor;
  20. beforeEach( () => {
  21. editorElement = global.document.createElement( 'div' );
  22. global.document.body.appendChild( editorElement );
  23. return ClassicTestEditor
  24. .create( editorElement, {
  25. plugins: [ Typing, Paragraph, Link, Image, ImageCaption, AutoImage ]
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. } );
  30. } );
  31. afterEach( () => {
  32. editorElement.remove();
  33. return editor.destroy();
  34. } );
  35. it( 'should load Clipboard plugin', () => {
  36. expect( editor.plugins.get( Clipboard ) ).to.instanceOf( Clipboard );
  37. } );
  38. it( 'should load Undo plugin', () => {
  39. expect( editor.plugins.get( Undo ) ).to.instanceOf( Undo );
  40. } );
  41. it( 'has proper name', () => {
  42. expect( AutoImage.pluginName ).to.equal( 'AutoImage' );
  43. } );
  44. describe( 'use fake timers', () => {
  45. let clock;
  46. beforeEach( () => {
  47. clock = sinon.useFakeTimers();
  48. } );
  49. afterEach( () => {
  50. clock.restore();
  51. } );
  52. it( 'replaces pasted text with image element after 100ms', () => {
  53. setData( editor.model, '<paragraph>[]</paragraph>' );
  54. pasteHtml( editor, 'http://example.com/image.png' );
  55. expect( getData( editor.model ) ).to.equal(
  56. '<paragraph>http://example.com/image.png[]</paragraph>'
  57. );
  58. clock.tick( 100 );
  59. expect( getData( editor.model ) ).to.equal(
  60. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  61. );
  62. } );
  63. it( 'can undo auto-embeding', () => {
  64. setData( editor.model, '<paragraph>[]</paragraph>' );
  65. pasteHtml( editor, 'http://example.com/image.png' );
  66. expect( getData( editor.model ) ).to.equal(
  67. '<paragraph>http://example.com/image.png[]</paragraph>'
  68. );
  69. clock.tick( 100 );
  70. editor.commands.execute( 'undo' );
  71. expect( getData( editor.model ) ).to.equal(
  72. '<paragraph>http://example.com/image.png[]</paragraph>'
  73. );
  74. } );
  75. describe( 'supported URL', () => {
  76. const supportedURLs = [
  77. 'example.com/image.png',
  78. 'www.example.com/image.png',
  79. 'https://www.example.com/image.png',
  80. 'https://example.com/image.png',
  81. 'http://www.example.com/image.png',
  82. 'http://example.com/image.png',
  83. 'http://example.com/image.jpg',
  84. 'http://example.com/image.jpeg',
  85. 'http://example.com/image.gif',
  86. 'http://example.com/image.ico',
  87. 'http://example.com/image.JPG',
  88. 'http://example.com%20Fimage.png',
  89. 'http://example.com/image.png?foo=bar',
  90. 'http://example.com/image.png#foo'
  91. ];
  92. for ( const supportedURL of supportedURLs ) {
  93. it( `should detect "${ supportedURL }" as a valid URL`, () => {
  94. setData( editor.model, '<paragraph>[]</paragraph>' );
  95. pasteHtml( editor, supportedURL );
  96. clock.tick( 100 );
  97. expect( getData( editor.model ) ).to.equal(
  98. `[<image src="${ supportedURL }"><caption></caption></image>]`
  99. );
  100. } );
  101. }
  102. } );
  103. describe( 'unsupported URL', () => {
  104. const unsupportedOrInvalid = [
  105. 'http://www.example.com',
  106. 'https://example.com',
  107. 'http://www.example.com/image.svg',
  108. 'http://www.example.com/image.webp',
  109. 'http://www.example.com/image.mp3',
  110. 'http://www.example.com/image.exe',
  111. 'http://www.example.com/image.txt',
  112. 'http://example.com/image.png http://example.com/image.png',
  113. 'Foo bar http://example.com/image.png bar foo.',
  114. 'https://example.com/im age.png'
  115. ];
  116. for ( const unsupportedURL of unsupportedOrInvalid ) {
  117. it( `should not detect "${ unsupportedURL }" as a valid URL`, () => {
  118. setData( editor.model, '<paragraph>[]</paragraph>' );
  119. pasteHtml( editor, unsupportedURL );
  120. clock.tick( 100 );
  121. expect( getData( editor.model ) ).to.equal(
  122. `<paragraph>${ unsupportedURL }[]</paragraph>`
  123. );
  124. } );
  125. }
  126. } );
  127. it( 'works for URL that was pasted as a link', () => {
  128. setData( editor.model, '<paragraph>[]</paragraph>' );
  129. pasteHtml( editor, '<a href="http://example.com/image.png">' +
  130. 'http://example.com/image.png</a>' );
  131. clock.tick( 100 );
  132. expect( getData( editor.model ) ).to.equal(
  133. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  134. );
  135. } );
  136. it( 'works for URL that contains some inline styles', () => {
  137. setData( editor.model, '<paragraph>[]</paragraph>' );
  138. pasteHtml( editor, '<b>http://example.com/image.png</b>' );
  139. clock.tick( 100 );
  140. expect( getData( editor.model ) ).to.equal(
  141. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  142. );
  143. } );
  144. it( 'works for not collapsed selection inside single element', () => {
  145. setData( editor.model, '<paragraph>[Foo]</paragraph>' );
  146. pasteHtml( editor, 'http://example.com/image.png' );
  147. clock.tick( 100 );
  148. expect( getData( editor.model ) ).to.equal(
  149. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  150. );
  151. } );
  152. it( 'works for not collapsed selection over a few elements', () => {
  153. setData( editor.model, '<paragraph>Fo[o</paragraph><paragraph>Ba]r</paragraph>' );
  154. pasteHtml( editor, 'http://example.com/image.png' );
  155. clock.tick( 100 );
  156. expect( getData( editor.model ) ).to.equal(
  157. '<paragraph>Fo</paragraph>' +
  158. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  159. '<paragraph>r</paragraph>'
  160. );
  161. } );
  162. it( 'inserts image in-place (collapsed selection)', () => {
  163. setData( editor.model, '<paragraph>Foo []Bar</paragraph>' );
  164. pasteHtml( editor, 'http://example.com/image.png' );
  165. clock.tick( 100 );
  166. expect( getData( editor.model ) ).to.equal(
  167. '<paragraph>Foo </paragraph>' +
  168. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  169. '<paragraph>Bar</paragraph>'
  170. );
  171. } );
  172. it( 'inserts image in-place (non-collapsed selection)', () => {
  173. setData( editor.model, '<paragraph>Foo [Bar] Baz</paragraph>' );
  174. pasteHtml( editor, 'http://example.com/image.png' );
  175. clock.tick( 100 );
  176. expect( getData( editor.model ) ).to.equal(
  177. '<paragraph>Foo </paragraph>' +
  178. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  179. '<paragraph> Baz</paragraph>'
  180. );
  181. } );
  182. it( 'does nothing if pasted more than single node', () => {
  183. setData( editor.model, '<paragraph>[]</paragraph>' );
  184. pasteHtml( editor,
  185. 'http://example.com/image.png ' +
  186. '<a href="http://example.com/image.png">' +
  187. 'http://example.com/image.png</a>'
  188. );
  189. clock.tick( 100 );
  190. expect( getData( editor.model, { withoutSelection: true } ) ).to.equal(
  191. '<paragraph>http://example.com/image.png ' +
  192. '<$text linkHref="http://example.com/image.png">' +
  193. 'http://example.com/image.png</$text>' +
  194. '</paragraph>'
  195. );
  196. } );
  197. it( 'does nothing if pasted a paragraph with the url', () => {
  198. setData( editor.model, '<paragraph>[]</paragraph>' );
  199. pasteHtml( editor, '<p>http://example.com/image.png</p>' );
  200. clock.tick( 100 );
  201. expect( getData( editor.model ) ).to.equal(
  202. '<paragraph>http://example.com/image.png[]</paragraph>'
  203. );
  204. } );
  205. it( 'does nothing if pasted a block of content that looks like a URL', () => {
  206. setData( editor.model, '<paragraph>[]</paragraph>' );
  207. pasteHtml( editor, '<p>https://</p><p>example.com/image</p><p>.png</p>' );
  208. clock.tick( 100 );
  209. expect( getData( editor.model ) ).to.equal(
  210. '<paragraph>https://</paragraph><paragraph>example.com/image</paragraph><paragraph>.png[]</paragraph>'
  211. );
  212. } );
  213. // #47
  214. it( 'does not transform a valid URL into a image if the element cannot be placed in the current position', () => {
  215. setData( editor.model, '<image src="/assets/sample.png"><caption>Foo.[]</caption></image>' );
  216. pasteHtml( editor, 'http://example.com/image.png' );
  217. clock.tick( 100 );
  218. expect( getData( editor.model ) ).to.equal(
  219. '<image src="/assets/sample.png"><caption>' +
  220. 'Foo.http://example.com/image.png[]' +
  221. '</caption></image>'
  222. );
  223. } );
  224. it( 'replaces a URL in image if pasted a link when other image element was selected', () => {
  225. setData(
  226. editor.model,
  227. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  228. );
  229. pasteHtml( editor, 'http://example.com/image2.png' );
  230. clock.tick( 100 );
  231. expect( getData( editor.model ) ).to.equal(
  232. '[<image src="http://example.com/image2.png"><caption></caption></image>]'
  233. );
  234. } );
  235. it( 'inserts a new image element if pasted a link when other image element was selected in correct place', () => {
  236. setData(
  237. editor.model,
  238. '<paragraph>Foo. <$text linkHref="https://cksource.com">Bar</$text></paragraph>' +
  239. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  240. '<paragraph><$text>Bar</$text>.</paragraph>'
  241. );
  242. pasteHtml( editor, 'http://example.com/image2.png' );
  243. clock.tick( 100 );
  244. expect( getData( editor.model ) ).to.equal(
  245. '<paragraph>Foo. <$text linkHref="https://cksource.com">Bar</$text></paragraph>' +
  246. '[<image src="http://example.com/image2.png"><caption></caption></image>]' +
  247. '<paragraph>Bar.</paragraph>'
  248. );
  249. } );
  250. } );
  251. describe( 'use real timers', () => {
  252. const characters = Array( 10 ).fill( 1 ).map( ( x, i ) => String.fromCharCode( 65 + i ) );
  253. it( 'undo breaks the auto-image feature (undo was done before auto-image)', done => {
  254. setData( editor.model, '<paragraph>[]</paragraph>' );
  255. pasteHtml( editor, 'http://example.com/image.png' );
  256. expect( getData( editor.model ) ).to.equal(
  257. '<paragraph>http://example.com/image.png[]</paragraph>'
  258. );
  259. setTimeout( () => {
  260. editor.commands.execute( 'undo' );
  261. expect( getData( editor.model ) ).to.equal(
  262. '<paragraph>[]</paragraph>'
  263. );
  264. done();
  265. } );
  266. } );
  267. // Checking whether paste+typing calls the auto-image handler once.
  268. it( 'pasting handler should be executed once', done => {
  269. const AutoImagePlugin = editor.plugins.get( AutoImage );
  270. const autoImageHandler = AutoImagePlugin._embedImageBetweenPositions;
  271. let counter = 0;
  272. AutoImagePlugin._embedImageBetweenPositions = function( ...args ) {
  273. counter += 1;
  274. return autoImageHandler.apply( this, args );
  275. };
  276. setData( editor.model, '<paragraph>[]</paragraph>' );
  277. pasteHtml( editor, 'http://example.com/image.png' );
  278. simulateTyping( 'Foo. Bar.' );
  279. setTimeout( () => {
  280. AutoImagePlugin._embedImageBetweenPositions = autoImageHandler;
  281. expect( counter ).to.equal( 1 );
  282. done();
  283. }, 100 );
  284. } );
  285. it( 'typing before pasted link during collaboration should not blow up', done => {
  286. setData( editor.model, '<paragraph>[]</paragraph>' );
  287. pasteHtml( editor, 'http://example.com/image.png' );
  288. for ( let i = 0; i < 10; ++i ) {
  289. const rootEl = editor.model.document.getRoot();
  290. setTimeout( () => {
  291. editor.model.enqueueChange( 'transparent', writer => {
  292. writer.insertText( characters[ i ], writer.createPositionFromPath( rootEl, [ 0, i ] ) );
  293. } );
  294. }, i * 5 );
  295. }
  296. setTimeout( () => {
  297. expect( getData( editor.model ) ).to.equal(
  298. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  299. '<paragraph>ABCDEFGHIJ</paragraph>'
  300. );
  301. done();
  302. }, 100 );
  303. } );
  304. it( 'typing after pasted link during collaboration should not blow up', done => {
  305. setData( editor.model, '<paragraph>[]</paragraph>' );
  306. pasteHtml( editor, 'http://example.com/image.png' );
  307. for ( let i = 0; i < 10; ++i ) {
  308. setTimeout( () => {
  309. editor.model.enqueueChange( 'transparent', writer => {
  310. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  311. } );
  312. }, i * 5 );
  313. }
  314. setTimeout( () => {
  315. expect( getData( editor.model ) ).to.equal(
  316. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  317. '<paragraph>ABCDEFGHIJ</paragraph>'
  318. );
  319. done();
  320. }, 100 );
  321. } );
  322. it( 'should insert the image element even if parent element where the URL was pasted has been deleted', done => {
  323. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  324. pasteHtml( editor, 'http://example.com/image.png' );
  325. editor.model.enqueueChange( 'transparent', writer => {
  326. writer.remove( writer.createRangeOn( editor.model.document.getRoot().getChild( 1 ) ) );
  327. } );
  328. setTimeout( () => {
  329. expect( getData( editor.model ) ).to.equal(
  330. '<paragraph>Foo.</paragraph>' +
  331. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  332. );
  333. done();
  334. }, 100 );
  335. } );
  336. it( 'should insert the image element even if new element appeared above the pasted URL', done => {
  337. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  338. pasteHtml( editor, 'http://example.com/image.png' );
  339. editor.model.enqueueChange( 'transparent', writer => {
  340. const paragraph = writer.createElement( 'paragraph' );
  341. writer.insert( paragraph, writer.createPositionAfter( editor.model.document.getRoot().getChild( 0 ) ) );
  342. writer.setSelection( paragraph, 'in' );
  343. } );
  344. for ( let i = 0; i < 10; ++i ) {
  345. setTimeout( () => {
  346. editor.model.enqueueChange( 'transparent', writer => {
  347. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  348. } );
  349. }, i * 5 );
  350. }
  351. setTimeout( () => {
  352. expect( getData( editor.model ) ).to.equal(
  353. '<paragraph>Foo.</paragraph>' +
  354. '<paragraph>ABCDEFGHIJ</paragraph>' +
  355. '<paragraph>Bar.</paragraph>' +
  356. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  357. );
  358. done();
  359. }, 100 );
  360. } );
  361. it( 'should insert the image element even if new element appeared below the pasted URL', done => {
  362. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  363. pasteHtml( editor, 'http://example.com/image.png' );
  364. editor.model.enqueueChange( 'transparent', writer => {
  365. const paragraph = writer.createElement( 'paragraph' );
  366. writer.insert( paragraph, writer.createPositionAfter( editor.model.document.getRoot().getChild( 1 ) ) );
  367. writer.setSelection( paragraph, 'in' );
  368. } );
  369. for ( let i = 0; i < 10; ++i ) {
  370. setTimeout( () => {
  371. editor.model.enqueueChange( 'transparent', writer => {
  372. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  373. } );
  374. }, i * 5 );
  375. }
  376. setTimeout( () => {
  377. expect( getData( editor.model ) ).to.equal(
  378. '<paragraph>Foo.</paragraph>' +
  379. '<paragraph>Bar.</paragraph>' +
  380. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  381. '<paragraph>ABCDEFGHIJ</paragraph>'
  382. );
  383. done();
  384. }, 100 );
  385. } );
  386. } );
  387. it( 'should detach LiveRange', async () => {
  388. const editor = await ClassicTestEditor.create( editorElement, {
  389. plugins: [ Typing, Paragraph, Link, Image, ImageCaption, Table, AutoImage ]
  390. } );
  391. setData(
  392. editor.model,
  393. '<table>' +
  394. '<tableRow>' +
  395. '[<tableCell><paragraph>foo</paragraph></tableCell>]' +
  396. '[<tableCell><paragraph>bar</paragraph></tableCell>]' +
  397. '</tableRow>' +
  398. '</table>'
  399. );
  400. pasteHtml( editor, '<table><tr><td>one</td><td>two</td></tr></table>' );
  401. expect( getData( editor.model, { withoutSelection: true } ) ).to.equal(
  402. '<table>' +
  403. '<tableRow>' +
  404. '<tableCell><paragraph>one</paragraph></tableCell>' +
  405. '<tableCell><paragraph>two</paragraph></tableCell>' +
  406. '</tableRow>' +
  407. '</table>'
  408. );
  409. expect( () => {
  410. editor.setData( '' );
  411. } ).not.to.throw();
  412. await editor.destroy();
  413. } );
  414. function simulateTyping( text ) {
  415. // While typing, every character is an atomic change.
  416. text.split( '' ).forEach( character => {
  417. editor.execute( 'input', {
  418. text: character
  419. } );
  420. } );
  421. }
  422. function pasteHtml( editor, html ) {
  423. editor.editing.view.document.fire( 'paste', {
  424. dataTransfer: createDataTransfer( { 'text/html': html } ),
  425. stopPropagation() {},
  426. preventDefault() {}
  427. } );
  428. }
  429. function createDataTransfer( data ) {
  430. return {
  431. getData( type ) {
  432. return data[ type ];
  433. }
  434. };
  435. }
  436. } );