8
0

mediaembedediting.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import log from '@ckeditor/ckeditor5-utils/src/log';
  12. describe( 'MediaEmbedEditing', () => {
  13. let editor, model, doc, view;
  14. testUtils.createSinonSandbox();
  15. const testProviders = {
  16. A: {
  17. name: 'A',
  18. url: /^foo\.com\/(\w+)/,
  19. html: id => `A, id=${ id }`
  20. },
  21. B: {
  22. name: 'B',
  23. url: /^bar\.com\/(\w+)/,
  24. html: id => `B, id=${ id }`
  25. },
  26. C: {
  27. name: 'C',
  28. url: /^\w+\.com\/(\w+)/,
  29. html: id => `C, id=${ id }`
  30. },
  31. extraA: {
  32. name: 'extraA',
  33. url: /^foo\.com\/(\w+)/,
  34. html: id => `extraA, id=${ id }`
  35. },
  36. extraB: {
  37. name: 'extraB',
  38. url: /^\w+\.com\/(\w+)/,
  39. html: id => `extraB, id=${ id }`
  40. },
  41. previewLess: {
  42. name: 'preview-less',
  43. url: /^https:\/\/preview-less/
  44. },
  45. allowEverything: {
  46. name: 'allow-everything',
  47. url: /(.*)/,
  48. html: id => `allow-everything, id=${ id }`
  49. }
  50. };
  51. describe( 'constructor()', () => {
  52. describe( 'configuration', () => {
  53. describe( '#providers', () => {
  54. it( 'should warn when provider has no name', () => {
  55. const logSpy = testUtils.sinon.stub( log, 'warn' );
  56. const provider = {
  57. url: /.*/
  58. };
  59. return createTestEditor( {
  60. providers: [ provider ]
  61. } ).then( () => {
  62. expect( logSpy.calledOnce ).to.equal( true );
  63. expect( logSpy.firstCall.args[ 0 ] ).to.match( /^media-embed-no-provider-name:/ );
  64. expect( logSpy.firstCall.args[ 1 ].provider ).to.equal( provider );
  65. } );
  66. } );
  67. it( 'can override all providers', () => {
  68. return createTestEditor( {
  69. providers: []
  70. } ).then( editor => {
  71. editor.setData( '<figure class="media"><div data-oembed-url="foo.com"></div></figure>' );
  72. expect( getViewData( editor.editing.view, { withoutSelection: true, renderUIElements: true } ) ).to.equal( '' );
  73. } );
  74. } );
  75. it( 'upcast media according to the order', () => {
  76. return createTestEditor( {
  77. providers: [
  78. testProviders.A,
  79. testProviders.B,
  80. testProviders.C,
  81. ]
  82. } ).then( editor => {
  83. editor.setData( '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' );
  84. expect( getViewData( editor.editing.view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  85. '<figure class="ck-widget media" contenteditable="false">' +
  86. '<div class="ck-media__wrapper" data-oembed-url="https://foo.com/123">' +
  87. 'A, id=123' +
  88. '</div>' +
  89. '</figure>'
  90. );
  91. editor.setData( '<figure class="media"><div data-oembed-url="bar.com/123"></div></figure>' );
  92. expect( getViewData( editor.editing.view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  93. '<figure class="ck-widget media" contenteditable="false">' +
  94. '<div class="ck-media__wrapper" data-oembed-url="https://bar.com/123">' +
  95. 'B, id=123' +
  96. '</div>' +
  97. '</figure>'
  98. );
  99. editor.setData( '<figure class="media"><div data-oembed-url="anything.com/123"></div></figure>' );
  100. expect( getViewData( editor.editing.view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  101. '<figure class="ck-widget media" contenteditable="false">' +
  102. '<div class="ck-media__wrapper" data-oembed-url="https://anything.com/123">' +
  103. 'C, id=123' +
  104. '</div>' +
  105. '</figure>'
  106. );
  107. } );
  108. } );
  109. describe( 'default value', () => {
  110. beforeEach( () => {
  111. return createTestEditor( {
  112. semanticDataOutput: true
  113. } )
  114. .then( newEditor => {
  115. editor = newEditor;
  116. view = editor.editing.view;
  117. } );
  118. } );
  119. describe( 'with preview', () => {
  120. it( 'upcasts the URL (dailymotion)', () => {
  121. testMediaUpcast( [
  122. 'https://www.dailymotion.com/video/foo',
  123. 'www.dailymotion.com/video/foo',
  124. 'dailymotion.com/video/foo',
  125. ],
  126. '<div style="position: relative; padding-bottom: 100%; height: 0; ">' +
  127. '<iframe src="https://www.dailymotion.com/embed/video/foo" ' +
  128. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  129. 'frameborder="0" width="480" height="270" allowfullscreen="" allow="autoplay">' +
  130. '</iframe>' +
  131. '</div>' );
  132. } );
  133. describe( 'spotify', () => {
  134. it( 'upcasts the URL (artist)', () => {
  135. testMediaUpcast( [
  136. 'https://www.open.spotify.com/artist/foo',
  137. 'www.open.spotify.com/artist/foo',
  138. 'open.spotify.com/artist/foo',
  139. ],
  140. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' +
  141. '<iframe src="https://open.spotify.com/embed/artist/foo" ' +
  142. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  143. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  144. '</iframe>' +
  145. '</div>' );
  146. } );
  147. it( 'upcasts the URL (album)', () => {
  148. testMediaUpcast( [
  149. 'https://www.open.spotify.com/album/foo',
  150. 'www.open.spotify.com/album/foo',
  151. 'open.spotify.com/album/foo',
  152. ],
  153. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' +
  154. '<iframe src="https://open.spotify.com/embed/album/foo" ' +
  155. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  156. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  157. '</iframe>' +
  158. '</div>' );
  159. } );
  160. it( 'upcasts the URL (track)', () => {
  161. testMediaUpcast( [
  162. 'https://www.open.spotify.com/track/foo',
  163. 'www.open.spotify.com/track/foo',
  164. 'open.spotify.com/track/foo',
  165. ],
  166. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' +
  167. '<iframe src="https://open.spotify.com/embed/track/foo" ' +
  168. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  169. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  170. '</iframe>' +
  171. '</div>' );
  172. } );
  173. } );
  174. it( 'upcasts the URL (youtube)', () => {
  175. testMediaUpcast( [
  176. 'https://www.youtube.com/watch?v=foo',
  177. 'www.youtube.com/watch?v=foo',
  178. 'youtube.com/watch?v=foo',
  179. 'https://www.youtube.com/v/foo',
  180. 'www.youtube.com/v/foo',
  181. 'youtube.com/v/foo',
  182. 'https://www.youtube.com/embed/foo',
  183. 'www.youtube.com/embed/foo',
  184. 'youtube.com/embed/foo',
  185. 'https://youtu.be/foo',
  186. 'youtu.be/foo'
  187. ],
  188. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 56.2493%;">' +
  189. '<iframe src="https://www.youtube.com/embed/foo" ' +
  190. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  191. 'frameborder="0" allow="autoplay; encrypted-media" allowfullscreen="">' +
  192. '</iframe>' +
  193. '</div>' );
  194. } );
  195. it( 'upcasts the URL (vimeo)', () => {
  196. testMediaUpcast( [
  197. 'https://www.vimeo.com/1234',
  198. 'www.vimeo.com/1234',
  199. 'vimeo.com/1234',
  200. 'https://www.vimeo.com/foo/foo/video/1234',
  201. 'www.vimeo.com/foo/foo/video/1234',
  202. 'vimeo.com/foo/foo/video/1234',
  203. 'https://www.vimeo.com/album/foo/video/1234',
  204. 'www.vimeo.com/album/foo/video/1234',
  205. 'vimeo.com/album/foo/video/1234',
  206. 'https://www.vimeo.com/channels/foo/1234',
  207. 'www.vimeo.com/channels/foo/1234',
  208. 'vimeo.com/channels/foo/1234',
  209. 'https://www.vimeo.com/groups/foo/videos/1234',
  210. 'www.vimeo.com/groups/foo/videos/1234',
  211. 'vimeo.com/groups/foo/videos/1234',
  212. 'https://www.vimeo.com/ondemand/foo/1234',
  213. 'www.vimeo.com/ondemand/foo/1234',
  214. 'vimeo.com/ondemand/foo/1234',
  215. 'https://www.player.vimeo.com/video/1234',
  216. 'www.player.vimeo.com/video/1234',
  217. 'player.vimeo.com/video/1234'
  218. ],
  219. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 56.2493%;">' +
  220. '<iframe src="https://player.vimeo.com/video/1234" ' +
  221. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  222. 'frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="">' +
  223. '</iframe>' +
  224. '</div>' );
  225. } );
  226. } );
  227. describe( 'preview-less', () => {
  228. it( 'upcasts the URL (instagram)', () => {
  229. testMediaUpcast( [
  230. 'https://www.instagram.com/p/foo',
  231. 'www.instagram.com/p/foo',
  232. 'instagram.com/p/foo',
  233. ] );
  234. } );
  235. it( 'upcasts the URL (twitter)', () => {
  236. testMediaUpcast( [
  237. 'https://www.twitter.com/foo/bar',
  238. 'www.twitter.com/foo/bar',
  239. 'twitter.com/foo/bar',
  240. ] );
  241. } );
  242. it( 'upcasts the URL (google maps)', () => {
  243. testMediaUpcast( [
  244. 'https://www.google.com/maps/foo',
  245. 'www.google.com/maps/foo',
  246. 'google.com/maps/foo',
  247. ] );
  248. } );
  249. it( 'upcasts the URL (flickr)', () => {
  250. testMediaUpcast( [
  251. 'https://www.flickr.com/foo/bar',
  252. 'www.flickr.com/foo/bar',
  253. 'flickr.com/foo/bar',
  254. ] );
  255. } );
  256. it( 'upcasts the URL (facebook)', () => {
  257. testMediaUpcast( [
  258. 'https://www.facebook.com/foo/bar',
  259. 'www.facebook.com/foo/bar',
  260. 'facebook.com/foo/bar',
  261. ] );
  262. } );
  263. } );
  264. } );
  265. } );
  266. describe( '#extraProviders', () => {
  267. it( 'should warn when provider has no name', () => {
  268. const logSpy = testUtils.sinon.stub( log, 'warn' );
  269. const provider = {
  270. url: /.*/
  271. };
  272. return createTestEditor( {
  273. extraProviders: [ provider ]
  274. } ).then( () => {
  275. expect( logSpy.calledOnce ).to.equal( true );
  276. expect( logSpy.firstCall.args[ 0 ] ).to.match( /^media-embed-no-provider-name:/ );
  277. expect( logSpy.firstCall.args[ 1 ].provider ).to.equal( provider );
  278. } );
  279. } );
  280. it( 'extend #providers but with the lower priority', () => {
  281. return createTestEditor( {
  282. providers: [
  283. testProviders.A,
  284. ],
  285. extraProviders: [
  286. testProviders.extraA,
  287. testProviders.extraB,
  288. ]
  289. } ).then( editor => {
  290. editor.setData( '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' );
  291. expect( getViewData( editor.editing.view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  292. '<figure class="ck-widget media" contenteditable="false">' +
  293. '<div class="ck-media__wrapper" data-oembed-url="https://foo.com/123">' +
  294. 'A, id=123' +
  295. '</div>' +
  296. '</figure>'
  297. );
  298. editor.setData( '<figure class="media"><div data-oembed-url="anything.com/123"></div></figure>' );
  299. expect( getViewData( editor.editing.view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  300. '<figure class="ck-widget media" contenteditable="false">' +
  301. '<div class="ck-media__wrapper" data-oembed-url="https://anything.com/123">' +
  302. 'extraB, id=123' +
  303. '</div>' +
  304. '</figure>'
  305. );
  306. } );
  307. } );
  308. } );
  309. describe( '#removeProviders', () => {
  310. it( 'removes #providers', () => {
  311. return createTestEditor( {
  312. providers: [
  313. testProviders.A,
  314. testProviders.B
  315. ],
  316. removeProviders: [ 'A' ]
  317. } ).then( editor => {
  318. editor.setData(
  319. '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' +
  320. '<figure class="media"><div data-oembed-url="bar.com/123"></div></figure>' );
  321. expect( getViewData( editor.editing.view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  322. '<figure class="ck-widget media" contenteditable="false">' +
  323. '<div class="ck-media__wrapper" data-oembed-url="https://bar.com/123">' +
  324. 'B, id=123' +
  325. '</div>' +
  326. '</figure>'
  327. );
  328. } );
  329. } );
  330. it( 'removes #extraProviders', () => {
  331. return createTestEditor( {
  332. providers: [],
  333. extraProviders: [
  334. testProviders.A,
  335. testProviders.B,
  336. ],
  337. removeProviders: [ 'A' ]
  338. } ).then( editor => {
  339. editor.setData(
  340. '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' +
  341. '<figure class="media"><div data-oembed-url="bar.com/123"></div></figure>' );
  342. expect( getViewData( editor.editing.view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  343. '<figure class="ck-widget media" contenteditable="false">' +
  344. '<div class="ck-media__wrapper" data-oembed-url="https://bar.com/123">' +
  345. 'B, id=123' +
  346. '</div>' +
  347. '</figure>'
  348. );
  349. } );
  350. } );
  351. it( 'removes even when the name of the provider repeats', () => {
  352. return createTestEditor( {
  353. providers: [
  354. testProviders.A,
  355. testProviders.A
  356. ],
  357. extraProviders: [
  358. testProviders.A,
  359. testProviders.A,
  360. testProviders.B
  361. ],
  362. removeProviders: [ 'A' ]
  363. } ).then( editor => {
  364. editor.setData(
  365. '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' +
  366. '<figure class="media"><div data-oembed-url="bar.com/123"></div></figure>' );
  367. expect( getViewData( editor.editing.view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  368. '<figure class="ck-widget media" contenteditable="false">' +
  369. '<div class="ck-media__wrapper" data-oembed-url="https://bar.com/123">' +
  370. 'B, id=123' +
  371. '</div>' +
  372. '</figure>'
  373. );
  374. } );
  375. } );
  376. } );
  377. } );
  378. } );
  379. describe( 'init()', () => {
  380. const providerDefinitions = [
  381. testProviders.previewLess,
  382. testProviders.allowEverything
  383. ];
  384. it( 'should be loaded', () => {
  385. return createTestEditor()
  386. .then( newEditor => {
  387. expect( newEditor.plugins.get( MediaEmbedEditing ) ).to.be.instanceOf( MediaEmbedEditing );
  388. } );
  389. } );
  390. it( 'should set proper schema rules', () => {
  391. return createTestEditor()
  392. .then( newEditor => {
  393. model = newEditor.model;
  394. expect( model.schema.checkChild( [ '$root' ], 'media' ) ).to.be.true;
  395. expect( model.schema.checkAttribute( [ '$root', 'media' ], 'url' ) ).to.be.true;
  396. expect( model.schema.isObject( 'media' ) ).to.be.true;
  397. expect( model.schema.checkChild( [ '$root', 'media' ], 'media' ) ).to.be.false;
  398. expect( model.schema.checkChild( [ '$root', 'media' ], '$text' ) ).to.be.false;
  399. expect( model.schema.checkChild( [ '$root', '$block' ], 'image' ) ).to.be.false;
  400. } );
  401. } );
  402. describe( 'conversion in the data pipeline', () => {
  403. describe( 'semanticDataOutput=true', () => {
  404. beforeEach( () => {
  405. return createTestEditor( {
  406. semanticDataOutput: true,
  407. providers: providerDefinitions
  408. } )
  409. .then( newEditor => {
  410. editor = newEditor;
  411. model = editor.model;
  412. doc = model.document;
  413. view = editor.editing.view;
  414. } );
  415. } );
  416. describe( 'model to view', () => {
  417. it( 'should convert', () => {
  418. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  419. expect( editor.getData() ).to.equal(
  420. '<figure class="media">' +
  421. '<oembed url="https://ckeditor.com"></oembed>' +
  422. '</figure>' );
  423. } );
  424. it( 'should convert (no url)', () => {
  425. setModelData( model, '<media></media>' );
  426. expect( editor.getData() ).to.equal(
  427. '<figure class="media">' +
  428. '<oembed></oembed>' +
  429. '</figure>' );
  430. } );
  431. it( 'should convert (preview-less media)', () => {
  432. setModelData( model, '<media url="https://preview-less"></media>' );
  433. expect( editor.getData() ).to.equal(
  434. '<figure class="media">' +
  435. '<oembed url="https://preview-less"></oembed>' +
  436. '</figure>' );
  437. } );
  438. } );
  439. describe( 'view to model', () => {
  440. it( 'should convert media figure', () => {
  441. editor.setData( '<figure class="media"><oembed url="https://ckeditor.com"></oembed></figure>' );
  442. expect( getModelData( model, { withoutSelection: true } ) )
  443. .to.equal( '<media url="https://ckeditor.com"></media>' );
  444. } );
  445. it( 'should not convert if there is no media class', () => {
  446. editor.setData( '<figure class="quote">My quote</figure>' );
  447. expect( getModelData( model, { withoutSelection: true } ) )
  448. .to.equal( '' );
  449. } );
  450. it( 'should not convert if there is no oembed wrapper inside #1', () => {
  451. editor.setData( '<figure class="media"></figure>' );
  452. expect( getModelData( model, { withoutSelection: true } ) )
  453. .to.equal( '' );
  454. } );
  455. it( 'should not convert if there is no oembed wrapper inside #2', () => {
  456. editor.setData( '<figure class="media">test</figure>' );
  457. expect( getModelData( model, { withoutSelection: true } ) )
  458. .to.equal( '' );
  459. } );
  460. it( 'should not convert when the wrapper has no data-oembed-url attribute', () => {
  461. editor.setData( '<figure class="media"><div></div></figure>' );
  462. expect( getModelData( model, { withoutSelection: true } ) )
  463. .to.equal( '' );
  464. } );
  465. it( 'should not convert in the wrong context', () => {
  466. model.schema.register( 'blockquote', { inheritAllFrom: '$block' } );
  467. model.schema.addChildCheck( ( ctx, childDef ) => {
  468. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  469. return false;
  470. }
  471. } );
  472. editor.conversion.elementToElement( { model: 'blockquote', view: 'blockquote' } );
  473. editor.setData(
  474. '<blockquote><figure class="media"><oembed url="https://ckeditor.com"></oembed></figure></blockquote>' );
  475. expect( getModelData( model, { withoutSelection: true } ) )
  476. .to.equal( '<blockquote></blockquote>' );
  477. } );
  478. it( 'should not convert if the oembed wrapper is already consumed', () => {
  479. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  480. const img = data.viewItem.getChild( 0 );
  481. conversionApi.consumable.consume( img, { name: true } );
  482. }, { priority: 'high' } );
  483. editor.setData( '<figure class="media"><oembed url="https://ckeditor.com"></oembed></figure>' );
  484. expect( getModelData( model, { withoutSelection: true } ) )
  485. .to.equal( '' );
  486. } );
  487. it( 'should not convert if the figure is already consumed', () => {
  488. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  489. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  490. }, { priority: 'high' } );
  491. editor.setData( '<figure class="media"><oembed url="https://ckeditor.com"></oembed></figure>' );
  492. expect( getModelData( model, { withoutSelection: true } ) )
  493. .to.equal( '' );
  494. } );
  495. it( 'should discard the contents of the media', () => {
  496. editor.setData( '<figure class="media"><oembed url="https://ckeditor.com">foo bar</oembed></figure>' );
  497. expect( getModelData( model, { withoutSelection: true } ) )
  498. .to.equal( '<media url="https://ckeditor.com"></media>' );
  499. } );
  500. it( 'should not convert unknown media', () => {
  501. return createTestEditor( {
  502. semanticDataOutput: true,
  503. providers: [
  504. testProviders.A
  505. ]
  506. } )
  507. .then( newEditor => {
  508. newEditor.setData(
  509. '<figure class="media"><oembed url="unknown.media"></oembed></figure>' +
  510. '<figure class="media"><oembed url="foo.com/123"></oembed></figure>' );
  511. expect( getModelData( newEditor.model, { withoutSelection: true } ) )
  512. .to.equal( '<media url="foo.com/123"></media>' );
  513. return newEditor.destroy();
  514. } );
  515. } );
  516. } );
  517. } );
  518. describe( 'semanticDataOutput=false', () => {
  519. beforeEach( () => {
  520. return createTestEditor( {
  521. providers: providerDefinitions
  522. } )
  523. .then( newEditor => {
  524. editor = newEditor;
  525. model = editor.model;
  526. doc = model.document;
  527. view = editor.editing.view;
  528. } );
  529. } );
  530. describe( 'conversion in the data pipeline', () => {
  531. describe( 'model to view', () => {
  532. it( 'should convert', () => {
  533. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  534. expect( editor.getData() ).to.equal(
  535. '<figure class="media">' +
  536. '<div data-oembed-url="https://ckeditor.com">' +
  537. 'allow-everything, id=https://ckeditor.com' +
  538. '</div>' +
  539. '</figure>' );
  540. } );
  541. it( 'should convert (no url)', () => {
  542. setModelData( model, '<media></media>' );
  543. expect( editor.getData() ).to.equal(
  544. '<figure class="media">' +
  545. '<oembed>' +
  546. '</oembed>' +
  547. '</figure>' );
  548. } );
  549. it( 'should convert (preview-less media)', () => {
  550. setModelData( model, '<media url="https://preview-less"></media>' );
  551. expect( editor.getData() ).to.equal(
  552. '<figure class="media">' +
  553. '<oembed url="https://preview-less"></oembed>' +
  554. '</figure>' );
  555. } );
  556. } );
  557. describe( 'view to model', () => {
  558. it( 'should convert media figure', () => {
  559. editor.setData(
  560. '<figure class="media">' +
  561. '<div data-oembed-url="https://ckeditor.com">' +
  562. 'allow-everything, id=https://cksource.com></iframe>' +
  563. '</div>' +
  564. '</figure>' );
  565. expect( getModelData( model, { withoutSelection: true } ) )
  566. .to.equal( '<media url="https://ckeditor.com"></media>' );
  567. } );
  568. it( 'should not convert if there is no media class', () => {
  569. editor.setData( '<figure class="quote">My quote</figure>' );
  570. expect( getModelData( model, { withoutSelection: true } ) )
  571. .to.equal( '' );
  572. } );
  573. it( 'should not convert if there is no oembed wrapper inside #1', () => {
  574. editor.setData( '<figure class="media"></figure>' );
  575. expect( getModelData( model, { withoutSelection: true } ) )
  576. .to.equal( '' );
  577. } );
  578. it( 'should not convert if there is no oembed wrapper inside #2', () => {
  579. editor.setData( '<figure class="media">test</figure>' );
  580. expect( getModelData( model, { withoutSelection: true } ) )
  581. .to.equal( '' );
  582. } );
  583. it( 'should not convert in the wrong context', () => {
  584. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  585. model.schema.addChildCheck( ( ctx, childDef ) => {
  586. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  587. return false;
  588. }
  589. } );
  590. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  591. editor.setData(
  592. '<div>' +
  593. '<figure class="media">' +
  594. '<div data-oembed-url="https://ckeditor.com">' +
  595. '<iframe src="this should be discarded"></iframe>' +
  596. '</div>' +
  597. '</figure>' +
  598. '</div>' );
  599. expect( getModelData( model, { withoutSelection: true } ) )
  600. .to.equal( '<div></div>' );
  601. } );
  602. it( 'should not convert if the oembed wrapper is already consumed', () => {
  603. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  604. const img = data.viewItem.getChild( 0 );
  605. conversionApi.consumable.consume( img, { name: true } );
  606. }, { priority: 'high' } );
  607. editor.setData(
  608. '<div>' +
  609. '<figure class="media">' +
  610. '<div data-oembed-url="https://ckeditor.com">' +
  611. '<iframe src="this should be discarded"></iframe>' +
  612. '</div>' +
  613. '</figure>' +
  614. '</div>' );
  615. expect( getModelData( model, { withoutSelection: true } ) )
  616. .to.equal( '' );
  617. } );
  618. it( 'should not convert if the figure is already consumed', () => {
  619. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  620. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  621. }, { priority: 'high' } );
  622. editor.setData( '<figure class="media"><div data-oembed-url="https://ckeditor.com"></div></figure>' );
  623. expect( getModelData( model, { withoutSelection: true } ) )
  624. .to.equal( '' );
  625. } );
  626. it( 'should discard the contents of the media', () => {
  627. editor.setData(
  628. '<figure class="media">' +
  629. '<div data-oembed-url="https://ckeditor.com">' +
  630. 'foo bar baz' +
  631. '</div>' +
  632. '</figure>' );
  633. expect( getModelData( model, { withoutSelection: true } ) )
  634. .to.equal( '<media url="https://ckeditor.com"></media>' );
  635. } );
  636. it( 'should not convert unknown media', () => {
  637. return createTestEditor( {
  638. providers: [
  639. testProviders.A
  640. ]
  641. } )
  642. .then( newEditor => {
  643. newEditor.setData(
  644. '<figure class="media">' +
  645. '<div data-oembed-url="foo.com/123"></div>' +
  646. '</figure>' +
  647. '<figure class="media">' +
  648. '<div data-oembed-url="unknown.media/123"></div>' +
  649. '</figure>' );
  650. expect( getModelData( newEditor.model, { withoutSelection: true } ) )
  651. .to.equal( '<media url="foo.com/123"></media>' );
  652. return newEditor.destroy();
  653. } );
  654. } );
  655. } );
  656. } );
  657. } );
  658. } );
  659. describe( 'conversion in the editing pipeline', () => {
  660. describe( 'semanticDataOutput=true', () => {
  661. beforeEach( () => {
  662. return createTestEditor( {
  663. providers: providerDefinitions,
  664. semanticDataOutput: true
  665. } )
  666. .then( newEditor => {
  667. editor = newEditor;
  668. model = editor.model;
  669. doc = model.document;
  670. view = editor.editing.view;
  671. } );
  672. } );
  673. test();
  674. } );
  675. describe( 'semanticDataOutput=false', () => {
  676. beforeEach( () => {
  677. return createTestEditor( {
  678. providers: providerDefinitions
  679. } )
  680. .then( newEditor => {
  681. editor = newEditor;
  682. model = editor.model;
  683. doc = model.document;
  684. view = editor.editing.view;
  685. } );
  686. } );
  687. test();
  688. } );
  689. function test() {
  690. describe( 'model to view', () => {
  691. it( 'should convert', () => {
  692. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  693. expect( getViewData( view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  694. '<figure class="ck-widget media" contenteditable="false">' +
  695. '<div class="ck-media__wrapper" data-oembed-url="https://ckeditor.com">' +
  696. 'allow-everything, id=https://ckeditor.com' +
  697. '</div>' +
  698. '</figure>'
  699. );
  700. } );
  701. it( 'should convert the url attribute change', () => {
  702. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  703. const media = doc.getRoot().getChild( 0 );
  704. model.change( writer => {
  705. writer.setAttribute( 'url', 'https://cksource.com', media );
  706. } );
  707. expect( getViewData( view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  708. '<figure class="ck-widget media" contenteditable="false">' +
  709. '<div class="ck-media__wrapper" data-oembed-url="https://cksource.com">' +
  710. 'allow-everything, id=https://cksource.com' +
  711. '</div>' +
  712. '</figure>'
  713. );
  714. } );
  715. it( 'should convert the url attribute removal', () => {
  716. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  717. const media = doc.getRoot().getChild( 0 );
  718. model.change( writer => {
  719. writer.removeAttribute( 'url', media );
  720. } );
  721. expect( getViewData( view, { withoutSelection: true, renderUIElements: true } ) )
  722. .to.equal(
  723. '<figure class="ck-widget media" contenteditable="false">' +
  724. '<div class="ck-media__wrapper">' +
  725. '</div>' +
  726. '</figure>'
  727. );
  728. } );
  729. it( 'should not convert the url attribute removal if is already consumed', () => {
  730. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  731. const media = doc.getRoot().getChild( 0 );
  732. editor.editing.downcastDispatcher.on( 'attribute:url:media', ( evt, data, conversionApi ) => {
  733. conversionApi.consumable.consume( data.item, 'attribute:url' );
  734. }, { priority: 'high' } );
  735. model.change( writer => {
  736. writer.removeAttribute( 'url', media );
  737. } );
  738. expect( getViewData( view, { withoutSelection: true, renderUIElements: true } ) ).to.equal(
  739. '<figure class="ck-widget media" contenteditable="false">' +
  740. '<div class="ck-media__wrapper" data-oembed-url="https://ckeditor.com">' +
  741. 'allow-everything, id=https://ckeditor.com' +
  742. '</div>' +
  743. '</figure>'
  744. );
  745. } );
  746. } );
  747. }
  748. } );
  749. } );
  750. function testMediaUpcast( urls, expected ) {
  751. for ( const url of urls ) {
  752. editor.setData( `<figure class="media"><div data-oembed-url="${ url }"></div></figure>` );
  753. const viewData = getViewData( view, { withoutSelection: true, renderUIElements: true } );
  754. let expectedRegExp;
  755. const expectedUrl = url.startsWith( 'http' ) ? url : 'https://' + url;
  756. if ( expected ) {
  757. expectedRegExp = new RegExp(
  758. '<figure[^>]+>' +
  759. '<div[^>]+>' +
  760. normalizeHtml( expected ) +
  761. '</div>' +
  762. '</figure>' );
  763. } else {
  764. expectedRegExp = new RegExp(
  765. '<figure[^>]+>' +
  766. '<div[^>]+>' +
  767. '<div class="ck ck-media__placeholder ck-reset_all">' +
  768. '<div class="ck-media__placeholder__icon">.*</div>' +
  769. `<a class="ck-media__placeholder__url" href="${ expectedUrl }" target="new">` +
  770. `<span class="ck-media__placeholder__url__text">${ expectedUrl }</span>` +
  771. '<span class="ck ck-tooltip ck-tooltip_s">' +
  772. '<span class="ck ck-tooltip__text">Open media in new tab</span>' +
  773. '</span>' +
  774. '</a>' +
  775. '</div>' +
  776. '</div>' +
  777. '</figure>' );
  778. }
  779. expect( normalizeHtml( viewData ) ).to.match( expectedRegExp, `assertion for "${ url }"` );
  780. }
  781. }
  782. function createTestEditor( mediaEmbedConfig ) {
  783. return VirtualTestEditor
  784. .create( {
  785. plugins: [ MediaEmbedEditing ],
  786. mediaEmbed: mediaEmbedConfig
  787. } );
  788. }
  789. } );