8
0

mediaembedediting.js 31 KB

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