8
0

mediaembed.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import MediaEmbed from '../src/mediaembed';
  7. import MediaEmbedEditing from '../src/mediaembedediting';
  8. import MediaEmbedUI from '../src/mediaembedui';
  9. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  10. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import Link from '@ckeditor/ckeditor5-link/src/link';
  13. import List from '@ckeditor/ckeditor5-list/src/list';
  14. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  15. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  16. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  17. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  18. describe( 'MediaEmbed', () => {
  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, Paragraph, Link, Bold, Undo, List ]
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. } );
  30. } );
  31. afterEach( () => {
  32. editorElement.remove();
  33. return editor.destroy();
  34. } );
  35. it( 'should be loaded', () => {
  36. expect( editor.plugins.get( MediaEmbed ) ).to.instanceOf( MediaEmbed );
  37. } );
  38. it( 'should load MediaEmbedEditing plugin', () => {
  39. expect( editor.plugins.get( MediaEmbedEditing ) ).to.instanceOf( MediaEmbedEditing );
  40. } );
  41. it( 'should load Widget plugin', () => {
  42. expect( editor.plugins.get( Widget ) ).to.instanceOf( Widget );
  43. } );
  44. it( 'should load Clipboard plugin', () => {
  45. expect( editor.plugins.get( Clipboard ) ).to.instanceOf( Clipboard );
  46. } );
  47. it( 'should load MediaEmbedUI plugin', () => {
  48. expect( editor.plugins.get( MediaEmbedUI ) ).to.instanceOf( MediaEmbedUI );
  49. } );
  50. it( 'has proper name', () => {
  51. expect( MediaEmbed.pluginName ).to.equal( 'MediaEmbed' );
  52. } );
  53. describe( 'auto-media embed', () => {
  54. let clock;
  55. beforeEach( () => {
  56. clock = sinon.useFakeTimers();
  57. } );
  58. afterEach( () => {
  59. clock.restore();
  60. } );
  61. it( 'replaces pasted text with media element after 500ms', () => {
  62. setData( editor.model, '<paragraph>[]</paragraph>' );
  63. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  64. expect( getData( editor.model ) ).to.equal(
  65. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  66. );
  67. clock.tick( 500 );
  68. expect( getData( editor.model ) ).to.equal(
  69. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  70. );
  71. } );
  72. it( 'can undo auto-embeding', () => {
  73. setData( editor.model, '<paragraph>[]</paragraph>' );
  74. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  75. expect( getData( editor.model ) ).to.equal(
  76. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  77. );
  78. clock.tick( 500 );
  79. editor.commands.execute( 'undo' );
  80. expect( getData( editor.model ) ).to.equal(
  81. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  82. );
  83. } );
  84. it( 'works for a full URL (https + "www" sub-domain)', () => {
  85. setData( editor.model, '<paragraph>[]</paragraph>' );
  86. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  87. clock.tick( 500 );
  88. expect( getData( editor.model ) ).to.equal(
  89. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  90. );
  91. } );
  92. it( 'works for a full URL (https without "wwww" sub-domain)', () => {
  93. setData( editor.model, '<paragraph>[]</paragraph>' );
  94. pasteHtml( editor, 'https://youtube.com/watch?v=H08tGjXNHO4' );
  95. clock.tick( 500 );
  96. expect( getData( editor.model ) ).to.equal(
  97. '<paragraph></paragraph>[<media url="https://youtube.com/watch?v=H08tGjXNHO4"></media>]'
  98. );
  99. } );
  100. it( 'works for a full URL (http + "www" sub-domain)', () => {
  101. setData( editor.model, '<paragraph>[]</paragraph>' );
  102. pasteHtml( editor, 'http://www.youtube.com/watch?v=H08tGjXNHO4' );
  103. clock.tick( 500 );
  104. expect( getData( editor.model ) ).to.equal(
  105. '<paragraph></paragraph>[<media url="http://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  106. );
  107. } );
  108. it( 'works for a full URL (http without "wwww" sub-domain)', () => {
  109. setData( editor.model, '<paragraph>[]</paragraph>' );
  110. pasteHtml( editor, 'http://youtube.com/watch?v=H08tGjXNHO4' );
  111. clock.tick( 500 );
  112. expect( getData( editor.model ) ).to.equal(
  113. '<paragraph></paragraph>[<media url="http://youtube.com/watch?v=H08tGjXNHO4"></media>]'
  114. );
  115. } );
  116. it( 'works for a URL without protocol (with "www" sub-domain)', () => {
  117. setData( editor.model, '<paragraph>[]</paragraph>' );
  118. pasteHtml( editor, 'www.youtube.com/watch?v=H08tGjXNHO4' );
  119. clock.tick( 500 );
  120. expect( getData( editor.model ) ).to.equal(
  121. '<paragraph></paragraph>[<media url="www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  122. );
  123. } );
  124. it( 'works for a URL without protocol (without "www" sub-domain)', () => {
  125. setData( editor.model, '<paragraph>[]</paragraph>' );
  126. pasteHtml( editor, 'youtube.com/watch?v=H08tGjXNHO4' );
  127. clock.tick( 500 );
  128. expect( getData( editor.model ) ).to.equal(
  129. '<paragraph></paragraph>[<media url="youtube.com/watch?v=H08tGjXNHO4"></media>]'
  130. );
  131. } );
  132. it( 'works fine if a media has no preview', () => {
  133. setData( editor.model, '<paragraph>[]</paragraph>' );
  134. pasteHtml( editor, 'https://twitter.com/ckeditor/status/1035181110140063749' );
  135. clock.tick( 500 );
  136. expect( getData( editor.model ) ).to.equal(
  137. '<paragraph></paragraph>[<media url="https://twitter.com/ckeditor/status/1035181110140063749"></media>]'
  138. );
  139. } );
  140. it( 'works for URL that was pasted as a link', () => {
  141. setData( editor.model, '<paragraph>[]</paragraph>' );
  142. pasteHtml( editor, '<a href="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4</a>' );
  143. clock.tick( 500 );
  144. expect( getData( editor.model ) ).to.equal(
  145. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  146. );
  147. } );
  148. it( 'works for URL that contains some inline styles', () => {
  149. setData( editor.model, '<paragraph>[]</paragraph>' );
  150. pasteHtml( editor, '<b>https://www.youtube.com/watch?v=H08tGjXNHO4</b>' );
  151. clock.tick( 500 );
  152. expect( getData( editor.model ) ).to.equal(
  153. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  154. );
  155. } );
  156. it( 'works for not collapsed selection inside single element', () => {
  157. setData( editor.model, '<paragraph>[Foo]</paragraph>' );
  158. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  159. clock.tick( 500 );
  160. expect( getData( editor.model ) ).to.equal(
  161. '<paragraph></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  162. );
  163. } );
  164. it( 'works for not collapsed selection over a few elements', () => {
  165. setData( editor.model, '<paragraph>Fo[o</paragraph><paragraph>Ba]r</paragraph>' );
  166. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  167. clock.tick( 500 );
  168. expect( getData( editor.model ) ).to.equal(
  169. '<paragraph>For</paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  170. );
  171. } );
  172. it( 'does nothing if a URL is invalid', () => {
  173. setData( editor.model, '<paragraph>[]</paragraph>' );
  174. pasteHtml( editor, 'https://youtube.com' );
  175. clock.tick( 500 );
  176. expect( getData( editor.model ) ).to.equal(
  177. '<paragraph>https://youtube.com[]</paragraph>'
  178. );
  179. } );
  180. it( 'does nothing if pasted two links as text', () => {
  181. setData( editor.model, '<paragraph>[]</paragraph>' );
  182. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4 https://www.youtube.com/watch?v=H08tGjXNHO4' );
  183. clock.tick( 500 );
  184. expect( getData( editor.model ) ).to.equal(
  185. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4 https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  186. );
  187. } );
  188. it( 'does nothing if pasted text contains a valid URL', () => {
  189. setData( editor.model, '<paragraph>[]</paragraph>' );
  190. pasteHtml( editor, 'Foo bar https://www.youtube.com/watch?v=H08tGjXNHO4 bar foo.' );
  191. clock.tick( 500 );
  192. expect( getData( editor.model ) ).to.equal(
  193. '<paragraph>Foo bar https://www.youtube.com/watch?v=H08tGjXNHO4 bar foo.[]</paragraph>'
  194. );
  195. } );
  196. it( 'does nothing if pasted more than single node', () => {
  197. setData( editor.model, '<paragraph>[]</paragraph>' );
  198. pasteHtml( editor,
  199. 'https://www.youtube.com/watch?v=H08tGjXNHO4 ' +
  200. '<a href="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4</a>'
  201. );
  202. clock.tick( 500 );
  203. expect( getData( editor.model ) ).to.equal(
  204. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4 ' +
  205. '<$text linkHref="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4[]</$text>' +
  206. '</paragraph>'
  207. );
  208. } );
  209. it( 'does nothing if pastes a block of content that looks like a URL', () => {
  210. setData( editor.model, '<paragraph>[]</paragraph>' );
  211. pasteHtml( editor, '<ul><li>https://</li><li>youtube.com/watch?</li></ul><p>v=H08tGjXNHO4</p>' );
  212. clock.tick( 500 );
  213. expect( getData( editor.model ) ).to.equal(
  214. '<listItem listIndent="0" listType="bulleted">https://</listItem>' +
  215. '<listItem listIndent="0" listType="bulleted">youtube.com/watch?</listItem>' +
  216. '<paragraph>v=H08tGjXNHO4[]</paragraph>'
  217. );
  218. } );
  219. it( 'does nothing if a URL is invalid (space inside URL)', () => {
  220. setData( editor.model, '<paragraph>[]</paragraph>' );
  221. pasteHtml( editor, 'youtube.com/watch?v=H08tGjXNHO4&amp;param=foo bar' );
  222. clock.tick( 500 );
  223. expect( getData( editor.model ) ).to.equal(
  224. '<paragraph>youtube.com/watch?v=H08tGjXNHO4&param=foo bar[]</paragraph>'
  225. );
  226. } );
  227. it( 'does nothing if URL match to media but it was removed', () => {
  228. return ClassicTestEditor
  229. .create( editorElement, {
  230. plugins: [ MediaEmbed, Paragraph ],
  231. mediaEmbed: {
  232. removeProviders: [ 'youtube' ]
  233. }
  234. } )
  235. .then( newEditor => {
  236. editor = newEditor;
  237. setData( editor.model, '<paragraph>[]</paragraph>' );
  238. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  239. clock.tick( 500 );
  240. expect( getData( editor.model ) ).to.equal(
  241. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  242. );
  243. editorElement.remove();
  244. return editor.destroy();
  245. } );
  246. } );
  247. function pasteHtml( editor, html ) {
  248. editor.editing.view.document.fire( 'paste', {
  249. dataTransfer: createDataTransfer( { 'text/html': html } ),
  250. preventDefault() {
  251. }
  252. } );
  253. }
  254. function createDataTransfer( data ) {
  255. return {
  256. getData( type ) {
  257. return data[ type ];
  258. }
  259. };
  260. }
  261. } );
  262. } );