automediaembed.js 19 KB

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