automediaembed.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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>For</paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  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( 'does nothing if URL match to media but it was removed', () => {
  229. return ClassicTestEditor
  230. .create( editorElement, {
  231. plugins: [ MediaEmbed, AutoMediaEmbed, Paragraph ],
  232. mediaEmbed: {
  233. removeProviders: [ 'youtube' ]
  234. }
  235. } )
  236. .then( newEditor => {
  237. editor = newEditor;
  238. setData( editor.model, '<paragraph>[]</paragraph>' );
  239. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  240. clock.tick( 100 );
  241. expect( getData( editor.model ) ).to.equal(
  242. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  243. );
  244. editorElement.remove();
  245. return editor.destroy();
  246. } );
  247. } );
  248. } );
  249. describe( 'use real timers', () => {
  250. const characters = Array( 10 ).fill( 1 ).map( ( x, i ) => String.fromCharCode( 65 + i ) );
  251. it( 'undo breaks the auto-media embed feature (undo was done before auto-media embed)', done => {
  252. setData( editor.model, '<paragraph>[]</paragraph>' );
  253. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  254. expect( getData( editor.model ) ).to.equal(
  255. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  256. );
  257. setTimeout( () => {
  258. editor.commands.execute( 'undo' );
  259. expect( getData( editor.model ) ).to.equal(
  260. '<paragraph>[]</paragraph>'
  261. );
  262. done();
  263. } );
  264. } );
  265. // Checking whether paste+typing calls the auto-media handler once.
  266. it( 'pasting handler should be executed once', done => {
  267. const autoMediaEmbedPlugin = editor.plugins.get( AutoMediaEmbed );
  268. const autoMediaHandler = autoMediaEmbedPlugin._embedMediaBetweenPositions;
  269. let counter = 0;
  270. autoMediaEmbedPlugin._embedMediaBetweenPositions = function( ...args ) {
  271. counter += 1;
  272. return autoMediaHandler.apply( this, args );
  273. };
  274. setData( editor.model, '<paragraph>[]</paragraph>' );
  275. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  276. simulateTyping( 'Foo. Bar.' );
  277. setTimeout( () => {
  278. autoMediaEmbedPlugin._embedMediaBetweenPositions = autoMediaHandler;
  279. expect( counter ).to.equal( 1 );
  280. done();
  281. }, 100 );
  282. } );
  283. it( 'typing before pasted link during collaboration should not blow up', done => {
  284. setData( editor.model, '<paragraph>[]</paragraph>' );
  285. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  286. for ( let i = 0; i < 10; ++i ) {
  287. const rootEl = editor.model.document.getRoot();
  288. setTimeout( () => {
  289. editor.model.enqueueChange( 'transparent', writer => {
  290. writer.insertText( characters[ i ], new ModelPosition( rootEl, [ 0, i ] ) );
  291. } );
  292. }, i * 5 );
  293. }
  294. setTimeout( () => {
  295. expect( getData( editor.model ) ).to.equal(
  296. '<paragraph>ABCDEFGHIJ</paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  297. );
  298. done();
  299. }, 100 );
  300. } );
  301. it( 'typing after pasted link during collaboration should not blow up', done => {
  302. setData( editor.model, '<paragraph>[]</paragraph>' );
  303. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  304. for ( let i = 0; i < 10; ++i ) {
  305. setTimeout( () => {
  306. editor.model.enqueueChange( 'transparent', writer => {
  307. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  308. } );
  309. }, i * 5 );
  310. }
  311. setTimeout( () => {
  312. expect( getData( editor.model ) ).to.equal(
  313. '<paragraph>ABCDEFGHIJ</paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  314. );
  315. done();
  316. }, 100 );
  317. } );
  318. it( 'should insert the media element even if parent element where the URL was pasted has been deleted', done => {
  319. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  320. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  321. editor.model.enqueueChange( 'transparent', writer => {
  322. writer.remove( ModelRange.createOn( editor.model.document.getRoot().getChild( 1 ) ) );
  323. } );
  324. setTimeout( () => {
  325. expect( getData( editor.model ) ).to.equal(
  326. '<paragraph>Foo.</paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  327. );
  328. done();
  329. }, 100 );
  330. } );
  331. it( 'should insert the media element even if new element appeared above the pasted URL', done => {
  332. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  333. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  334. editor.model.enqueueChange( 'transparent', writer => {
  335. const paragraph = writer.createElement( 'paragraph' );
  336. writer.insert( paragraph, ModelPosition.createAfter( editor.model.document.getRoot().getChild( 0 ) ) );
  337. writer.setSelection( paragraph, 'in' );
  338. } );
  339. for ( let i = 0; i < 10; ++i ) {
  340. setTimeout( () => {
  341. editor.model.enqueueChange( 'transparent', writer => {
  342. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  343. } );
  344. }, i * 5 );
  345. }
  346. setTimeout( () => {
  347. expect( getData( editor.model ) ).to.equal(
  348. '<paragraph>Foo.</paragraph>' +
  349. '<paragraph>ABCDEFGHIJ</paragraph>' +
  350. '<paragraph>Bar.</paragraph>' +
  351. '[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  352. );
  353. done();
  354. }, 100 );
  355. } );
  356. it( 'should insert the media element even if new element appeared below the pasted URL', done => {
  357. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  358. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  359. editor.model.enqueueChange( 'transparent', writer => {
  360. const paragraph = writer.createElement( 'paragraph' );
  361. writer.insert( paragraph, ModelPosition.createAfter( editor.model.document.getRoot().getChild( 1 ) ) );
  362. writer.setSelection( paragraph, 'in' );
  363. } );
  364. for ( let i = 0; i < 10; ++i ) {
  365. setTimeout( () => {
  366. editor.model.enqueueChange( 'transparent', writer => {
  367. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  368. } );
  369. }, i * 5 );
  370. }
  371. setTimeout( () => {
  372. expect( getData( editor.model ) ).to.equal(
  373. '<paragraph>Foo.</paragraph>' +
  374. '<paragraph>Bar.</paragraph>' +
  375. '[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]' +
  376. '<paragraph>ABCDEFGHIJ</paragraph>'
  377. );
  378. done();
  379. }, 100 );
  380. } );
  381. } );
  382. function simulateTyping( text ) {
  383. // While typing, every character is an atomic change.
  384. text.split( '' ).forEach( character => {
  385. editor.execute( 'input', {
  386. text: character
  387. } );
  388. } );
  389. }
  390. function pasteHtml( editor, html ) {
  391. editor.editing.view.document.fire( 'paste', {
  392. dataTransfer: createDataTransfer( { 'text/html': html } ),
  393. preventDefault() {
  394. }
  395. } );
  396. }
  397. function createDataTransfer( data ) {
  398. return {
  399. getData( type ) {
  400. return data[ type ];
  401. }
  402. };
  403. }
  404. } );