8
0

mediaembedediting.js 31 KB

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