mediaembedediting.js 22 KB

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