mediaembedediting.js 22 KB

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