mediaembedediting.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. describe( 'MediaEmbedEditing', () => {
  11. let editor, model, doc, view;
  12. const mediaDefinitions = {
  13. test: {
  14. url: /(.*)/,
  15. html: id => `<iframe src="${ id }"></iframe>`
  16. }
  17. };
  18. testUtils.createSinonSandbox();
  19. it( 'should be loaded', () => {
  20. return VirtualTestEditor
  21. .create( {
  22. plugins: [ MediaEmbedEditing ],
  23. } )
  24. .then( newEditor => {
  25. expect( newEditor.plugins.get( MediaEmbedEditing ) ).to.be.instanceOf( MediaEmbedEditing );
  26. } );
  27. } );
  28. it( 'should set proper schema rules', () => {
  29. return VirtualTestEditor
  30. .create( {
  31. plugins: [ MediaEmbedEditing ],
  32. } )
  33. .then( newEditor => {
  34. model = newEditor.model;
  35. expect( model.schema.checkChild( [ '$root' ], 'media' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ '$root', 'media' ], 'url' ) ).to.be.true;
  37. expect( model.schema.isObject( 'media' ) ).to.be.true;
  38. expect( model.schema.checkChild( [ '$root', 'media' ], 'media' ) ).to.be.false;
  39. expect( model.schema.checkChild( [ '$root', 'media' ], '$text' ) ).to.be.false;
  40. expect( model.schema.checkChild( [ '$root', '$block' ], 'image' ) ).to.be.false;
  41. } );
  42. } );
  43. describe( 'conversion in the data pipeline', () => {
  44. describe( 'semanticDataOutput=true', () => {
  45. beforeEach( () => {
  46. return VirtualTestEditor
  47. .create( {
  48. plugins: [ MediaEmbedEditing ],
  49. mediaEmbed: {
  50. semanticDataOutput: true,
  51. media: mediaDefinitions
  52. }
  53. } )
  54. .then( newEditor => {
  55. editor = newEditor;
  56. model = editor.model;
  57. doc = model.document;
  58. view = editor.editing.view;
  59. } );
  60. } );
  61. describe( 'model to view', () => {
  62. it( 'should convert', () => {
  63. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  64. expect( editor.getData() ).to.equal(
  65. '<figure class="media">' +
  66. '<div data-oembed-url="http://ckeditor.com"></div>' +
  67. '</figure>' );
  68. } );
  69. it( 'should convert (no url)', () => {
  70. setModelData( model, '<media></media>' );
  71. expect( editor.getData() ).to.equal(
  72. '<figure class="media">' +
  73. '<div></div>' +
  74. '</figure>' );
  75. } );
  76. } );
  77. describe( 'view to model', () => {
  78. it( 'should convert media figure', () => {
  79. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  80. expect( getModelData( model, { withoutSelection: true } ) )
  81. .to.equal( '<media url="http://ckeditor.com"></media>' );
  82. } );
  83. it( 'should not convert if there is no media class', () => {
  84. editor.setData( '<figure class="quote">My quote</figure>' );
  85. expect( getModelData( model, { withoutSelection: true } ) )
  86. .to.equal( '' );
  87. } );
  88. it( 'should not convert if there is no oembed wrapper inside #1', () => {
  89. editor.setData( '<figure class="media"></figure>' );
  90. expect( getModelData( model, { withoutSelection: true } ) )
  91. .to.equal( '' );
  92. } );
  93. it( 'should not convert if there is no oembed wrapper inside #2', () => {
  94. editor.setData( '<figure class="media">test</figure>' );
  95. expect( getModelData( model, { withoutSelection: true } ) )
  96. .to.equal( '' );
  97. } );
  98. it( 'should not convert in the wrong context', () => {
  99. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  100. model.schema.addChildCheck( ( ctx, childDef ) => {
  101. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  102. return false;
  103. }
  104. } );
  105. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  106. editor.setData( '<div><figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure></div>' );
  107. expect( getModelData( model, { withoutSelection: true } ) )
  108. .to.equal( '<div></div>' );
  109. } );
  110. it( 'should not convert if the oembed wrapper is already consumed', () => {
  111. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  112. const img = data.viewItem.getChild( 0 );
  113. conversionApi.consumable.consume( img, { name: true } );
  114. }, { priority: 'high' } );
  115. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  116. expect( getModelData( model, { withoutSelection: true } ) )
  117. .to.equal( '' );
  118. } );
  119. it( 'should not convert if the figure is already consumed', () => {
  120. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  121. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  122. }, { priority: 'high' } );
  123. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  124. expect( getModelData( model, { withoutSelection: true } ) )
  125. .to.equal( '' );
  126. } );
  127. } );
  128. } );
  129. describe( 'semanticDataOutput=false', () => {
  130. beforeEach( () => {
  131. return VirtualTestEditor
  132. .create( {
  133. plugins: [ MediaEmbedEditing ],
  134. mediaEmbed: {
  135. media: mediaDefinitions
  136. }
  137. } )
  138. .then( newEditor => {
  139. editor = newEditor;
  140. model = editor.model;
  141. doc = model.document;
  142. view = editor.editing.view;
  143. } );
  144. } );
  145. describe( 'conversion in the data pipeline', () => {
  146. describe( 'model to view', () => {
  147. it( 'should convert', () => {
  148. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  149. expect( editor.getData() ).to.equal(
  150. '<figure class="media">' +
  151. '<div data-oembed-url="http://ckeditor.com">' +
  152. '<div class="ck-media__wrapper__aspect">' +
  153. '<iframe src="http://ckeditor.com"></iframe>' +
  154. '</div>' +
  155. '</div>' +
  156. '</figure>' );
  157. } );
  158. it( 'should convert (no url)', () => {
  159. setModelData( model, '<media></media>' );
  160. expect( editor.getData() ).to.equal(
  161. '<figure class="media">' +
  162. '<div>' +
  163. '<div class="ck-media__wrapper__aspect"></div>' +
  164. '</div>' +
  165. '</figure>' );
  166. } );
  167. } );
  168. describe( 'view to model', () => {
  169. it( 'should convert media figure', () => {
  170. editor.setData(
  171. '<figure class="media">' +
  172. '<div data-oembed-url="http://ckeditor.com">' +
  173. '<div class="ck-media__wrapper__aspect">' +
  174. '<iframe src="http://cksource.com"></iframe>' +
  175. '</div>' +
  176. '</div>' +
  177. '</figure>' );
  178. expect( getModelData( model, { withoutSelection: true } ) )
  179. .to.equal( '<media url="http://ckeditor.com"></media>' );
  180. } );
  181. it( 'should not convert if there is no media class', () => {
  182. editor.setData( '<figure class="quote">My quote</figure>' );
  183. expect( getModelData( model, { withoutSelection: true } ) )
  184. .to.equal( '' );
  185. } );
  186. it( 'should not convert if there is no oembed wrapper inside #1', () => {
  187. editor.setData( '<figure class="media"></figure>' );
  188. expect( getModelData( model, { withoutSelection: true } ) )
  189. .to.equal( '' );
  190. } );
  191. it( 'should not convert if there is no oembed wrapper inside #2', () => {
  192. editor.setData( '<figure class="media">test</figure>' );
  193. expect( getModelData( model, { withoutSelection: true } ) )
  194. .to.equal( '' );
  195. } );
  196. it( 'should not convert in the wrong context', () => {
  197. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  198. model.schema.addChildCheck( ( ctx, childDef ) => {
  199. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  200. return false;
  201. }
  202. } );
  203. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  204. editor.setData(
  205. '<div>' +
  206. '<figure class="media">' +
  207. '<div data-oembed-url="http://ckeditor.com">' +
  208. '<div class="ck-media__wrapper__aspect">' +
  209. '<iframe src="http://cksource.com"></iframe>' +
  210. '</div>' +
  211. '</div>' +
  212. '</figure>' +
  213. '</div>' );
  214. expect( getModelData( model, { withoutSelection: true } ) )
  215. .to.equal( '<div></div>' );
  216. } );
  217. it( 'should not convert if the oembed wrapper is already consumed', () => {
  218. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  219. const img = data.viewItem.getChild( 0 );
  220. conversionApi.consumable.consume( img, { name: true } );
  221. }, { priority: 'high' } );
  222. editor.setData(
  223. '<div>' +
  224. '<figure class="media">' +
  225. '<div data-oembed-url="http://ckeditor.com">' +
  226. '<div class="ck-media__wrapper__aspect">' +
  227. '<iframe src="http://cksource.com"></iframe>' +
  228. '</div>' +
  229. '</div>' +
  230. '</figure>' +
  231. '</div>' );
  232. expect( getModelData( model, { withoutSelection: true } ) )
  233. .to.equal( '' );
  234. } );
  235. it( 'should not convert if the figure is already consumed', () => {
  236. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  237. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  238. }, { priority: 'high' } );
  239. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  240. expect( getModelData( model, { withoutSelection: true } ) )
  241. .to.equal( '' );
  242. } );
  243. } );
  244. } );
  245. } );
  246. } );
  247. describe( 'conversion in the editing pipeline', () => {
  248. describe( 'semanticDataOutput=true', () => {
  249. beforeEach( () => {
  250. return VirtualTestEditor
  251. .create( {
  252. plugins: [ MediaEmbedEditing ],
  253. mediaEmbed: {
  254. media: mediaDefinitions,
  255. semanticDataOutput: true
  256. }
  257. } )
  258. .then( newEditor => {
  259. editor = newEditor;
  260. model = editor.model;
  261. doc = model.document;
  262. view = editor.editing.view;
  263. } );
  264. } );
  265. test();
  266. } );
  267. describe( 'semanticDataOutput=false', () => {
  268. beforeEach( () => {
  269. return VirtualTestEditor
  270. .create( {
  271. plugins: [ MediaEmbedEditing ],
  272. mediaEmbed: {
  273. media: mediaDefinitions
  274. }
  275. } )
  276. .then( newEditor => {
  277. editor = newEditor;
  278. model = editor.model;
  279. doc = model.document;
  280. view = editor.editing.view;
  281. } );
  282. } );
  283. test();
  284. } );
  285. function test() {
  286. describe( 'model to view', () => {
  287. it( 'should convert', () => {
  288. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  289. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  290. '<figure class="ck-widget media" contenteditable="false">' +
  291. '<div class="ck-media__wrapper" data-oembed-url="http://ckeditor.com">' +
  292. '<div class="ck-media__wrapper__aspect">' +
  293. '<iframe src="http://ckeditor.com"></iframe>' +
  294. '</div>' +
  295. '</div>' +
  296. '</figure>'
  297. );
  298. } );
  299. it( 'should convert the url attribute change', () => {
  300. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  301. const media = doc.getRoot().getChild( 0 );
  302. model.change( writer => {
  303. writer.setAttribute( 'url', 'http://cksource.com', media );
  304. } );
  305. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  306. '<figure class="ck-widget media" contenteditable="false">' +
  307. '<div class="ck-media__wrapper" data-oembed-url="http://cksource.com">' +
  308. '<div class="ck-media__wrapper__aspect">' +
  309. '<iframe src="http://cksource.com"></iframe>' +
  310. '</div>' +
  311. '</div>' +
  312. '</figure>'
  313. );
  314. } );
  315. it( 'should convert the url attribute removal', () => {
  316. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  317. const media = doc.getRoot().getChild( 0 );
  318. model.change( writer => {
  319. writer.removeAttribute( 'url', media );
  320. } );
  321. expect( getViewData( view, { withoutSelection: true } ) )
  322. .to.equal(
  323. '<figure class="ck-widget media" contenteditable="false">' +
  324. '<div class="ck-media__wrapper">' +
  325. '<div class="ck-media__wrapper__aspect">' +
  326. '<p>No embeddable media found for given URL.</p>' +
  327. '</div>' +
  328. '</div>' +
  329. '</figure>'
  330. );
  331. } );
  332. it( 'should not convert the url attribute removal if is already consumed', () => {
  333. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  334. const media = doc.getRoot().getChild( 0 );
  335. editor.editing.downcastDispatcher.on( 'attribute:url:media', ( evt, data, conversionApi ) => {
  336. conversionApi.consumable.consume( data.item, 'attribute:url' );
  337. }, { priority: 'high' } );
  338. model.change( writer => {
  339. writer.removeAttribute( 'url', media );
  340. } );
  341. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  342. '<figure class="ck-widget media" contenteditable="false">' +
  343. '<div class="ck-media__wrapper" data-oembed-url="http://ckeditor.com">' +
  344. '<div class="ck-media__wrapper__aspect">' +
  345. '<iframe src="http://ckeditor.com"></iframe>' +
  346. '</div>' +
  347. '</div>' +
  348. '</figure>'
  349. );
  350. } );
  351. } );
  352. }
  353. } );
  354. } );