mediaembed.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  14. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  15. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. describe( 'MediaEmbed', () => {
  17. let editorElement, editor;
  18. beforeEach( () => {
  19. editorElement = global.document.createElement( 'div' );
  20. global.document.body.appendChild( editorElement );
  21. return ClassicTestEditor
  22. .create( editorElement, {
  23. plugins: [ MediaEmbed, Paragraph, Link, Bold ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. } );
  28. } );
  29. afterEach( () => {
  30. editorElement.remove();
  31. return editor.destroy();
  32. } );
  33. it( 'should be loaded', () => {
  34. expect( editor.plugins.get( MediaEmbed ) ).to.instanceOf( MediaEmbed );
  35. } );
  36. it( 'should load MediaEmbedEditing plugin', () => {
  37. expect( editor.plugins.get( MediaEmbedEditing ) ).to.instanceOf( MediaEmbedEditing );
  38. } );
  39. it( 'should load Widget plugin', () => {
  40. expect( editor.plugins.get( Widget ) ).to.instanceOf( Widget );
  41. } );
  42. it( 'should load Clipboard plugin', () => {
  43. expect( editor.plugins.get( Clipboard ) ).to.instanceOf( Clipboard );
  44. } );
  45. it( 'should load MediaEmbedUI plugin', () => {
  46. expect( editor.plugins.get( MediaEmbedUI ) ).to.instanceOf( MediaEmbedUI );
  47. } );
  48. it( 'has proper name', () => {
  49. expect( MediaEmbed.pluginName ).to.equal( 'MediaEmbed' );
  50. } );
  51. describe.only( 'auto-media embed', () => {
  52. it( 'works for a full URL (https + "www" sub-domain)', () => {
  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></paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  57. );
  58. } );
  59. it( 'works for a full URL (https without "wwww" sub-domain)', () => {
  60. setData( editor.model, '<paragraph>[]</paragraph>' );
  61. pasteHtml( editor, 'https://youtube.com/watch?v=H08tGjXNHO4' );
  62. expect( getData( editor.model ) ).to.equal(
  63. '<paragraph></paragraph>[<media url="https://youtube.com/watch?v=H08tGjXNHO4"></media>]'
  64. );
  65. } );
  66. it( 'works for a full URL (http + "www" sub-domain)', () => {
  67. setData( editor.model, '<paragraph>[]</paragraph>' );
  68. pasteHtml( editor, 'http://www.youtube.com/watch?v=H08tGjXNHO4' );
  69. expect( getData( editor.model ) ).to.equal(
  70. '<paragraph></paragraph>[<media url="http://www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  71. );
  72. } );
  73. it( 'works for a full URL (http without "wwww" sub-domain)', () => {
  74. setData( editor.model, '<paragraph>[]</paragraph>' );
  75. pasteHtml( editor, 'http://youtube.com/watch?v=H08tGjXNHO4' );
  76. expect( getData( editor.model ) ).to.equal(
  77. '<paragraph></paragraph>[<media url="http://youtube.com/watch?v=H08tGjXNHO4"></media>]'
  78. );
  79. } );
  80. it( 'works for a URL without protocol (with "www" sub-domain)', () => {
  81. setData( editor.model, '<paragraph>[]</paragraph>' );
  82. pasteHtml( editor, 'www.youtube.com/watch?v=H08tGjXNHO4' );
  83. expect( getData( editor.model ) ).to.equal(
  84. '<paragraph></paragraph>[<media url="www.youtube.com/watch?v=H08tGjXNHO4"></media>]'
  85. );
  86. } );
  87. it( 'works for a URL without protocol (without "www" sub-domain)', () => {
  88. setData( editor.model, '<paragraph>[]</paragraph>' );
  89. pasteHtml( editor, 'youtube.com/watch?v=H08tGjXNHO4' );
  90. expect( getData( editor.model ) ).to.equal(
  91. '<paragraph></paragraph>[<media url="youtube.com/watch?v=H08tGjXNHO4"></media>]'
  92. );
  93. } );
  94. it( 'works fine if a media has no preview', () => {
  95. setData( editor.model, '<paragraph>[]</paragraph>' );
  96. pasteHtml( editor, 'https://twitter.com/ckeditor/status/1035181110140063749' );
  97. expect( getData( editor.model ) ).to.equal(
  98. '<paragraph></paragraph>[<media url="https://twitter.com/ckeditor/status/1035181110140063749"></media>]'
  99. );
  100. } );
  101. it( 'does nothing if a URL is invalid', () => {
  102. setData( editor.model, '<paragraph>[]</paragraph>' );
  103. pasteHtml( editor, 'https://youtube.com' );
  104. expect( getData( editor.model ) ).to.equal(
  105. '<paragraph>https://youtube.com[]</paragraph>'
  106. );
  107. } );
  108. it( 'does nothing if pasted two links as text', () => {
  109. setData( editor.model, '<paragraph>[]</paragraph>' );
  110. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4 https://www.youtube.com/watch?v=H08tGjXNHO4' );
  111. expect( getData( editor.model ) ).to.equal(
  112. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4 https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  113. );
  114. } );
  115. it( 'does nothing if pasted link', () => {
  116. setData( editor.model, '<paragraph>[]</paragraph>' );
  117. pasteHtml( editor, '<a href="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4</a>' );
  118. expect( getData( editor.model ) ).to.equal(
  119. '<paragraph>' +
  120. '<$text linkHref="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4[]</$text>' +
  121. '</paragraph>'
  122. );
  123. } );
  124. it( 'does nothing if node contains a valid URL but it is not a text', () => {
  125. setData( editor.model, '<paragraph>[]</paragraph>' );
  126. pasteHtml( editor, '<b>https://www.youtube.com/watch?v=H08tGjXNHO4</b>' );
  127. expect( getData( editor.model ) ).to.equal(
  128. '<paragraph>' +
  129. '<$text bold="true">https://www.youtube.com/watch?v=H08tGjXNHO4[]</$text>' +
  130. '</paragraph>'
  131. );
  132. } );
  133. it( 'does nothing if pasted more than single node', () => {
  134. setData( editor.model, '<paragraph>[]</paragraph>' );
  135. pasteHtml( editor,
  136. 'https://www.youtube.com/watch?v=H08tGjXNHO4 ' +
  137. '<a href="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4</a>'
  138. );
  139. expect( getData( editor.model ) ).to.equal(
  140. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4 ' +
  141. '<$text linkHref="https://www.youtube.com/watch?v=H08tGjXNHO4">https://www.youtube.com/watch?v=H08tGjXNHO4[]</$text>' +
  142. '</paragraph>'
  143. );
  144. } );
  145. it( 'does nothing if a URL is invalid (space inside URL)', () => {
  146. setData( editor.model, '<paragraph>[]</paragraph>' );
  147. pasteHtml( editor, 'youtube.com/watch?v=H08tGjXNHO4&amp;param=foo bar' );
  148. expect( getData( editor.model ) ).to.equal(
  149. '<paragraph>youtube.com/watch?v=H08tGjXNHO4&param=foo bar[]</paragraph>'
  150. );
  151. } );
  152. it( 'does nothing if URL match to media but it was removed', () => {
  153. return ClassicTestEditor
  154. .create( editorElement, {
  155. plugins: [ MediaEmbed, Paragraph ],
  156. mediaEmbed: {
  157. removeProviders: [ 'youtube' ]
  158. }
  159. } )
  160. .then( newEditor => {
  161. editor = newEditor;
  162. setData( editor.model, '<paragraph>[]</paragraph>' );
  163. pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );
  164. expect( getData( editor.model ) ).to.equal(
  165. '<paragraph>https://www.youtube.com/watch?v=H08tGjXNHO4[]</paragraph>'
  166. );
  167. editorElement.remove();
  168. return editor.destroy();
  169. } );
  170. } );
  171. function pasteHtml( editor, html ) {
  172. editor.editing.view.document.fire( 'paste', {
  173. dataTransfer: createDataTransfer( { 'text/html': html } ),
  174. preventDefault() {
  175. }
  176. } );
  177. }
  178. function createDataTransfer( data ) {
  179. return {
  180. getData( type ) {
  181. return data[ type ];
  182. }
  183. };
  184. }
  185. } );
  186. } );