8
0

mediaembedediting.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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( 'MediaEmbedEditing', () => {
  12. let editor, model, doc, view;
  13. const mediaDefinitions = [
  14. /^https:\/\/generic/,
  15. {
  16. url: /(.*)/,
  17. html: id => `<iframe src="${ id }"></iframe>`
  18. }
  19. ];
  20. testUtils.createSinonSandbox();
  21. describe( 'constructor()', () => {
  22. describe( 'config.mediaEmbed.media', () => {
  23. beforeEach( () => {
  24. return VirtualTestEditor
  25. .create( {
  26. plugins: [ MediaEmbedEditing ],
  27. mediaEmbed: {
  28. semanticDataOutput: true
  29. }
  30. } )
  31. .then( newEditor => {
  32. editor = newEditor;
  33. view = editor.editing.view;
  34. } );
  35. } );
  36. describe( 'with preview', () => {
  37. it( 'upcasts the URL (dailymotion)', () => {
  38. testMediaUpcast( [
  39. 'https://www.dailymotion.com/video/foo',
  40. 'www.dailymotion.com/video/foo',
  41. 'dailymotion.com/video/foo',
  42. ],
  43. '<div style="position: relative; padding-bottom: 100%; height: 0; ">' +
  44. '<iframe src="https://www.dailymotion.com/embed/video/foo" ' +
  45. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  46. 'frameborder="0" width="480" height="270" allowfullscreen="" allow="autoplay">' +
  47. '</iframe>' +
  48. '</div>' );
  49. } );
  50. describe( 'spotify', () => {
  51. it( 'upcasts the URL (artist)', () => {
  52. testMediaUpcast( [
  53. 'https://www.open.spotify.com/artist/foo',
  54. 'www.open.spotify.com/artist/foo',
  55. 'open.spotify.com/artist/foo',
  56. ],
  57. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' +
  58. '<iframe src="https://open.spotify.com/embed/artist/foo" ' +
  59. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  60. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  61. '</iframe>' +
  62. '</div>' );
  63. } );
  64. it( 'upcasts the URL (album)', () => {
  65. testMediaUpcast( [
  66. 'https://www.open.spotify.com/album/foo',
  67. 'www.open.spotify.com/album/foo',
  68. 'open.spotify.com/album/foo',
  69. ],
  70. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' +
  71. '<iframe src="https://open.spotify.com/embed/album/foo" ' +
  72. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  73. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  74. '</iframe>' +
  75. '</div>' );
  76. } );
  77. it( 'upcasts the URL (track)', () => {
  78. testMediaUpcast( [
  79. 'https://www.open.spotify.com/track/foo',
  80. 'www.open.spotify.com/track/foo',
  81. 'open.spotify.com/track/foo',
  82. ],
  83. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' +
  84. '<iframe src="https://open.spotify.com/embed/track/foo" ' +
  85. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  86. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  87. '</iframe>' +
  88. '</div>' );
  89. } );
  90. } );
  91. it( 'upcasts the URL (youtube)', () => {
  92. testMediaUpcast( [
  93. 'https://www.youtube.com/watch?v=foo',
  94. 'www.youtube.com/watch?v=foo',
  95. 'youtube.com/watch?v=foo',
  96. 'https://www.youtube.com/v/foo',
  97. 'www.youtube.com/v/foo',
  98. 'youtube.com/v/foo',
  99. 'https://www.youtube.com/embed/foo',
  100. 'www.youtube.com/embed/foo',
  101. 'youtube.com/embed/foo',
  102. 'https://youtu.be/foo',
  103. 'youtu.be/foo'
  104. ],
  105. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 56.2493%;">' +
  106. '<iframe src="https://www.youtube.com/embed/foo" ' +
  107. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  108. 'frameborder="0" allow="autoplay; encrypted-media" allowfullscreen="">' +
  109. '</iframe>' +
  110. '</div>' );
  111. } );
  112. it( 'upcasts the URL (vimeo)', () => {
  113. testMediaUpcast( [
  114. 'https://www.vimeo.com/1234',
  115. 'www.vimeo.com/1234',
  116. 'vimeo.com/1234',
  117. 'https://www.vimeo.com/foo/foo/video/1234',
  118. 'www.vimeo.com/foo/foo/video/1234',
  119. 'vimeo.com/foo/foo/video/1234',
  120. 'https://www.vimeo.com/album/foo/video/1234',
  121. 'www.vimeo.com/album/foo/video/1234',
  122. 'vimeo.com/album/foo/video/1234',
  123. 'https://www.vimeo.com/channels/foo/1234',
  124. 'www.vimeo.com/channels/foo/1234',
  125. 'vimeo.com/channels/foo/1234',
  126. 'https://www.vimeo.com/groups/foo/videos/1234',
  127. 'www.vimeo.com/groups/foo/videos/1234',
  128. 'vimeo.com/groups/foo/videos/1234',
  129. 'https://www.vimeo.com/ondemand/foo/1234',
  130. 'www.vimeo.com/ondemand/foo/1234',
  131. 'vimeo.com/ondemand/foo/1234',
  132. 'https://www.player.vimeo.com/video/1234',
  133. 'www.player.vimeo.com/video/1234',
  134. 'player.vimeo.com/video/1234'
  135. ],
  136. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 56.2493%;">' +
  137. '<iframe src="https://player.vimeo.com/video/1234" ' +
  138. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  139. 'frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="">' +
  140. '</iframe>' +
  141. '</div>' );
  142. } );
  143. } );
  144. describe( 'preview-less', () => {
  145. it( 'upcasts the URL (instagram)', () => {
  146. testMediaUpcast( [
  147. 'https://www.instagram.com/p/foo',
  148. 'www.instagram.com/p/foo',
  149. 'instagram.com/p/foo',
  150. ] );
  151. } );
  152. it( 'upcasts the URL (twitter)', () => {
  153. testMediaUpcast( [
  154. 'https://www.twitter.com/foo/bar',
  155. 'www.twitter.com/foo/bar',
  156. 'twitter.com/foo/bar',
  157. ] );
  158. } );
  159. it( 'upcasts the URL (google maps)', () => {
  160. testMediaUpcast( [
  161. 'https://www.google.com/maps/foo',
  162. 'www.google.com/maps/foo',
  163. 'google.com/maps/foo',
  164. ] );
  165. } );
  166. it( 'upcasts the URL (flickr)', () => {
  167. testMediaUpcast( [
  168. 'https://www.flickr.com/foo/bar',
  169. 'www.flickr.com/foo/bar',
  170. 'flickr.com/foo/bar',
  171. ] );
  172. } );
  173. it( 'upcasts the URL (facebook)', () => {
  174. testMediaUpcast( [
  175. 'https://www.facebook.com/foo/bar',
  176. 'www.facebook.com/foo/bar',
  177. 'facebook.com/foo/bar',
  178. ] );
  179. } );
  180. } );
  181. } );
  182. } );
  183. describe( 'init()', () => {
  184. it( 'should be loaded', () => {
  185. return VirtualTestEditor
  186. .create( {
  187. plugins: [ MediaEmbedEditing ],
  188. } )
  189. .then( newEditor => {
  190. expect( newEditor.plugins.get( MediaEmbedEditing ) ).to.be.instanceOf( MediaEmbedEditing );
  191. } );
  192. } );
  193. it( 'should set proper schema rules', () => {
  194. return VirtualTestEditor
  195. .create( {
  196. plugins: [ MediaEmbedEditing ],
  197. } )
  198. .then( newEditor => {
  199. model = newEditor.model;
  200. expect( model.schema.checkChild( [ '$root' ], 'media' ) ).to.be.true;
  201. expect( model.schema.checkAttribute( [ '$root', 'media' ], 'url' ) ).to.be.true;
  202. expect( model.schema.isObject( 'media' ) ).to.be.true;
  203. expect( model.schema.checkChild( [ '$root', 'media' ], 'media' ) ).to.be.false;
  204. expect( model.schema.checkChild( [ '$root', 'media' ], '$text' ) ).to.be.false;
  205. expect( model.schema.checkChild( [ '$root', '$block' ], 'image' ) ).to.be.false;
  206. } );
  207. } );
  208. describe( 'conversion in the data pipeline', () => {
  209. describe( 'semanticDataOutput=true', () => {
  210. beforeEach( () => {
  211. return VirtualTestEditor
  212. .create( {
  213. plugins: [ MediaEmbedEditing ],
  214. mediaEmbed: {
  215. semanticDataOutput: true,
  216. media: mediaDefinitions
  217. }
  218. } )
  219. .then( newEditor => {
  220. editor = newEditor;
  221. model = editor.model;
  222. doc = model.document;
  223. view = editor.editing.view;
  224. } );
  225. } );
  226. describe( 'model to view', () => {
  227. it( 'should convert', () => {
  228. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  229. expect( editor.getData() ).to.equal(
  230. '<figure class="media">' +
  231. '<oembed url="https://ckeditor.com"></oembed>' +
  232. '</figure>' );
  233. } );
  234. it( 'should convert (no url)', () => {
  235. setModelData( model, '<media></media>' );
  236. expect( editor.getData() ).to.equal(
  237. '<figure class="media">' +
  238. '<oembed></oembed>' +
  239. '</figure>' );
  240. } );
  241. it( 'should convert (generic media)', () => {
  242. setModelData( model, '<media url="https://generic"></media>' );
  243. expect( editor.getData() ).to.equal(
  244. '<figure class="media">' +
  245. '<oembed url="https://generic"></oembed>' +
  246. '</figure>' );
  247. } );
  248. } );
  249. describe( 'view to model', () => {
  250. it( 'should convert media figure', () => {
  251. editor.setData( '<figure class="media"><oembed url="https://ckeditor.com"></oembed></figure>' );
  252. expect( getModelData( model, { withoutSelection: true } ) )
  253. .to.equal( '<media url="https://ckeditor.com"></media>' );
  254. } );
  255. it( 'should not convert if there is no media class', () => {
  256. editor.setData( '<figure class="quote">My quote</figure>' );
  257. expect( getModelData( model, { withoutSelection: true } ) )
  258. .to.equal( '' );
  259. } );
  260. it( 'should not convert if there is no oembed wrapper inside #1', () => {
  261. editor.setData( '<figure class="media"></figure>' );
  262. expect( getModelData( model, { withoutSelection: true } ) )
  263. .to.equal( '' );
  264. } );
  265. it( 'should not convert if there is no oembed wrapper inside #2', () => {
  266. editor.setData( '<figure class="media">test</figure>' );
  267. expect( getModelData( model, { withoutSelection: true } ) )
  268. .to.equal( '' );
  269. } );
  270. it( 'should not convert when the wrapper has no data-oembed-url attribute', () => {
  271. editor.setData( '<figure class="media"><div></div></figure>' );
  272. expect( getModelData( model, { withoutSelection: true } ) )
  273. .to.equal( '' );
  274. } );
  275. it( 'should not convert in the wrong context', () => {
  276. model.schema.register( 'blockquote', { inheritAllFrom: '$block' } );
  277. model.schema.addChildCheck( ( ctx, childDef ) => {
  278. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  279. return false;
  280. }
  281. } );
  282. editor.conversion.elementToElement( { model: 'blockquote', view: 'blockquote' } );
  283. editor.setData(
  284. '<blockquote><figure class="media"><oembed url="https://ckeditor.com"></oembed></figure></blockquote>' );
  285. expect( getModelData( model, { withoutSelection: true } ) )
  286. .to.equal( '<blockquote></blockquote>' );
  287. } );
  288. it( 'should not convert if the oembed wrapper is already consumed', () => {
  289. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  290. const img = data.viewItem.getChild( 0 );
  291. conversionApi.consumable.consume( img, { name: true } );
  292. }, { priority: 'high' } );
  293. editor.setData( '<figure class="media"><oembed url="https://ckeditor.com"></oembed></figure>' );
  294. expect( getModelData( model, { withoutSelection: true } ) )
  295. .to.equal( '' );
  296. } );
  297. it( 'should not convert if the figure is already consumed', () => {
  298. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  299. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  300. }, { priority: 'high' } );
  301. editor.setData( '<figure class="media"><oembed url="https://ckeditor.com"></oembed></figure>' );
  302. expect( getModelData( model, { withoutSelection: true } ) )
  303. .to.equal( '' );
  304. } );
  305. it( 'should discard the contents of the media', () => {
  306. editor.setData( '<figure class="media"><oembed url="https://ckeditor.com">foo bar</oembed></figure>' );
  307. expect( getModelData( model, { withoutSelection: true } ) )
  308. .to.equal( '<media url="https://ckeditor.com"></media>' );
  309. } );
  310. it( 'should not convert unknown media', () => {
  311. return VirtualTestEditor
  312. .create( {
  313. plugins: [ MediaEmbedEditing ],
  314. mediaEmbed: {
  315. semanticDataOutput: true,
  316. media: [
  317. {
  318. url: /^https:\/\/known\.media/,
  319. html: () => 'known-media'
  320. }
  321. ]
  322. }
  323. } )
  324. .then( newEditor => {
  325. newEditor.setData(
  326. '<figure class="media"><oembed url="https://unknown.media"></oembed></figure>' +
  327. '<figure class="media"><oembed url="https://known.media"></oembed></figure>' );
  328. expect( getModelData( newEditor.model, { withoutSelection: true } ) )
  329. .to.equal( '<media url="https://known.media"></media>' );
  330. return newEditor.destroy();
  331. } );
  332. } );
  333. } );
  334. } );
  335. describe( 'semanticDataOutput=false', () => {
  336. beforeEach( () => {
  337. return VirtualTestEditor
  338. .create( {
  339. plugins: [ MediaEmbedEditing ],
  340. mediaEmbed: {
  341. media: mediaDefinitions
  342. }
  343. } )
  344. .then( newEditor => {
  345. editor = newEditor;
  346. model = editor.model;
  347. doc = model.document;
  348. view = editor.editing.view;
  349. } );
  350. } );
  351. describe( 'conversion in the data pipeline', () => {
  352. describe( 'model to view', () => {
  353. it( 'should convert', () => {
  354. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  355. expect( editor.getData() ).to.equal(
  356. '<figure class="media">' +
  357. '<div data-oembed-url="https://ckeditor.com">' +
  358. '<iframe src="https://ckeditor.com"></iframe>' +
  359. '</div>' +
  360. '</figure>' );
  361. } );
  362. it( 'should convert (no url)', () => {
  363. setModelData( model, '<media></media>' );
  364. expect( editor.getData() ).to.equal(
  365. '<figure class="media">' +
  366. '<oembed>' +
  367. '</oembed>' +
  368. '</figure>' );
  369. } );
  370. it( 'should convert (generic media)', () => {
  371. setModelData( model, '<media url="https://generic"></media>' );
  372. expect( editor.getData() ).to.equal(
  373. '<figure class="media">' +
  374. '<oembed url="https://generic"></oembed>' +
  375. '</figure>' );
  376. } );
  377. } );
  378. describe( 'view to model', () => {
  379. it( 'should convert media figure', () => {
  380. editor.setData(
  381. '<figure class="media">' +
  382. '<div data-oembed-url="https://ckeditor.com">' +
  383. '<iframe src="https://cksource.com"></iframe>' +
  384. '</div>' +
  385. '</figure>' );
  386. expect( getModelData( model, { withoutSelection: true } ) )
  387. .to.equal( '<media url="https://ckeditor.com"></media>' );
  388. } );
  389. it( 'should not convert if there is no media class', () => {
  390. editor.setData( '<figure class="quote">My quote</figure>' );
  391. expect( getModelData( model, { withoutSelection: true } ) )
  392. .to.equal( '' );
  393. } );
  394. it( 'should not convert if there is no oembed wrapper inside #1', () => {
  395. editor.setData( '<figure class="media"></figure>' );
  396. expect( getModelData( model, { withoutSelection: true } ) )
  397. .to.equal( '' );
  398. } );
  399. it( 'should not convert if there is no oembed wrapper inside #2', () => {
  400. editor.setData( '<figure class="media">test</figure>' );
  401. expect( getModelData( model, { withoutSelection: true } ) )
  402. .to.equal( '' );
  403. } );
  404. it( 'should not convert in the wrong context', () => {
  405. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  406. model.schema.addChildCheck( ( ctx, childDef ) => {
  407. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  408. return false;
  409. }
  410. } );
  411. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  412. editor.setData(
  413. '<div>' +
  414. '<figure class="media">' +
  415. '<div data-oembed-url="https://ckeditor.com">' +
  416. '<iframe src="https://cksource.com"></iframe>' +
  417. '</div>' +
  418. '</figure>' +
  419. '</div>' );
  420. expect( getModelData( model, { withoutSelection: true } ) )
  421. .to.equal( '<div></div>' );
  422. } );
  423. it( 'should not convert if the oembed wrapper is already consumed', () => {
  424. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  425. const img = data.viewItem.getChild( 0 );
  426. conversionApi.consumable.consume( img, { name: true } );
  427. }, { priority: 'high' } );
  428. editor.setData(
  429. '<div>' +
  430. '<figure class="media">' +
  431. '<div data-oembed-url="https://ckeditor.com">' +
  432. '<iframe src="https://cksource.com"></iframe>' +
  433. '</div>' +
  434. '</figure>' +
  435. '</div>' );
  436. expect( getModelData( model, { withoutSelection: true } ) )
  437. .to.equal( '' );
  438. } );
  439. it( 'should not convert if the figure is already consumed', () => {
  440. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  441. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  442. }, { priority: 'high' } );
  443. editor.setData( '<figure class="media"><div data-oembed-url="https://ckeditor.com"></div></figure>' );
  444. expect( getModelData( model, { withoutSelection: true } ) )
  445. .to.equal( '' );
  446. } );
  447. it( 'should discard the contents of the media', () => {
  448. editor.setData(
  449. '<figure class="media">' +
  450. '<div data-oembed-url="https://ckeditor.com">' +
  451. 'foo bar baz' +
  452. '</div>' +
  453. '</figure>' );
  454. expect( getModelData( model, { withoutSelection: true } ) )
  455. .to.equal( '<media url="https://ckeditor.com"></media>' );
  456. } );
  457. it( 'should not convert unknown media', () => {
  458. return VirtualTestEditor
  459. .create( {
  460. plugins: [ MediaEmbedEditing ],
  461. mediaEmbed: {
  462. media: [
  463. {
  464. url: /^https:\/\/known\.media/,
  465. html: () => 'known-media'
  466. }
  467. ]
  468. }
  469. } )
  470. .then( newEditor => {
  471. newEditor.setData(
  472. '<figure class="media">' +
  473. '<div data-oembed-url="https://known.media">' +
  474. '</div>' +
  475. '</figure>' +
  476. '<figure class="media">' +
  477. '<div data-oembed-url="https://unknown.media">' +
  478. '</div>' +
  479. '</figure>' );
  480. expect( getModelData( newEditor.model, { withoutSelection: true } ) )
  481. .to.equal( '<media url="https://known.media"></media>' );
  482. return newEditor.destroy();
  483. } );
  484. } );
  485. } );
  486. } );
  487. } );
  488. } );
  489. describe( 'conversion in the editing pipeline', () => {
  490. describe( 'semanticDataOutput=true', () => {
  491. beforeEach( () => {
  492. return VirtualTestEditor
  493. .create( {
  494. plugins: [ MediaEmbedEditing ],
  495. mediaEmbed: {
  496. media: mediaDefinitions,
  497. semanticDataOutput: true
  498. }
  499. } )
  500. .then( newEditor => {
  501. editor = newEditor;
  502. model = editor.model;
  503. doc = model.document;
  504. view = editor.editing.view;
  505. } );
  506. } );
  507. test();
  508. } );
  509. describe( 'semanticDataOutput=false', () => {
  510. beforeEach( () => {
  511. return VirtualTestEditor
  512. .create( {
  513. plugins: [ MediaEmbedEditing ],
  514. mediaEmbed: {
  515. media: mediaDefinitions
  516. }
  517. } )
  518. .then( newEditor => {
  519. editor = newEditor;
  520. model = editor.model;
  521. doc = model.document;
  522. view = editor.editing.view;
  523. } );
  524. } );
  525. test();
  526. } );
  527. function test() {
  528. describe( 'model to view', () => {
  529. it( 'should convert', () => {
  530. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  531. expect( getViewData( view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  532. '<figure class="ck-widget media" contenteditable="false">' +
  533. '<div class="ck-media__wrapper" data-oembed-url="https://ckeditor.com">' +
  534. '<iframe src="https://ckeditor.com"></iframe>' +
  535. '</div>' +
  536. '</figure>'
  537. );
  538. } );
  539. it( 'should convert the url attribute change', () => {
  540. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  541. const media = doc.getRoot().getChild( 0 );
  542. model.change( writer => {
  543. writer.setAttribute( 'url', 'https://cksource.com', media );
  544. } );
  545. expect( getViewData( view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  546. '<figure class="ck-widget media" contenteditable="false">' +
  547. '<div class="ck-media__wrapper" data-oembed-url="https://cksource.com">' +
  548. '<iframe src="https://cksource.com"></iframe>' +
  549. '</div>' +
  550. '</figure>'
  551. );
  552. } );
  553. it( 'should convert the url attribute removal', () => {
  554. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  555. const media = doc.getRoot().getChild( 0 );
  556. model.change( writer => {
  557. writer.removeAttribute( 'url', media );
  558. } );
  559. expect( getViewData( view, { withoutSelection: true, renderUIElements: true } ) )
  560. .to.equal(
  561. '<figure class="ck-widget media" contenteditable="false">' +
  562. '<div class="ck-media__wrapper">' +
  563. '</div>' +
  564. '</figure>'
  565. );
  566. } );
  567. it( 'should not convert the url attribute removal if is already consumed', () => {
  568. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  569. const media = doc.getRoot().getChild( 0 );
  570. editor.editing.downcastDispatcher.on( 'attribute:url:media', ( evt, data, conversionApi ) => {
  571. conversionApi.consumable.consume( data.item, 'attribute:url' );
  572. }, { priority: 'high' } );
  573. model.change( writer => {
  574. writer.removeAttribute( 'url', media );
  575. } );
  576. expect( getViewData( view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  577. '<figure class="ck-widget media" contenteditable="false">' +
  578. '<div class="ck-media__wrapper" data-oembed-url="https://ckeditor.com">' +
  579. '<iframe src="https://ckeditor.com"></iframe>' +
  580. '</div>' +
  581. '</figure>'
  582. );
  583. } );
  584. } );
  585. }
  586. } );
  587. } );
  588. function testMediaUpcast( urls, expected ) {
  589. for ( const url of urls ) {
  590. editor.setData(
  591. `<figure class="media"><div data-oembed-url="${ url }"></div></figure>` );
  592. const viewData = getViewData( view, { withoutSelection: true, renderUIElements: true } );
  593. let expectedRegExp;
  594. if ( expected ) {
  595. expectedRegExp = new RegExp(
  596. '<figure[^>]+>' +
  597. '<div[^>]+>' +
  598. normalizeHtml( expected ) +
  599. '</div>' +
  600. '</figure>' );
  601. } else {
  602. expectedRegExp = new RegExp(
  603. '<figure[^>]+>' +
  604. '<div[^>]+>' +
  605. '<div class="ck-media__placeholder">.*</div>' +
  606. '</div>' +
  607. '</figure>' );
  608. }
  609. expect( normalizeHtml( viewData ) ).to.match( expectedRegExp, `assertion for "${ url }"` );
  610. }
  611. }
  612. } );