automediaembed.js 17 KB

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