8
0

automediaembed.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 Typing from '@ckeditor/ckeditor5-typing/src/typing';
  15. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  16. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. describe( 'AutoMediaEmbed - integration', () => {
  18. let editorElement, editor;
  19. beforeEach( () => {
  20. editorElement = global.document.createElement( 'div' );
  21. global.document.body.appendChild( editorElement );
  22. return ClassicTestEditor
  23. .create( editorElement, {
  24. plugins: [ MediaEmbed, AutoMediaEmbed, Link, List, Bold, Typing ]
  25. } )
  26. .then( newEditor => {
  27. editor = newEditor;
  28. } );
  29. } );
  30. afterEach( () => {
  31. editorElement.remove();
  32. return editor.destroy();
  33. } );
  34. it( 'should load Clipboard plugin', () => {
  35. expect( editor.plugins.get( Clipboard ) ).to.instanceOf( Clipboard );
  36. } );
  37. it( 'has proper name', () => {
  38. expect( AutoMediaEmbed.pluginName ).to.equal( 'AutoMediaEmbed' );
  39. } );
  40. describe( 'use fake timers', () => {
  41. let clock;
  42. beforeEach( () => {
  43. clock = sinon.useFakeTimers();
  44. } );
  45. afterEach( () => {
  46. clock.restore();
  47. } );
  48. it( 'replaces pasted text with media element after 100ms', () => {
  49. setData( editor.model, '<paragraph>[]</paragraph>' );
  50. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  51. expect( getData( editor.model ) ).to.equal(
  52. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  53. );
  54. clock.tick( 100 );
  55. expect( getData( editor.model ) ).to.equal(
  56. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  57. );
  58. } );
  59. it( 'can undo auto-embeding', () => {
  60. setData( editor.model, '<paragraph>[]</paragraph>' );
  61. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  62. expect( getData( editor.model ) ).to.equal(
  63. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  64. );
  65. clock.tick( 100 );
  66. editor.commands.execute( 'undo' );
  67. expect( getData( editor.model ) ).to.equal(
  68. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  69. );
  70. } );
  71. it( 'works for a full URL (https + "www" sub-domain)', () => {
  72. setData( editor.model, '<paragraph>[]</paragraph>' );
  73. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  74. clock.tick( 100 );
  75. expect( getData( editor.model ) ).to.equal(
  76. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  77. );
  78. } );
  79. it( 'works for a full URL (https without "wwww" sub-domain)', () => {
  80. setData( editor.model, '<paragraph>[]</paragraph>' );
  81. pasteHtml( editor, 'https://youtube.com/watch?v=H08tGjXNHO4' );
  82. clock.tick( 100 );
  83. expect( getData( editor.model ) ).to.equal(
  84. '<paragraph></paragraph>[<media url="https://youtube.com/watch?v=H08tGjXNHO4"></media>]'
  85. );
  86. } );
  87. it( 'works for a full URL (http + "www" sub-domain)', () => {
  88. setData( editor.model, '<paragraph>[]</paragraph>' );
  89. pasteHtml( editor, 'http://www.youtube.com/watch?v=H08tGjXNHO4' );
  90. clock.tick( 100 );
  91. expect( getData( editor.model ) ).to.equal(
  92. '<paragraph></paragraph>[<media url="http://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  93. );
  94. } );
  95. it( 'works for a full URL (http without "wwww" sub-domain)', () => {
  96. setData( editor.model, '<paragraph>[]</paragraph>' );
  97. pasteHtml( editor, 'http://youtube.com/watch?v=H08tGjXNHO4' );
  98. clock.tick( 100 );
  99. expect( getData( editor.model ) ).to.equal(
  100. '<paragraph></paragraph>[<media url="http://youtube.com/watch?v=H08tGjXNHO4"></media>]'
  101. );
  102. } );
  103. it( 'works for a URL without protocol (with "www" sub-domain)', () => {
  104. setData( editor.model, '<paragraph>[]</paragraph>' );
  105. pasteHtml( editor, 'www.youtube.com/watch?v=H08tGjXNHO4' );
  106. clock.tick( 100 );
  107. expect( getData( editor.model ) ).to.equal(
  108. '<paragraph></paragraph>[<media url="www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  109. );
  110. } );
  111. it( 'works for a URL without protocol (without "www" sub-domain)', () => {
  112. setData( editor.model, '<paragraph>[]</paragraph>' );
  113. pasteHtml( editor, 'youtube.com/watch?v=H08tGjXNHO4' );
  114. clock.tick( 100 );
  115. expect( getData( editor.model ) ).to.equal(
  116. '<paragraph></paragraph>[<media url="youtube.com/watch?v=H08tGjXNHO4"></media>]'
  117. );
  118. } );
  119. it( 'works fine if a media has no preview', () => {
  120. setData( editor.model, '<paragraph>[]</paragraph>' );
  121. pasteHtml( editor, 'https://twitter.com/ckeditor/status/1035181110140063749' );
  122. clock.tick( 100 );
  123. expect( getData( editor.model ) ).to.equal(
  124. '<paragraph></paragraph>[<media url="https://twitter.com/ckeditor/status/1035181110140063749"></media>]'
  125. );
  126. } );
  127. it( 'works for URL that was pasted as a link', () => {
  128. setData( editor.model, '<paragraph>[]</paragraph>' );
  129. pasteHtml( editor, '<a href="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4</a>' );
  130. clock.tick( 100 );
  131. expect( getData( editor.model ) ).to.equal(
  132. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  133. );
  134. } );
  135. it( 'works for URL that contains some inline styles', () => {
  136. setData( editor.model, '<paragraph>[]</paragraph>' );
  137. pasteHtml( editor, '<b>https://www.youtube.com/watch?v=H08tGjXNHO4</b>' );
  138. clock.tick( 100 );
  139. expect( getData( editor.model ) ).to.equal(
  140. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  141. );
  142. } );
  143. it( 'works for not collapsed selection inside single element', () => {
  144. setData( editor.model, '<paragraph>[Foo]</paragraph>' );
  145. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  146. clock.tick( 100 );
  147. expect( getData( editor.model ) ).to.equal(
  148. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  149. );
  150. } );
  151. it( 'works for not collapsed selection over a few elements', () => {
  152. setData( editor.model, '<paragraph>Fo[o</paragraph><paragraph>Ba]r</paragraph>' );
  153. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  154. clock.tick( 100 );
  155. expect( getData( editor.model ) ).to.equal(
  156. '<paragraph>For</paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  157. );
  158. } );
  159. it( 'does nothing if a URL is invalid', () => {
  160. setData( editor.model, '<paragraph>[]</paragraph>' );
  161. pasteHtml( editor, 'https://youtube.com' );
  162. clock.tick( 100 );
  163. expect( getData( editor.model ) ).to.equal(
  164. '<paragraph>https://youtube.com[]</paragraph>'
  165. );
  166. } );
  167. it( 'does nothing if pasted two links as text', () => {
  168. setData( editor.model, '<paragraph>[]</paragraph>' );
  169. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4 https://www.youtube.com/watch?v=H08tGjXNHO4' );
  170. clock.tick( 100 );
  171. expect( getData( editor.model ) ).to.equal(
  172. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4 https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  173. );
  174. } );
  175. it( 'does nothing if pasted text contains a valid URL', () => {
  176. setData( editor.model, '<paragraph>[]</paragraph>' );
  177. pasteHtml( editor, 'Foo bar https://www.youtube.com/watch?v=H08tGjXNHO4 bar foo.' );
  178. clock.tick( 100 );
  179. expect( getData( editor.model ) ).to.equal(
  180. '<paragraph>Foo bar https://www.youtube.com/watch?v=H08tGjXNHO4 bar foo.[]</paragraph>'
  181. );
  182. } );
  183. it( 'does nothing if pasted more than single node', () => {
  184. setData( editor.model, '<paragraph>[]</paragraph>' );
  185. pasteHtml( editor,
  186. 'https://www.youtube.com/watch?v=H08tGjXNHO4 ' +
  187. '<a href="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4</a>'
  188. );
  189. clock.tick( 100 );
  190. expect( getData( editor.model ) ).to.equal(
  191. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4 ' +
  192. '<$text linkHref="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4[]</$text>' +
  193. '</paragraph>'
  194. );
  195. } );
  196. it( 'does nothing if pasted a paragraph with the url', () => {
  197. setData( editor.model, '<paragraph>[]</paragraph>' );
  198. pasteHtml( editor, '<p>https://www.youtube.com/watch?v=H08tGjXNHO4</p>' );
  199. clock.tick( 100 );
  200. expect( getData( editor.model ) ).to.equal(
  201. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  202. );
  203. } );
  204. it( 'does nothing if pasted a block of content that looks like a URL', () => {
  205. setData( editor.model, '<paragraph>[]</paragraph>' );
  206. pasteHtml( editor, '<ul><li>https://</li><li>youtube.com/watch?</li></ul><p>v=H08tGjXNHO4</p>' );
  207. clock.tick( 100 );
  208. expect( getData( editor.model ) ).to.equal(
  209. '<listItem listIndent="0" listType="bulleted">https://</listItem>' +
  210. '<listItem listIndent="0" listType="bulleted">youtube.com/watch?</listItem>' +
  211. '<paragraph>v=H08tGjXNHO4[]</paragraph>'
  212. );
  213. } );
  214. it( 'does nothing if a URL is invalid (space inside URL)', () => {
  215. setData( editor.model, '<paragraph>[]</paragraph>' );
  216. pasteHtml( editor, 'youtube.com/watch?v=H08tGjXNHO4&amp;param=foo bar' );
  217. clock.tick( 100 );
  218. expect( getData( editor.model ) ).to.equal(
  219. '<paragraph>youtube.com/watch?v=H08tGjXNHO4&param=foo bar[]</paragraph>'
  220. );
  221. } );
  222. it( 'does nothing if URL match to media but it was removed', () => {
  223. return ClassicTestEditor
  224. .create( editorElement, {
  225. plugins: [ MediaEmbed, AutoMediaEmbed, Paragraph ],
  226. mediaEmbed: {
  227. removeProviders: [ 'youtube' ]
  228. }
  229. } )
  230. .then( newEditor => {
  231. editor = newEditor;
  232. setData( editor.model, '<paragraph>[]</paragraph>' );
  233. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  234. clock.tick( 100 );
  235. expect( getData( editor.model ) ).to.equal(
  236. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  237. );
  238. editorElement.remove();
  239. return editor.destroy();
  240. } );
  241. } );
  242. } );
  243. describe( 'real timers', () => {
  244. it( 'undo breaks the auto-media embed feature', done => {
  245. setData( editor.model, '<paragraph>[]</paragraph>' );
  246. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  247. expect( getData( editor.model ) ).to.equal(
  248. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  249. );
  250. setTimeout( () => {
  251. editor.commands.execute( 'undo' );
  252. expect( getData( editor.model ) ).to.equal(
  253. '<paragraph>[]</paragraph>'
  254. );
  255. done();
  256. } );
  257. } );
  258. // Checking whether paste+typing calls the auto-media handler once.
  259. it( 'pasting handler should be executed once', done => {
  260. const autoMediaEmbedPlugin = editor.plugins.get( AutoMediaEmbed );
  261. const autoMediaHandler = autoMediaEmbedPlugin._autoEmbedingEventHandler;
  262. let counter = 0;
  263. autoMediaEmbedPlugin._autoEmbedingEventHandler = function( ...args ) {
  264. counter += 1;
  265. return autoMediaHandler.apply( this, args );
  266. };
  267. setData( editor.model, '<paragraph>[]</paragraph>' );
  268. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  269. simulateTyping( 'Foo. Bar.' );
  270. setTimeout( () => {
  271. autoMediaEmbedPlugin._autoEmbedingEventHandler = autoMediaHandler;
  272. expect( counter ).to.equal( 1 );
  273. done();
  274. }, 100 );
  275. } );
  276. } );
  277. function simulateTyping( text ) {
  278. // While typing, every character is an atomic change.
  279. text.split( '' ).forEach( character => {
  280. editor.execute( 'input', {
  281. text: character
  282. } );
  283. } );
  284. }
  285. function pasteHtml( editor, html ) {
  286. editor.editing.view.document.fire( 'paste', {
  287. dataTransfer: createDataTransfer( { 'text/html': html } ),
  288. preventDefault() {
  289. }
  290. } );
  291. }
  292. function createDataTransfer( data ) {
  293. return {
  294. getData( type ) {
  295. return data[ type ];
  296. }
  297. };
  298. }
  299. } );