8
0

automediaembed.js 20 KB

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