mediaembedediting.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 when the wrapper has no data-oembed-url attribute', () => {
  99. editor.setData( '<figure class="media"><div></div></figure>' );
  100. expect( getModelData( model, { withoutSelection: true } ) )
  101. .to.equal( '' );
  102. } );
  103. it( 'should not convert in the wrong context', () => {
  104. model.schema.register( 'blockquote', { inheritAllFrom: '$block' } );
  105. model.schema.addChildCheck( ( ctx, childDef ) => {
  106. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  107. return false;
  108. }
  109. } );
  110. editor.conversion.elementToElement( { model: 'blockquote', view: 'blockquote' } );
  111. editor.setData(
  112. '<blockquote><figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure></blockquote>' );
  113. expect( getModelData( model, { withoutSelection: true } ) )
  114. .to.equal( '<blockquote></blockquote>' );
  115. } );
  116. it( 'should not convert if the oembed wrapper is already consumed', () => {
  117. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  118. const img = data.viewItem.getChild( 0 );
  119. conversionApi.consumable.consume( img, { name: true } );
  120. }, { priority: 'high' } );
  121. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  122. expect( getModelData( model, { withoutSelection: true } ) )
  123. .to.equal( '' );
  124. } );
  125. it( 'should not convert if the figure is already consumed', () => {
  126. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  127. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  128. }, { priority: 'high' } );
  129. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  130. expect( getModelData( model, { withoutSelection: true } ) )
  131. .to.equal( '' );
  132. } );
  133. } );
  134. } );
  135. describe( 'semanticDataOutput=false', () => {
  136. beforeEach( () => {
  137. return VirtualTestEditor
  138. .create( {
  139. plugins: [ MediaEmbedEditing ],
  140. mediaEmbed: {
  141. media: mediaDefinitions
  142. }
  143. } )
  144. .then( newEditor => {
  145. editor = newEditor;
  146. model = editor.model;
  147. doc = model.document;
  148. view = editor.editing.view;
  149. } );
  150. } );
  151. describe( 'conversion in the data pipeline', () => {
  152. describe( 'model to view', () => {
  153. it( 'should convert', () => {
  154. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  155. expect( editor.getData() ).to.equal(
  156. '<figure class="media">' +
  157. '<div data-oembed-url="http://ckeditor.com">' +
  158. '<div class="ck-media__wrapper__aspect">' +
  159. '<iframe src="http://ckeditor.com"></iframe>' +
  160. '</div>' +
  161. '</div>' +
  162. '</figure>' );
  163. } );
  164. it( 'should convert (no url)', () => {
  165. setModelData( model, '<media></media>' );
  166. expect( editor.getData() ).to.equal(
  167. '<figure class="media">' +
  168. '<div>' +
  169. '<div class="ck-media__wrapper__aspect"></div>' +
  170. '</div>' +
  171. '</figure>' );
  172. } );
  173. } );
  174. describe( 'view to model', () => {
  175. it( 'should convert media figure', () => {
  176. editor.setData(
  177. '<figure class="media">' +
  178. '<div data-oembed-url="http://ckeditor.com">' +
  179. '<div class="ck-media__wrapper__aspect">' +
  180. '<iframe src="http://cksource.com"></iframe>' +
  181. '</div>' +
  182. '</div>' +
  183. '</figure>' );
  184. expect( getModelData( model, { withoutSelection: true } ) )
  185. .to.equal( '<media url="http://ckeditor.com"></media>' );
  186. } );
  187. it( 'should not convert if there is no media class', () => {
  188. editor.setData( '<figure class="quote">My quote</figure>' );
  189. expect( getModelData( model, { withoutSelection: true } ) )
  190. .to.equal( '' );
  191. } );
  192. it( 'should not convert if there is no oembed wrapper inside #1', () => {
  193. editor.setData( '<figure class="media"></figure>' );
  194. expect( getModelData( model, { withoutSelection: true } ) )
  195. .to.equal( '' );
  196. } );
  197. it( 'should not convert if there is no oembed wrapper inside #2', () => {
  198. editor.setData( '<figure class="media">test</figure>' );
  199. expect( getModelData( model, { withoutSelection: true } ) )
  200. .to.equal( '' );
  201. } );
  202. it( 'should not convert in the wrong context', () => {
  203. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  204. model.schema.addChildCheck( ( ctx, childDef ) => {
  205. if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
  206. return false;
  207. }
  208. } );
  209. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  210. editor.setData(
  211. '<div>' +
  212. '<figure class="media">' +
  213. '<div data-oembed-url="http://ckeditor.com">' +
  214. '<div class="ck-media__wrapper__aspect">' +
  215. '<iframe src="http://cksource.com"></iframe>' +
  216. '</div>' +
  217. '</div>' +
  218. '</figure>' +
  219. '</div>' );
  220. expect( getModelData( model, { withoutSelection: true } ) )
  221. .to.equal( '<div></div>' );
  222. } );
  223. it( 'should not convert if the oembed wrapper is already consumed', () => {
  224. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  225. const img = data.viewItem.getChild( 0 );
  226. conversionApi.consumable.consume( img, { name: true } );
  227. }, { priority: 'high' } );
  228. editor.setData(
  229. '<div>' +
  230. '<figure class="media">' +
  231. '<div data-oembed-url="http://ckeditor.com">' +
  232. '<div class="ck-media__wrapper__aspect">' +
  233. '<iframe src="http://cksource.com"></iframe>' +
  234. '</div>' +
  235. '</div>' +
  236. '</figure>' +
  237. '</div>' );
  238. expect( getModelData( model, { withoutSelection: true } ) )
  239. .to.equal( '' );
  240. } );
  241. it( 'should not convert if the figure is already consumed', () => {
  242. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  243. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  244. }, { priority: 'high' } );
  245. editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
  246. expect( getModelData( model, { withoutSelection: true } ) )
  247. .to.equal( '' );
  248. } );
  249. } );
  250. } );
  251. } );
  252. } );
  253. describe( 'conversion in the editing pipeline', () => {
  254. describe( 'semanticDataOutput=true', () => {
  255. beforeEach( () => {
  256. return VirtualTestEditor
  257. .create( {
  258. plugins: [ MediaEmbedEditing ],
  259. mediaEmbed: {
  260. media: mediaDefinitions,
  261. semanticDataOutput: true
  262. }
  263. } )
  264. .then( newEditor => {
  265. editor = newEditor;
  266. model = editor.model;
  267. doc = model.document;
  268. view = editor.editing.view;
  269. } );
  270. } );
  271. test();
  272. } );
  273. describe( 'semanticDataOutput=false', () => {
  274. beforeEach( () => {
  275. return VirtualTestEditor
  276. .create( {
  277. plugins: [ MediaEmbedEditing ],
  278. mediaEmbed: {
  279. media: mediaDefinitions
  280. }
  281. } )
  282. .then( newEditor => {
  283. editor = newEditor;
  284. model = editor.model;
  285. doc = model.document;
  286. view = editor.editing.view;
  287. } );
  288. } );
  289. test();
  290. } );
  291. function test() {
  292. describe( 'model to view', () => {
  293. it( 'should convert', () => {
  294. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  295. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  296. '<figure class="ck-widget media" contenteditable="false">' +
  297. '<div class="ck-media__wrapper" data-oembed-url="http://ckeditor.com">' +
  298. '<div class="ck-media__wrapper__aspect">' +
  299. '<iframe src="http://ckeditor.com"></iframe>' +
  300. '</div>' +
  301. '</div>' +
  302. '</figure>'
  303. );
  304. } );
  305. it( 'should convert the url attribute change', () => {
  306. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  307. const media = doc.getRoot().getChild( 0 );
  308. model.change( writer => {
  309. writer.setAttribute( 'url', 'http://cksource.com', media );
  310. } );
  311. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  312. '<figure class="ck-widget media" contenteditable="false">' +
  313. '<div class="ck-media__wrapper" data-oembed-url="http://cksource.com">' +
  314. '<div class="ck-media__wrapper__aspect">' +
  315. '<iframe src="http://cksource.com"></iframe>' +
  316. '</div>' +
  317. '</div>' +
  318. '</figure>'
  319. );
  320. } );
  321. it( 'should convert the url attribute removal', () => {
  322. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  323. const media = doc.getRoot().getChild( 0 );
  324. model.change( writer => {
  325. writer.removeAttribute( 'url', media );
  326. } );
  327. expect( getViewData( view, { withoutSelection: true } ) )
  328. .to.equal(
  329. '<figure class="ck-widget media" contenteditable="false">' +
  330. '<div class="ck-media__wrapper">' +
  331. '<div class="ck-media__wrapper__aspect">' +
  332. '<p>No embeddable media found for given URL.</p>' +
  333. '</div>' +
  334. '</div>' +
  335. '</figure>'
  336. );
  337. } );
  338. it( 'should not convert the url attribute removal if is already consumed', () => {
  339. setModelData( model, '<media url="http://ckeditor.com"></media>' );
  340. const media = doc.getRoot().getChild( 0 );
  341. editor.editing.downcastDispatcher.on( 'attribute:url:media', ( evt, data, conversionApi ) => {
  342. conversionApi.consumable.consume( data.item, 'attribute:url' );
  343. }, { priority: 'high' } );
  344. model.change( writer => {
  345. writer.removeAttribute( 'url', media );
  346. } );
  347. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  348. '<figure class="ck-widget media" contenteditable="false">' +
  349. '<div class="ck-media__wrapper" data-oembed-url="http://ckeditor.com">' +
  350. '<div class="ck-media__wrapper__aspect">' +
  351. '<iframe src="http://ckeditor.com"></iframe>' +
  352. '</div>' +
  353. '</div>' +
  354. '</figure>'
  355. );
  356. } );
  357. } );
  358. }
  359. } );
  360. } );