8
0

automediaembed.js 18 KB

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