8
0

mediaembedediting.js 31 KB

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