autoimage.js 21 KB

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