automediaembed.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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', () => {
  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( 'does nothing if a URL is invalid', () => {
  176. setData( editor.model, '<paragraph>[]</paragraph>' );
  177. pasteHtml( editor, 'https://youtube.com' );
  178. clock.tick( 100 );
  179. expect( getData( editor.model ) ).to.equal(
  180. '<paragraph>https://youtube.com[]</paragraph>'
  181. );
  182. } );
  183. it( 'does nothing if pasted two links as text', () => {
  184. setData( editor.model, '<paragraph>[]</paragraph>' );
  185. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4 https://www.youtube.com/watch?v=H08tGjXNHO4' );
  186. clock.tick( 100 );
  187. expect( getData( editor.model ) ).to.equal(
  188. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4 https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  189. );
  190. } );
  191. it( 'does nothing if pasted text contains a valid URL', () => {
  192. setData( editor.model, '<paragraph>[]</paragraph>' );
  193. pasteHtml( editor, 'Foo bar https://www.youtube.com/watch?v=H08tGjXNHO4 bar foo.' );
  194. clock.tick( 100 );
  195. expect( getData( editor.model ) ).to.equal(
  196. '<paragraph>Foo bar https://www.youtube.com/watch?v=H08tGjXNHO4 bar foo.[]</paragraph>'
  197. );
  198. } );
  199. it( 'does nothing if pasted more than single node', () => {
  200. setData( editor.model, '<paragraph>[]</paragraph>' );
  201. pasteHtml( editor,
  202. 'https://www.youtube.com/watch?v=H08tGjXNHO4 ' +
  203. '<a href="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4</a>'
  204. );
  205. clock.tick( 100 );
  206. expect( getData( editor.model ) ).to.equal(
  207. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4 ' +
  208. '<$text linkHref="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4[]</$text>' +
  209. '</paragraph>'
  210. );
  211. } );
  212. it( 'does nothing if pasted a paragraph with the url', () => {
  213. setData( editor.model, '<paragraph>[]</paragraph>' );
  214. pasteHtml( editor, '<p>https://www.youtube.com/watch?v=H08tGjXNHO4</p>' );
  215. clock.tick( 100 );
  216. expect( getData( editor.model ) ).to.equal(
  217. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  218. );
  219. } );
  220. it( 'does nothing if pasted a block of content that looks like a URL', () => {
  221. setData( editor.model, '<paragraph>[]</paragraph>' );
  222. pasteHtml( editor, '<ul><li>https://</li><li>youtube.com/watch?</li></ul><p>v=H08tGjXNHO4</p>' );
  223. clock.tick( 100 );
  224. expect( getData( editor.model ) ).to.equal(
  225. '<listItem listIndent="0" listType="bulleted">https://</listItem>' +
  226. '<listItem listIndent="0" listType="bulleted">youtube.com/watch?</listItem>' +
  227. '<paragraph>v=H08tGjXNHO4[]</paragraph>'
  228. );
  229. } );
  230. it( 'does nothing if a URL is invalid (space inside URL)', () => {
  231. setData( editor.model, '<paragraph>[]</paragraph>' );
  232. pasteHtml( editor, 'youtube.com/watch?v=H08tGjXNHO4&amp;param=foo bar' );
  233. clock.tick( 100 );
  234. expect( getData( editor.model ) ).to.equal(
  235. '<paragraph>youtube.com/watch?v=H08tGjXNHO4&param=foo bar[]</paragraph>'
  236. );
  237. } );
  238. // #47
  239. it( 'does not transform a valid URL into a media if the element cannot be placed in the current position', () => {
  240. setData( editor.model, '<image src="foo.png"><caption>Foo.[]</caption></image>' );
  241. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  242. clock.tick( 100 );
  243. expect( getData( editor.model ) ).to.equal(
  244. '<image src="foo.png"><caption>Foo.https://www.youtube.com/watch?v=H08tGjXNHO4[]</caption></image>'
  245. );
  246. } );
  247. it( 'replaces a URL in media if pasted a link when other media element was selected', () => {
  248. setData(
  249. editor.model,
  250. '[<media url="https://open.spotify.com/album/2IXlgvecaDqOeF3viUZnPI?si=ogVw7KlcQAGZKK4Jz9QzvA"></media>]'
  251. );
  252. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  253. clock.tick( 100 );
  254. expect( getData( editor.model ) ).to.equal(
  255. '[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  256. );
  257. } );
  258. it( 'inserts a new media element if pasted a link when other media element was selected in correct place', () => {
  259. setData(
  260. editor.model,
  261. '<paragraph>Foo. <$text linkHref="https://cksource.com">Bar</$text></paragraph>' +
  262. '[<media url="https://open.spotify.com/album/2IXlgvecaDqOeF3viUZnPI?si=ogVw7KlcQAGZKK4Jz9QzvA"></media>]' +
  263. '<paragraph><$text bold="true">Bar</$text>.</paragraph>'
  264. );
  265. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  266. clock.tick( 100 );
  267. expect( getData( editor.model ) ).to.equal(
  268. '<paragraph>Foo. <$text linkHref="https://cksource.com">Bar</$text></paragraph>' +
  269. '[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]' +
  270. '<paragraph><$text bold="true">Bar</$text>.</paragraph>'
  271. );
  272. } );
  273. it( 'does nothing if URL match to media but it was removed', () => {
  274. return ClassicTestEditor
  275. .create( editorElement, {
  276. plugins: [ MediaEmbed, AutoMediaEmbed, Paragraph ],
  277. mediaEmbed: {
  278. removeProviders: [ 'youtube' ]
  279. }
  280. } )
  281. .then( newEditor => {
  282. editor = newEditor;
  283. setData( editor.model, '<paragraph>[]</paragraph>' );
  284. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  285. clock.tick( 100 );
  286. expect( getData( editor.model ) ).to.equal(
  287. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  288. );
  289. editorElement.remove();
  290. return editor.destroy();
  291. } );
  292. } );
  293. } );
  294. describe( 'use real timers', () => {
  295. const characters = Array( 10 ).fill( 1 ).map( ( x, i ) => String.fromCharCode( 65 + i ) );
  296. it( 'undo breaks the auto-media embed feature (undo was done before auto-media embed)', done => {
  297. setData( editor.model, '<paragraph>[]</paragraph>' );
  298. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  299. expect( getData( editor.model ) ).to.equal(
  300. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  301. );
  302. setTimeout( () => {
  303. editor.commands.execute( 'undo' );
  304. expect( getData( editor.model ) ).to.equal(
  305. '<paragraph>[]</paragraph>'
  306. );
  307. done();
  308. } );
  309. } );
  310. // Checking whether paste+typing calls the auto-media handler once.
  311. it( 'pasting handler should be executed once', done => {
  312. const autoMediaEmbedPlugin = editor.plugins.get( AutoMediaEmbed );
  313. const autoMediaHandler = autoMediaEmbedPlugin._embedMediaBetweenPositions;
  314. let counter = 0;
  315. autoMediaEmbedPlugin._embedMediaBetweenPositions = function( ...args ) {
  316. counter += 1;
  317. return autoMediaHandler.apply( this, args );
  318. };
  319. setData( editor.model, '<paragraph>[]</paragraph>' );
  320. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  321. simulateTyping( 'Foo. Bar.' );
  322. setTimeout( () => {
  323. autoMediaEmbedPlugin._embedMediaBetweenPositions = autoMediaHandler;
  324. expect( counter ).to.equal( 1 );
  325. done();
  326. }, 100 );
  327. } );
  328. it( 'typing before pasted link during collaboration should not blow up', done => {
  329. setData( editor.model, '<paragraph>[]</paragraph>' );
  330. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  331. for ( let i = 0; i < 10; ++i ) {
  332. const rootEl = editor.model.document.getRoot();
  333. setTimeout( () => {
  334. editor.model.enqueueChange( 'transparent', writer => {
  335. writer.insertText( characters[ i ], writer.createPositionFromPath( rootEl, [ 0, i ] ) );
  336. } );
  337. }, i * 5 );
  338. }
  339. setTimeout( () => {
  340. expect( getData( editor.model ) ).to.equal(
  341. '[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]<paragraph>ABCDEFGHIJ</paragraph>'
  342. );
  343. done();
  344. }, 100 );
  345. } );
  346. it( 'typing after pasted link during collaboration should not blow up', done => {
  347. setData( editor.model, '<paragraph>[]</paragraph>' );
  348. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  349. for ( let i = 0; i < 10; ++i ) {
  350. setTimeout( () => {
  351. editor.model.enqueueChange( 'transparent', writer => {
  352. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  353. } );
  354. }, i * 5 );
  355. }
  356. setTimeout( () => {
  357. expect( getData( editor.model ) ).to.equal(
  358. '[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]<paragraph>ABCDEFGHIJ</paragraph>'
  359. );
  360. done();
  361. }, 100 );
  362. } );
  363. it( 'should insert the media 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://www.youtube.com/watch?v=H08tGjXNHO4' );
  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>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  372. );
  373. done();
  374. }, 100 );
  375. } );
  376. it( 'should insert the media element even if new element appeared above the pasted URL', done => {
  377. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  378. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  379. editor.model.enqueueChange( 'transparent', writer => {
  380. const paragraph = writer.createElement( 'paragraph' );
  381. writer.insert( paragraph, writer.createPositionAfter( editor.model.document.getRoot().getChild( 0 ) ) );
  382. writer.setSelection( paragraph, 'in' );
  383. } );
  384. for ( let i = 0; i < 10; ++i ) {
  385. setTimeout( () => {
  386. editor.model.enqueueChange( 'transparent', writer => {
  387. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  388. } );
  389. }, i * 5 );
  390. }
  391. setTimeout( () => {
  392. expect( getData( editor.model ) ).to.equal(
  393. '<paragraph>Foo.</paragraph>' +
  394. '<paragraph>ABCDEFGHIJ</paragraph>' +
  395. '<paragraph>Bar.</paragraph>' +
  396. '[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  397. );
  398. done();
  399. }, 100 );
  400. } );
  401. it( 'should insert the media element even if new element appeared below the pasted URL', done => {
  402. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  403. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  404. editor.model.enqueueChange( 'transparent', writer => {
  405. const paragraph = writer.createElement( 'paragraph' );
  406. writer.insert( paragraph, writer.createPositionAfter( editor.model.document.getRoot().getChild( 1 ) ) );
  407. writer.setSelection( paragraph, 'in' );
  408. } );
  409. for ( let i = 0; i < 10; ++i ) {
  410. setTimeout( () => {
  411. editor.model.enqueueChange( 'transparent', writer => {
  412. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  413. } );
  414. }, i * 5 );
  415. }
  416. setTimeout( () => {
  417. expect( getData( editor.model ) ).to.equal(
  418. '<paragraph>Foo.</paragraph>' +
  419. '<paragraph>Bar.</paragraph>' +
  420. '[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]' +
  421. '<paragraph>ABCDEFGHIJ</paragraph>'
  422. );
  423. done();
  424. }, 100 );
  425. } );
  426. } );
  427. function simulateTyping( text ) {
  428. // While typing, every character is an atomic change.
  429. text.split( '' ).forEach( character => {
  430. editor.execute( 'input', {
  431. text: character
  432. } );
  433. } );
  434. }
  435. function pasteHtml( editor, html ) {
  436. editor.editing.view.document.fire( 'paste', {
  437. dataTransfer: createDataTransfer( { 'text/html': html } ),
  438. preventDefault() {
  439. }
  440. } );
  441. }
  442. function createDataTransfer( data ) {
  443. return {
  444. getData( type ) {
  445. return data[ type ];
  446. }
  447. };
  448. }
  449. } );