8
0

automediaembed.js 11 KB

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