mediaembedediting.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import MediaEmbedEditing from '../src/mediaembedediting';
  7. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  11. describe.only( 'MediaEmbedEditing', () => {
  12. let editor, model, doc, view;
  13. const mediaDefinitions = {
  14. test: {
  15. url: /(.*)/,
  16. html: id => `<iframe src="${ id }"></iframe>`
  17. }
  18. };
  19. testUtils.createSinonSandbox();
  20. describe( 'constructor()', () => {
  21. describe( 'config.mediaEmbed.media', () => {
  22. beforeEach( () => {
  23. return VirtualTestEditor
  24. .create( {
  25. plugins: [ MediaEmbedEditing ],
  26. mediaEmbed: {
  27. semanticDataOutput: true
  28. }
  29. } )
  30. .then( newEditor => {
  31. editor = newEditor;
  32. view = editor.editing.view;
  33. } );
  34. } );
  35. it( 'upcasts the URL (dailymotion)', () => {
  36. testMediaConfig( [
  37. 'https://www.dailymotion.com/video/foo',
  38. 'www.dailymotion.com/video/foo',
  39. 'dailymotion.com/video/foo',
  40. ],
  41. '<iframe src="https://www.dailymotion.com/embed/video/foo" ' +
  42. 'frameborder="0" width="480" height="270" allowfullscreen="" allow="autoplay">' +
  43. '</iframe>' );
  44. } );
  45. it( 'upcasts the URL (instagram)', () => {
  46. testMediaConfig( [
  47. 'https://www.instagram.com/p/foo',
  48. 'www.instagram.com/p/foo',
  49. 'instagram.com/p/foo',
  50. ],
  51. '<iframe src="http://instagram.com/p/foo/embed" frameborder="0"></iframe>' );
  52. } );
  53. describe( 'spotify', () => {
  54. it( 'upcasts the URL (artist)', () => {
  55. testMediaConfig( [
  56. 'https://www.open.spotify.com/artist/foo',
  57. 'www.open.spotify.com/artist/foo',
  58. 'open.spotify.com/artist/foo',
  59. ],
  60. '<iframe src="https://open.spotify.com/embed/artist/foo" ' +
  61. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  62. '</iframe>' );
  63. } );
  64. it( 'upcasts the URL (album)', () => {
  65. testMediaConfig( [
  66. 'https://www.open.spotify.com/album/foo',
  67. 'www.open.spotify.com/album/foo',
  68. 'open.spotify.com/album/foo',
  69. ],
  70. '<iframe src="https://open.spotify.com/embed/album/foo" ' +
  71. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  72. '</iframe>' );
  73. } );
  74. it( 'upcasts the URL (track)', () => {
  75. testMediaConfig( [
  76. 'https://www.open.spotify.com/track/foo',
  77. 'www.open.spotify.com/track/foo',
  78. 'open.spotify.com/track/foo',
  79. ],
  80. '<iframe src="https://open.spotify.com/embed/track/foo" ' +
  81. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  82. '</iframe>' );
  83. } );
  84. } );
  85. it( 'upcasts the URL (youtube)', () => {
  86. testMediaConfig( [
  87. 'https://www.youtube.com/watch?v=foo',
  88. 'www.youtube.com/watch?v=foo',
  89. 'youtube.com/watch?v=foo',
  90. 'https://www.youtube.com/v/foo',
  91. 'www.youtube.com/v/foo',
  92. 'youtube.com/v/foo',
  93. 'https://www.youtube.com/embed/foo',
  94. 'www.youtube.com/embed/foo',
  95. 'youtube.com/embed/foo',
  96. 'https://youtu.be/foo',
  97. 'youtu.be/foo'
  98. ],
  99. '<iframe src="https://www.youtube.com/embed/foo" ' +
  100. 'frameborder="0" allow="autoplay; encrypted-media" allowfullscreen="">' +
  101. '</iframe>' );
  102. } );
  103. it( 'upcasts the URL (vimeo)', () => {
  104. testMediaConfig( [
  105. 'https://www.vimeo.com/1234',
  106. 'www.vimeo.com/1234',
  107. 'vimeo.com/1234',
  108. 'https://www.vimeo.com/foo/foo/video/1234',
  109. 'www.vimeo.com/foo/foo/video/1234',
  110. 'vimeo.com/foo/foo/video/1234',
  111. 'https://www.vimeo.com/album/foo/video/1234',
  112. 'www.vimeo.com/album/foo/video/1234',
  113. 'vimeo.com/album/foo/video/1234',
  114. 'https://www.vimeo.com/channels/foo/1234',
  115. 'www.vimeo.com/channels/foo/1234',
  116. 'vimeo.com/channels/foo/1234',
  117. 'https://www.vimeo.com/groups/foo/videos/1234',
  118. 'www.vimeo.com/groups/foo/videos/1234',
  119. 'vimeo.com/groups/foo/videos/1234',
  120. 'https://www.vimeo.com/ondemand/foo/1234',
  121. 'www.vimeo.com/ondemand/foo/1234',
  122. 'vimeo.com/ondemand/foo/1234',
  123. 'https://www.player.vimeo.com/video/1234',
  124. 'www.player.vimeo.com/video/1234',
  125. 'player.vimeo.com/video/1234'
  126. ],
  127. '<iframe src="https://player.vimeo.com/video/1234" ' +
  128. 'frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="">' +
  129. '</iframe>' );
  130. } );
  131. } );
  132. } );
  133. describe( 'init()', () => {
  134. it( 'should be loaded', () => {
  135. return VirtualTestEditor
  136. .create( {
  137. plugins: [ MediaEmbedEditing ],
  138. } )
  139. .then( newEditor => {
  140. expect( newEditor.plugins.get( MediaEmbedEditing ) ).to.be.instanceOf( MediaEmbedEditing );
  141. } );
  142. } );
  143. it( 'should set proper schema rules', () => {
  144. return VirtualTestEditor
  145. .create( {
  146. plugins: [ MediaEmbedEditing ],
  147. } )
  148. .then( newEditor => {
  149. model = newEditor.model;
  150. expect( model.schema.checkChild( [ '$root' ], 'media' ) ).to.be.true;
  151. expect( model.schema.checkAttribute( [ '$root', 'media' ], 'url' ) ).to.be.true;
  152. expect( model.schema.isObject( 'media' ) ).to.be.true;
  153. expect( model.schema.checkChild( [ '$root', 'media' ], 'media' ) ).to.be.false;
  154. expect( model.schema.checkChild( [ '$root', 'media' ], '$text' ) ).to.be.false;
  155. expect( model.schema.checkChild( [ '$root', '$block' ], 'image' ) ).to.be.false;
  156. } );
  157. } );
  158. describe( 'conversion in the data pipeline', () => {
  159. describe( 'semanticDataOutput=true', () => {
  160. beforeEach( () => {
  161. return VirtualTestEditor
  162. .create( {
  163. plugins: [ MediaEmbedEditing ],
  164. mediaEmbed: {
  165. semanticDataOutput: true,
  166. media: mediaDefinitions
  167. }
  168. } )
  169. .then( newEditor => {
  170. editor = newEditor;
  171. model = editor.model;
  172. doc = model.document;
  173. view = editor.editing.view;
  174. } );
  175. } );
  176. describe( 'model to view', () => {
  177. it( 'should convert', () => {
  178. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  179. expect( editor.getData() ).to.equal(
  180. '<figure class="media">' +
  181. '<div data-oembed-url="http://ckeditor.com"></div>' +
  182. '</figure>' );
  183. } );
  184. it( 'should convert (no url)', () => {
  185. setModelData( model, '<media></media>' );
  186. expect( editor.getData() ).to.equal(
  187. '<figure class="media">' +
  188. '<div></div>' +
  189. '</figure>' );
  190. } );
  191. } );
  192. describe( 'view to model', () => {
  193. it( 'should convert media figure', () => {
  194. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  195. expect( getModelData( model, { withoutSelection: true } ) )
  196. .to.equal( '<media url="http://ckeditor.com"></media>' );
  197. } );
  198. it( 'should not convert if there is no media class', () => {
  199. editor.setData( '<figure class="quote">My quote</figure>' );
  200. expect( getModelData( model, { withoutSelection: true } ) )
  201. .to.equal( '' );
  202. } );
  203. it( 'should not convert if there is no oembed wrapper inside #1', () => {
  204. editor.setData( '<figure class="media"></figure>' );
  205. expect( getModelData( model, { withoutSelection: true } ) )
  206. .to.equal( '' );
  207. } );
  208. it( 'should not convert if there is no oembed wrapper inside #2', () => {
  209. editor.setData( '<figure class="media">test</figure>' );
  210. expect( getModelData( model, { withoutSelection: true } ) )
  211. .to.equal( '' );
  212. } );
  213. it( 'should not convert when the wrapper has no data-oembed-url attribute', () => {
  214. editor.setData( '<figure class="media"><div></div></figure>' );
  215. expect( getModelData( model, { withoutSelection: true } ) )
  216. .to.equal( '' );
  217. } );
  218. it( 'should not convert in the wrong context', () => {
  219. model.schema.register( 'blockquote', { inheritAllFrom: '$block' } );
  220. model.schema.addChildCheck( ( ctx, childDef ) => {
  221. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  222. return false;
  223. }
  224. } );
  225. editor.conversion.elementToElement( { model: 'blockquote', view: 'blockquote' } );
  226. editor.setData(
  227. '<blockquote><figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure></blockquote>' );
  228. expect( getModelData( model, { withoutSelection: true } ) )
  229. .to.equal( '<blockquote></blockquote>' );
  230. } );
  231. it( 'should not convert if the oembed wrapper is already consumed', () => {
  232. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  233. const img = data.viewItem.getChild( 0 );
  234. conversionApi.consumable.consume( img, { name: true } );
  235. }, { priority: 'high' } );
  236. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  237. expect( getModelData( model, { withoutSelection: true } ) )
  238. .to.equal( '' );
  239. } );
  240. it( 'should not convert if the figure is already consumed', () => {
  241. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  242. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  243. }, { priority: 'high' } );
  244. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  245. expect( getModelData( model, { withoutSelection: true } ) )
  246. .to.equal( '' );
  247. } );
  248. } );
  249. } );
  250. describe( 'semanticDataOutput=false', () => {
  251. beforeEach( () => {
  252. return VirtualTestEditor
  253. .create( {
  254. plugins: [ MediaEmbedEditing ],
  255. mediaEmbed: {
  256. media: mediaDefinitions
  257. }
  258. } )
  259. .then( newEditor => {
  260. editor = newEditor;
  261. model = editor.model;
  262. doc = model.document;
  263. view = editor.editing.view;
  264. } );
  265. } );
  266. describe( 'conversion in the data pipeline', () => {
  267. describe( 'model to view', () => {
  268. it( 'should convert', () => {
  269. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  270. expect( editor.getData() ).to.equal(
  271. '<figure class="media">' +
  272. '<div data-oembed-url="http://ckeditor.com">' +
  273. '<iframe src="http://ckeditor.com"></iframe>' +
  274. '</div>' +
  275. '</figure>' );
  276. } );
  277. it( 'should convert (no url)', () => {
  278. setModelData( model, '<media></media>' );
  279. expect( editor.getData() ).to.equal(
  280. '<figure class="media">' +
  281. '<div>' +
  282. '</div>' +
  283. '</figure>' );
  284. } );
  285. } );
  286. describe( 'view to model', () => {
  287. it( 'should convert media figure', () => {
  288. editor.setData(
  289. '<figure class="media">' +
  290. '<div data-oembed-url="http://ckeditor.com">' +
  291. '<iframe src="http://cksource.com"></iframe>' +
  292. '</div>' +
  293. '</figure>' );
  294. expect( getModelData( model, { withoutSelection: true } ) )
  295. .to.equal( '<media url="http://ckeditor.com"></media>' );
  296. } );
  297. it( 'should not convert if there is no media class', () => {
  298. editor.setData( '<figure class="quote">My quote</figure>' );
  299. expect( getModelData( model, { withoutSelection: true } ) )
  300. .to.equal( '' );
  301. } );
  302. it( 'should not convert if there is no oembed wrapper inside #1', () => {
  303. editor.setData( '<figure class="media"></figure>' );
  304. expect( getModelData( model, { withoutSelection: true } ) )
  305. .to.equal( '' );
  306. } );
  307. it( 'should not convert if there is no oembed wrapper inside #2', () => {
  308. editor.setData( '<figure class="media">test</figure>' );
  309. expect( getModelData( model, { withoutSelection: true } ) )
  310. .to.equal( '' );
  311. } );
  312. it( 'should not convert in the wrong context', () => {
  313. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  314. model.schema.addChildCheck( ( ctx, childDef ) => {
  315. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  316. return false;
  317. }
  318. } );
  319. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  320. editor.setData(
  321. '<div>' +
  322. '<figure class="media">' +
  323. '<div data-oembed-url="http://ckeditor.com">' +
  324. '<iframe src="http://cksource.com"></iframe>' +
  325. '</div>' +
  326. '</figure>' +
  327. '</div>' );
  328. expect( getModelData( model, { withoutSelection: true } ) )
  329. .to.equal( '<div></div>' );
  330. } );
  331. it( 'should not convert if the oembed wrapper is already consumed', () => {
  332. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  333. const img = data.viewItem.getChild( 0 );
  334. conversionApi.consumable.consume( img, { name: true } );
  335. }, { priority: 'high' } );
  336. editor.setData(
  337. '<div>' +
  338. '<figure class="media">' +
  339. '<div data-oembed-url="http://ckeditor.com">' +
  340. '<iframe src="http://cksource.com"></iframe>' +
  341. '</div>' +
  342. '</figure>' +
  343. '</div>' );
  344. expect( getModelData( model, { withoutSelection: true } ) )
  345. .to.equal( '' );
  346. } );
  347. it( 'should not convert if the figure is already consumed', () => {
  348. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  349. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  350. }, { priority: 'high' } );
  351. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  352. expect( getModelData( model, { withoutSelection: true } ) )
  353. .to.equal( '' );
  354. } );
  355. } );
  356. } );
  357. } );
  358. } );
  359. describe( 'conversion in the editing pipeline', () => {
  360. describe( 'semanticDataOutput=true', () => {
  361. beforeEach( () => {
  362. return VirtualTestEditor
  363. .create( {
  364. plugins: [ MediaEmbedEditing ],
  365. mediaEmbed: {
  366. media: mediaDefinitions,
  367. semanticDataOutput: true
  368. }
  369. } )
  370. .then( newEditor => {
  371. editor = newEditor;
  372. model = editor.model;
  373. doc = model.document;
  374. view = editor.editing.view;
  375. } );
  376. } );
  377. test();
  378. } );
  379. describe( 'semanticDataOutput=false', () => {
  380. beforeEach( () => {
  381. return VirtualTestEditor
  382. .create( {
  383. plugins: [ MediaEmbedEditing ],
  384. mediaEmbed: {
  385. media: mediaDefinitions
  386. }
  387. } )
  388. .then( newEditor => {
  389. editor = newEditor;
  390. model = editor.model;
  391. doc = model.document;
  392. view = editor.editing.view;
  393. } );
  394. } );
  395. test();
  396. } );
  397. function test() {
  398. describe( 'model to view', () => {
  399. it( 'should convert', () => {
  400. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  401. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  402. '<figure class="ck-widget media" contenteditable="false">' +
  403. '<div class="ck-media__wrapper" data-oembed-url="http://ckeditor.com">' +
  404. '<iframe src="http://ckeditor.com"></iframe>' +
  405. '</div>' +
  406. '</figure>'
  407. );
  408. } );
  409. it( 'should convert the url attribute change', () => {
  410. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  411. const media = doc.getRoot().getChild( 0 );
  412. model.change( writer => {
  413. writer.setAttribute( 'url', 'http://cksource.com', media );
  414. } );
  415. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  416. '<figure class="ck-widget media" contenteditable="false">' +
  417. '<div class="ck-media__wrapper" data-oembed-url="http://cksource.com">' +
  418. '<iframe src="http://cksource.com"></iframe>' +
  419. '</div>' +
  420. '</figure>'
  421. );
  422. } );
  423. it( 'should convert the url attribute removal', () => {
  424. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  425. const media = doc.getRoot().getChild( 0 );
  426. model.change( writer => {
  427. writer.removeAttribute( 'url', media );
  428. } );
  429. expect( getViewData( view, { withoutSelection: true } ) )
  430. .to.equal(
  431. '<figure class="ck-widget media" contenteditable="false">' +
  432. '<div class="ck-media__wrapper">' +
  433. '</div>' +
  434. '</figure>'
  435. );
  436. } );
  437. it( 'should not convert the url attribute removal if is already consumed', () => {
  438. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  439. const media = doc.getRoot().getChild( 0 );
  440. editor.editing.downcastDispatcher.on( 'attribute:url:media', ( evt, data, conversionApi ) => {
  441. conversionApi.consumable.consume( data.item, 'attribute:url' );
  442. }, { priority: 'high' } );
  443. model.change( writer => {
  444. writer.removeAttribute( 'url', media );
  445. } );
  446. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  447. '<figure class="ck-widget media" contenteditable="false">' +
  448. '<div class="ck-media__wrapper" data-oembed-url="http://ckeditor.com">' +
  449. '<iframe src="http://ckeditor.com"></iframe>' +
  450. '</div>' +
  451. '</figure>'
  452. );
  453. } );
  454. } );
  455. }
  456. } );
  457. } );
  458. function testMediaConfig( urls, expected ) {
  459. for ( const url of urls ) {
  460. editor.setData(
  461. `<figure class="media"><div data-oembed-url="${ url }"></div></figure>` );
  462. expect( normalizeHtml( getViewData( view, { withoutSelection: true } ) ) ).to.match(
  463. new RegExp( '<figure[^>]+>' +
  464. '<div[^>]+>' +
  465. `<div[^>]+>${ normalizeHtml( expected ) }</div>` +
  466. '</div>' +
  467. '</figure>' ), `assertion for "${ url }"` );
  468. }
  469. }
  470. } );