mediaembedediting.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. describe( 'MediaEmbedEditing', () => {
  12. let editor, model, doc, view;
  13. const testProviders = {
  14. A: {
  15. name: 'A',
  16. url: /^foo\.com\/(\w+)/,
  17. html: match => `A, id=${ match[ 1 ] }`
  18. },
  19. B: {
  20. name: 'B',
  21. url: /^bar\.com\/(\w+)/,
  22. html: match => `B, id=${ match[ 1 ] }`
  23. },
  24. C: {
  25. name: 'C',
  26. url: /^\w+\.com\/(\w+)/,
  27. html: match => `C, id=${ match[ 1 ] }`
  28. },
  29. extraA: {
  30. name: 'extraA',
  31. url: /^foo\.com\/(\w+)/,
  32. html: match => `extraA, id=${ match[ 1 ] }`
  33. },
  34. extraB: {
  35. name: 'extraB',
  36. url: /^\w+\.com\/(\w+)/,
  37. html: match => `extraB, id=${ match[ 1 ] }`
  38. },
  39. previewLess: {
  40. name: 'preview-less',
  41. url: /^https:\/\/preview-less/
  42. },
  43. allowEverything: {
  44. name: 'allow-everything',
  45. url: /(.*)/,
  46. html: match => `allow-everything, id=${ match[ 1 ] }`
  47. }
  48. };
  49. afterEach( () => {
  50. sinon.restore();
  51. } );
  52. it( 'should be named', () => {
  53. expect( MediaEmbedEditing.pluginName ).to.equal( 'MediaEmbedEditing' );
  54. } );
  55. describe( 'constructor()', () => {
  56. describe( 'configuration', () => {
  57. describe( '#providers', () => {
  58. it( 'should warn when provider has no name', () => {
  59. const consoleWarnStub = sinon.stub( console, 'warn' );
  60. const provider = {
  61. url: /.*/
  62. };
  63. return createTestEditor( {
  64. providers: [ provider ]
  65. } ).then( () => {
  66. expect( consoleWarnStub.calledOnce ).to.equal( true );
  67. expect( consoleWarnStub.firstCall.args[ 0 ] ).to.match( /^media-embed-no-provider-name/ );
  68. expect( consoleWarnStub.firstCall.args[ 1 ].provider ).to.deep.equal( provider );
  69. } );
  70. } );
  71. it( 'can override all providers', () => {
  72. return createTestEditor( {
  73. providers: []
  74. } ).then( editor => {
  75. editor.setData( '<figure class="media"><div data-oembed-url="foo.com"></div></figure>' );
  76. expect( getViewData( editor.editing.view, { withoutSelection: true, renderRawElements: true } ) ).to.equal( '' );
  77. } );
  78. } );
  79. it( 'upcast media according to the order', () => {
  80. return createTestEditor( {
  81. providers: [
  82. testProviders.A,
  83. testProviders.B,
  84. testProviders.C
  85. ]
  86. } ).then( editor => {
  87. editor.setData( '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' );
  88. expect( getViewData( editor.editing.view, { withoutSelection: true, renderRawElements: true } ) ).to.equal(
  89. '<figure class="ck-widget media" contenteditable="false">' +
  90. '<div class="ck-media__wrapper" data-oembed-url="https://foo.com/123">' +
  91. 'A, id=123' +
  92. '</div>' +
  93. '</figure>'
  94. );
  95. editor.setData( '<figure class="media"><div data-oembed-url="bar.com/123"></div></figure>' );
  96. expect( getViewData( editor.editing.view, { withoutSelection: true, renderRawElements: true } ) ).to.equal(
  97. '<figure class="ck-widget media" contenteditable="false">' +
  98. '<div class="ck-media__wrapper" data-oembed-url="https://bar.com/123">' +
  99. 'B, id=123' +
  100. '</div>' +
  101. '</figure>'
  102. );
  103. editor.setData( '<figure class="media"><div data-oembed-url="anything.com/123"></div></figure>' );
  104. expect( getViewData( editor.editing.view, { withoutSelection: true, renderRawElements: true } ) ).to.equal(
  105. '<figure class="ck-widget media" contenteditable="false">' +
  106. '<div class="ck-media__wrapper" data-oembed-url="https://anything.com/123">' +
  107. 'C, id=123' +
  108. '</div>' +
  109. '</figure>'
  110. );
  111. } );
  112. } );
  113. describe( 'default value', () => {
  114. beforeEach( () => {
  115. return createTestEditor()
  116. .then( newEditor => {
  117. editor = newEditor;
  118. view = editor.editing.view;
  119. } );
  120. } );
  121. describe( 'with preview', () => {
  122. it( 'upcasts the URL (dailymotion)', () => {
  123. testMediaUpcast( [
  124. 'https://www.dailymotion.com/video/foo',
  125. 'www.dailymotion.com/video/foo',
  126. 'dailymotion.com/video/foo'
  127. ],
  128. '<div style="position: relative; padding-bottom: 100%; height: 0; ">' +
  129. '<iframe src="https://www.dailymotion.com/embed/video/foo" ' +
  130. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  131. 'frameborder="0" width="480" height="270" allowfullscreen="" allow="autoplay">' +
  132. '</iframe>' +
  133. '</div>' );
  134. } );
  135. describe( 'spotify', () => {
  136. it( 'upcasts the URL (artist)', () => {
  137. testMediaUpcast( [
  138. 'https://www.open.spotify.com/artist/foo',
  139. 'www.open.spotify.com/artist/foo',
  140. 'open.spotify.com/artist/foo'
  141. ],
  142. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' +
  143. '<iframe src="https://open.spotify.com/embed/artist/foo" ' +
  144. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  145. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  146. '</iframe>' +
  147. '</div>' );
  148. } );
  149. it( 'upcasts the URL (album)', () => {
  150. testMediaUpcast( [
  151. 'https://www.open.spotify.com/album/foo',
  152. 'www.open.spotify.com/album/foo',
  153. 'open.spotify.com/album/foo'
  154. ],
  155. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' +
  156. '<iframe src="https://open.spotify.com/embed/album/foo" ' +
  157. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  158. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  159. '</iframe>' +
  160. '</div>' );
  161. } );
  162. it( 'upcasts the URL (track)', () => {
  163. testMediaUpcast( [
  164. 'https://www.open.spotify.com/track/foo',
  165. 'www.open.spotify.com/track/foo',
  166. 'open.spotify.com/track/foo'
  167. ],
  168. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' +
  169. '<iframe src="https://open.spotify.com/embed/track/foo" ' +
  170. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  171. 'frameborder="0" allowtransparency="true" allow="encrypted-media">' +
  172. '</iframe>' +
  173. '</div>' );
  174. } );
  175. } );
  176. it( 'upcasts the URL (youtube)', () => {
  177. testMediaUpcast( [
  178. 'https://www.youtube.com/watch?v=foo',
  179. 'www.youtube.com/watch?v=foo',
  180. 'youtube.com/watch?v=foo',
  181. 'https://m.youtube.com/watch?v=foo',
  182. 'm.youtube.com/watch?v=foo',
  183. 'https://www.youtube.com/v/foo',
  184. 'www.youtube.com/v/foo',
  185. 'youtube.com/v/foo',
  186. 'https://m.youtube.com/v/foo',
  187. 'm.youtube.com/v/foo',
  188. 'https://www.youtube.com/embed/foo',
  189. 'www.youtube.com/embed/foo',
  190. 'youtube.com/embed/foo',
  191. 'https://youtu.be/foo',
  192. 'youtu.be/foo'
  193. ],
  194. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 56.2493%;">' +
  195. '<iframe src="https://www.youtube.com/embed/foo" ' +
  196. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  197. 'frameborder="0" allow="autoplay; encrypted-media" allowfullscreen="">' +
  198. '</iframe>' +
  199. '</div>' );
  200. } );
  201. // See: https://github.com/ckeditor/ckeditor5-media-embed/issues/26
  202. it( 'upcasts the URL that contains a dash (youtube)', () => {
  203. testMediaUpcast( [
  204. 'https://www.youtube.com/watch?v=euqbMkM-QQk'
  205. ],
  206. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 56.2493%;">' +
  207. '<iframe src="https://www.youtube.com/embed/euqbMkM-QQk" ' +
  208. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  209. 'frameborder="0" allow="autoplay; encrypted-media" allowfullscreen="">' +
  210. '</iframe>' +
  211. '</div>' );
  212. } );
  213. it( 'upcasts the URL (vimeo)', () => {
  214. testMediaUpcast( [
  215. 'https://www.vimeo.com/1234',
  216. 'www.vimeo.com/1234',
  217. 'vimeo.com/1234',
  218. 'https://www.vimeo.com/foo/foo/video/1234',
  219. 'www.vimeo.com/foo/foo/video/1234',
  220. 'vimeo.com/foo/foo/video/1234',
  221. 'https://www.vimeo.com/album/foo/video/1234',
  222. 'www.vimeo.com/album/foo/video/1234',
  223. 'vimeo.com/album/foo/video/1234',
  224. 'https://www.vimeo.com/channels/foo/1234',
  225. 'www.vimeo.com/channels/foo/1234',
  226. 'vimeo.com/channels/foo/1234',
  227. 'https://www.vimeo.com/groups/foo/videos/1234',
  228. 'www.vimeo.com/groups/foo/videos/1234',
  229. 'vimeo.com/groups/foo/videos/1234',
  230. 'https://www.vimeo.com/ondemand/foo/1234',
  231. 'www.vimeo.com/ondemand/foo/1234',
  232. 'vimeo.com/ondemand/foo/1234',
  233. 'https://www.player.vimeo.com/video/1234',
  234. 'www.player.vimeo.com/video/1234',
  235. 'player.vimeo.com/video/1234'
  236. ],
  237. '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 56.2493%;">' +
  238. '<iframe src="https://player.vimeo.com/video/1234" ' +
  239. 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' +
  240. 'frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="">' +
  241. '</iframe>' +
  242. '</div>' );
  243. } );
  244. } );
  245. describe( 'preview-less', () => {
  246. it( 'upcasts the URL (instagram)', () => {
  247. testMediaUpcast( [
  248. 'https://www.instagram.com/p/foo',
  249. 'www.instagram.com/p/foo',
  250. 'instagram.com/p/foo'
  251. ] );
  252. } );
  253. it( 'upcasts the URL (twitter)', () => {
  254. testMediaUpcast( [
  255. 'https://www.twitter.com/foo/bar',
  256. 'www.twitter.com/foo/bar',
  257. 'twitter.com/foo/bar'
  258. ] );
  259. } );
  260. it( 'upcasts the URL (google maps)', () => {
  261. testMediaUpcast( [
  262. 'https://www.google.com/maps/foo',
  263. 'www.google.com/maps/foo',
  264. 'google.com/maps/foo'
  265. ] );
  266. } );
  267. it( 'upcasts the URL (flickr)', () => {
  268. testMediaUpcast( [
  269. 'https://www.flickr.com/foo/bar',
  270. 'www.flickr.com/foo/bar',
  271. 'flickr.com/foo/bar'
  272. ] );
  273. } );
  274. it( 'upcasts the URL (facebook)', () => {
  275. testMediaUpcast( [
  276. 'https://www.facebook.com/foo/bar',
  277. 'www.facebook.com/foo/bar',
  278. 'facebook.com/foo/bar'
  279. ] );
  280. } );
  281. } );
  282. } );
  283. } );
  284. describe( '#extraProviders', () => {
  285. it( 'should warn when provider has no name', () => {
  286. const consoleWarnStub = sinon.stub( console, 'warn' );
  287. const provider = {
  288. url: /.*/
  289. };
  290. return createTestEditor( {
  291. extraProviders: [ provider ]
  292. } ).then( () => {
  293. expect( consoleWarnStub.calledOnce ).to.equal( true );
  294. expect( consoleWarnStub.firstCall.args[ 0 ] ).to.match( /^media-embed-no-provider-name/ );
  295. expect( consoleWarnStub.firstCall.args[ 1 ].provider ).to.deep.equal( provider );
  296. } );
  297. } );
  298. it( 'extend #providers but with the lower priority', () => {
  299. return createTestEditor( {
  300. providers: [
  301. testProviders.A
  302. ],
  303. extraProviders: [
  304. testProviders.extraA,
  305. testProviders.extraB
  306. ]
  307. } ).then( editor => {
  308. editor.setData( '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' );
  309. expect( getViewData( editor.editing.view, { withoutSelection: true, renderRawElements: true } ) ).to.equal(
  310. '<figure class="ck-widget media" contenteditable="false">' +
  311. '<div class="ck-media__wrapper" data-oembed-url="https://foo.com/123">' +
  312. 'A, id=123' +
  313. '</div>' +
  314. '</figure>'
  315. );
  316. editor.setData( '<figure class="media"><div data-oembed-url="anything.com/123"></div></figure>' );
  317. expect( getViewData( editor.editing.view, { withoutSelection: true, renderRawElements: true } ) ).to.equal(
  318. '<figure class="ck-widget media" contenteditable="false">' +
  319. '<div class="ck-media__wrapper" data-oembed-url="https://anything.com/123">' +
  320. 'extraB, id=123' +
  321. '</div>' +
  322. '</figure>'
  323. );
  324. } );
  325. } );
  326. } );
  327. describe( '#removeProviders', () => {
  328. it( 'removes #providers', () => {
  329. return createTestEditor( {
  330. providers: [
  331. testProviders.A,
  332. testProviders.B
  333. ],
  334. removeProviders: [ 'A' ]
  335. } ).then( editor => {
  336. editor.setData(
  337. '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' +
  338. '<figure class="media"><div data-oembed-url="bar.com/123"></div></figure>' );
  339. expect( getViewData( editor.editing.view, { withoutSelection: true, renderRawElements: true } ) ).to.equal(
  340. '<figure class="ck-widget media" contenteditable="false">' +
  341. '<div class="ck-media__wrapper" data-oembed-url="https://bar.com/123">' +
  342. 'B, id=123' +
  343. '</div>' +
  344. '</figure>'
  345. );
  346. } );
  347. } );
  348. it( 'removes #extraProviders', () => {
  349. return createTestEditor( {
  350. providers: [],
  351. extraProviders: [
  352. testProviders.A,
  353. testProviders.B
  354. ],
  355. removeProviders: [ 'A' ]
  356. } ).then( editor => {
  357. editor.setData(
  358. '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' +
  359. '<figure class="media"><div data-oembed-url="bar.com/123"></div></figure>' );
  360. expect( getViewData( editor.editing.view, { withoutSelection: true, renderRawElements: true } ) ).to.equal(
  361. '<figure class="ck-widget media" contenteditable="false">' +
  362. '<div class="ck-media__wrapper" data-oembed-url="https://bar.com/123">' +
  363. 'B, id=123' +
  364. '</div>' +
  365. '</figure>'
  366. );
  367. } );
  368. } );
  369. it( 'removes even when the name of the provider repeats', () => {
  370. return createTestEditor( {
  371. providers: [
  372. testProviders.A,
  373. testProviders.A
  374. ],
  375. extraProviders: [
  376. testProviders.A,
  377. testProviders.A,
  378. testProviders.B
  379. ],
  380. removeProviders: [ 'A' ]
  381. } ).then( editor => {
  382. editor.setData(
  383. '<figure class="media"><div data-oembed-url="foo.com/123"></div></figure>' +
  384. '<figure class="media"><div data-oembed-url="bar.com/123"></div></figure>' );
  385. expect( getViewData( editor.editing.view, { withoutSelection: true, renderRawElements: true } ) ).to.equal(
  386. '<figure class="ck-widget media" contenteditable="false">' +
  387. '<div class="ck-media__wrapper" data-oembed-url="https://bar.com/123">' +
  388. 'B, id=123' +
  389. '</div>' +
  390. '</figure>'
  391. );
  392. } );
  393. } );
  394. } );
  395. } );
  396. } );
  397. describe( 'init()', () => {
  398. const providerDefinitions = [
  399. testProviders.previewLess,
  400. testProviders.allowEverything
  401. ];
  402. it( 'should be loaded', () => {
  403. return createTestEditor()
  404. .then( newEditor => {
  405. expect( newEditor.plugins.get( MediaEmbedEditing ) ).to.be.instanceOf( MediaEmbedEditing );
  406. } );
  407. } );
  408. it( 'should set proper schema rules', () => {
  409. return createTestEditor()
  410. .then( newEditor => {
  411. model = newEditor.model;
  412. expect( model.schema.checkChild( [ '$root' ], 'media' ) ).to.be.true;
  413. expect( model.schema.checkAttribute( [ '$root', 'media' ], 'url' ) ).to.be.true;
  414. expect( model.schema.isObject( 'media' ) ).to.be.true;
  415. expect( model.schema.checkChild( [ '$root', 'media' ], 'media' ) ).to.be.false;
  416. expect( model.schema.checkChild( [ '$root', 'media' ], '$text' ) ).to.be.false;
  417. expect( model.schema.checkChild( [ '$root', '$block' ], 'image' ) ).to.be.false;
  418. } );
  419. } );
  420. describe( 'conversion in the data pipeline', () => {
  421. describe( 'previewsInData=false', () => {
  422. beforeEach( () => {
  423. return createTestEditor( {
  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. providers: [
  520. testProviders.A
  521. ]
  522. } )
  523. .then( newEditor => {
  524. newEditor.setData(
  525. '<figure class="media"><oembed url="unknown.media"></oembed></figure>' +
  526. '<figure class="media"><oembed url="foo.com/123"></oembed></figure>' );
  527. expect( getModelData( newEditor.model, { withoutSelection: true } ) )
  528. .to.equal( '<media url="foo.com/123"></media>' );
  529. return newEditor.destroy();
  530. } );
  531. } );
  532. } );
  533. } );
  534. describe( 'previewsInData=true', () => {
  535. beforeEach( () => {
  536. return createTestEditor( {
  537. providers: providerDefinitions,
  538. previewsInData: true
  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( 'previewsInData=false', () => {
  678. beforeEach( () => {
  679. return createTestEditor( {
  680. providers: providerDefinitions
  681. } )
  682. .then( newEditor => {
  683. editor = newEditor;
  684. model = editor.model;
  685. doc = model.document;
  686. view = editor.editing.view;
  687. } );
  688. } );
  689. test();
  690. } );
  691. describe( 'previewsInData=true', () => {
  692. beforeEach( () => {
  693. return createTestEditor( {
  694. providers: providerDefinitions,
  695. previewsInData: true
  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, renderRawElements: 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, renderRawElements: 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, renderRawElements: 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, renderRawElements: 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. // Related to https://github.com/ckeditor/ckeditor5/issues/407.
  764. it( 'should not discard internals (e.g. UI) injected by other features when converting the url attribute', () => {
  765. setModelData( model, '<media url="https://ckeditor.com"></media>' );
  766. const media = doc.getRoot().getChild( 0 );
  767. editor.editing.view.change( writer => {
  768. const widgetViewElement = editor.editing.mapper.toViewElement( media );
  769. const externalUIElement = writer.createUIElement( 'div', null, function( domDocument ) {
  770. const domElement = this.toDomElement( domDocument );
  771. domElement.innerHTML = 'external UI';
  772. return domElement;
  773. } );
  774. writer.insert( writer.createPositionAt( widgetViewElement, 'end' ), externalUIElement );
  775. } );
  776. expect( getViewData( view, { withoutSelection: true, renderUIElements: true, renderRawElements: true } ) ).to.equal(
  777. '<figure class="ck-widget media" contenteditable="false">' +
  778. '<div class="ck-media__wrapper" data-oembed-url="https://ckeditor.com">' +
  779. 'allow-everything, id=https://ckeditor.com' +
  780. '</div>' +
  781. '<div>external UI</div>' +
  782. '</figure>'
  783. );
  784. model.change( writer => {
  785. writer.setAttribute( 'url', 'https://cksource.com', media );
  786. } );
  787. expect( getViewData( view, { withoutSelection: true, renderUIElements: true, renderRawElements: true } ) ).to.equal(
  788. '<figure class="ck-widget media" contenteditable="false">' +
  789. '<div class="ck-media__wrapper" data-oembed-url="https://cksource.com">' +
  790. 'allow-everything, id=https://cksource.com' +
  791. '</div>' +
  792. '<div>external UI</div>' +
  793. '</figure>'
  794. );
  795. } );
  796. } );
  797. }
  798. } );
  799. } );
  800. function testMediaUpcast( urls, expected ) {
  801. for ( const url of urls ) {
  802. editor.setData( `<figure class="media"><div data-oembed-url="${ url }"></div></figure>` );
  803. const viewData = getViewData( view, { withoutSelection: true, renderRawElements: true } );
  804. let expectedRegExp;
  805. const expectedUrl = url.match( /^https?:\/\// ) ? url : 'https://' + url;
  806. if ( expected ) {
  807. expectedRegExp = new RegExp(
  808. '<figure[^>]+>' +
  809. '<div[^>]+>' +
  810. normalizeHtml( expected ) +
  811. '</div>' +
  812. '</figure>' );
  813. } else {
  814. expectedRegExp = new RegExp(
  815. '<figure[^>]+>' +
  816. '<div[^>]+>' +
  817. '<div class="ck ck-media__placeholder ck-reset_all">' +
  818. '<div class="ck-media__placeholder__icon">.*</div>' +
  819. `<a class="ck-media__placeholder__url" href="${ expectedUrl }" rel="noopener noreferrer" target="_blank">` +
  820. `<span class="ck-media__placeholder__url__text">${ expectedUrl }</span>` +
  821. '<span class="ck ck-tooltip ck-tooltip_s">' +
  822. '<span class="ck ck-tooltip__text">Open media in new tab</span>' +
  823. '</span>' +
  824. '</a>' +
  825. '</div>' +
  826. '</div>' +
  827. '</figure>' );
  828. }
  829. expect( normalizeHtml( viewData ) ).to.match( expectedRegExp, `assertion for "${ url }"` );
  830. }
  831. }
  832. function createTestEditor( mediaEmbedConfig ) {
  833. return VirtualTestEditor
  834. .create( {
  835. plugins: [ MediaEmbedEditing ],
  836. mediaEmbed: mediaEmbedConfig
  837. } );
  838. }
  839. } );