mediaembedediting.js 30 KB

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