mediaembedediting.js 31 KB

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