8
0

automediaembed.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph></paragraph>[<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. '<paragraph>For</paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  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( 'does nothing if URL match to media but it was removed', () => {
  227. return ClassicTestEditor
  228. .create( editorElement, {
  229. plugins: [ MediaEmbed, AutoMediaEmbed, Paragraph ],
  230. mediaEmbed: {
  231. removeProviders: [ 'youtube' ]
  232. }
  233. } )
  234. .then( newEditor => {
  235. editor = newEditor;
  236. setData( editor.model, '<paragraph>[]</paragraph>' );
  237. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  238. clock.tick( 100 );
  239. expect( getData( editor.model ) ).to.equal(
  240. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  241. );
  242. editorElement.remove();
  243. return editor.destroy();
  244. } );
  245. } );
  246. } );
  247. describe( 'real timers', () => {
  248. it( 'undo breaks the auto-media embed feature', done => {
  249. setData( editor.model, '<paragraph>[]</paragraph>' );
  250. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  251. expect( getData( editor.model ) ).to.equal(
  252. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  253. );
  254. setTimeout( () => {
  255. editor.commands.execute( 'undo' );
  256. expect( getData( editor.model ) ).to.equal(
  257. '<paragraph>[]</paragraph>'
  258. );
  259. done();
  260. } );
  261. } );
  262. // Checking whether paste+typing calls the auto-media handler once.
  263. it( 'pasting handler should be executed once', done => {
  264. const autoMediaEmbedPlugin = editor.plugins.get( AutoMediaEmbed );
  265. const autoMediaHandler = autoMediaEmbedPlugin._autoEmbedingEventHandler;
  266. let counter = 0;
  267. autoMediaEmbedPlugin._autoEmbedingEventHandler = function( ...args ) {
  268. counter += 1;
  269. return autoMediaHandler.apply( this, args );
  270. };
  271. setData( editor.model, '<paragraph>[]</paragraph>' );
  272. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  273. simulateTyping( 'Foo. Bar.' );
  274. setTimeout( () => {
  275. autoMediaEmbedPlugin._autoEmbedingEventHandler = autoMediaHandler;
  276. expect( counter ).to.equal( 1 );
  277. done();
  278. }, 100 );
  279. } );
  280. } );
  281. function simulateTyping( text ) {
  282. // While typing, every character is an atomic change.
  283. text.split( '' ).forEach( character => {
  284. editor.execute( 'input', {
  285. text: character
  286. } );
  287. } );
  288. }
  289. function pasteHtml( editor, html ) {
  290. editor.editing.view.document.fire( 'paste', {
  291. dataTransfer: createDataTransfer( { 'text/html': html } ),
  292. preventDefault() {
  293. }
  294. } );
  295. }
  296. function createDataTransfer( data ) {
  297. return {
  298. getData( type ) {
  299. return data[ type ];
  300. }
  301. };
  302. }
  303. } );