imageengine.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 ImageEngine from '../../src/image/imageengine';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  10. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  11. import { isImageWidget } from '../../src/image/utils';
  12. describe( 'ImageEngine', () => {
  13. let editor, document, viewDocument;
  14. beforeEach( () => {
  15. return VirtualTestEditor.create( {
  16. plugins: [ ImageEngine ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. document = editor.document;
  21. viewDocument = editor.editing.view;
  22. } );
  23. } );
  24. it( 'should be loaded', () => {
  25. expect( editor.plugins.get( ImageEngine ) ).to.be.instanceOf( ImageEngine );
  26. } );
  27. it( 'should set proper schema rules', () => {
  28. expect( document.schema.check( { name: 'image', attributes: [ 'src', 'alt' ], inside: '$root' } ) ).to.be.true;
  29. expect( document.schema.objects.has( 'image' ) ).to.be.true;
  30. } );
  31. describe( 'conversion in data pipeline', () => {
  32. describe( 'model to view', () => {
  33. it( 'should convert', () => {
  34. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  35. expect( editor.getData() ).to.equal( '<figure class="image"><img alt="alt text" src="foo.png"></figure>' );
  36. } );
  37. it( 'should convert without alt attribute', () => {
  38. setModelData( document, '<image src="foo.png"></image>' );
  39. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  40. } );
  41. it( 'should convert with srcset attribute and add sizes attribute', () => {
  42. setModelData( document, '<image src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w"></image>' );
  43. expect( editor.getData() ).to.equal(
  44. '<figure class="image">' +
  45. '<img srcset="small.png 148w, big.png 1024w" sizes="100vw" alt="alt text" src="foo.png">' +
  46. '</figure>'
  47. );
  48. } );
  49. } );
  50. describe( 'view to model', () => {
  51. it( 'should convert image figure', () => {
  52. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  53. expect( getModelData( document, { withoutSelection: true } ) )
  54. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  55. } );
  56. it( 'should not convert if there is no image class', () => {
  57. editor.setData( '<figure class="quote">My quote</figure>' );
  58. expect( getModelData( document, { withoutSelection: true } ) )
  59. .to.equal( '' );
  60. } );
  61. it( 'should not convert if there is no img inside #1', () => {
  62. editor.setData( '<figure class="image"></figure>' );
  63. expect( getModelData( document, { withoutSelection: true } ) )
  64. .to.equal( '' );
  65. } );
  66. it( 'should not convert if there is no img inside #2', () => {
  67. editor.setData( '<figure class="image">test</figure>' );
  68. expect( getModelData( document, { withoutSelection: true } ) )
  69. .to.equal( '' );
  70. } );
  71. it( 'should convert without alt attribute', () => {
  72. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  73. expect( getModelData( document, { withoutSelection: true } ) )
  74. .to.equal( '<image src="foo.png"></image>' );
  75. } );
  76. it( 'should not convert without src attribute', () => {
  77. editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
  78. expect( getModelData( document, { withoutSelection: true } ) )
  79. .to.equal( '' );
  80. } );
  81. it( 'should not convert in wrong context', () => {
  82. const data = editor.data;
  83. const editing = editor.editing;
  84. document.schema.registerItem( 'div', '$block' );
  85. document.schema.disallow( { name: 'image', inside: '$root', attributes: 'src' } );
  86. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  87. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  88. editor.setData( '<div><figure class="image"><img src="foo.png" alt="alt text" /></figure></div>' );
  89. expect( getModelData( document, { withoutSelection: true } ) )
  90. .to.equal( '<div></div>' );
  91. } );
  92. it( 'should not convert if img is already consumed', () => {
  93. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  94. const img = data.input.getChild( 0 );
  95. consumable.consume( img, { name: true } );
  96. }, { priority: 'high' } );
  97. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  98. expect( getModelData( document, { withoutSelection: true } ) )
  99. .to.equal( '' );
  100. } );
  101. it( 'should not convert if figure is already consumed', () => {
  102. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  103. const figure = data.input;
  104. consumable.consume( figure, { name: true, class: 'image' } );
  105. }, { priority: 'high' } );
  106. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  107. expect( getModelData( document, { withoutSelection: true } ) )
  108. .to.equal( '' );
  109. } );
  110. it( 'should dispatch conversion for nested elements', () => {
  111. const conversionSpy = sinon.spy();
  112. editor.data.viewToModel.on( 'element:figcaption', conversionSpy );
  113. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /><figcaption></figcaption></figure>' );
  114. sinon.assert.calledOnce( conversionSpy );
  115. } );
  116. it( 'should convert bare img element', () => {
  117. editor.setData( '<img src="foo.png" alt="alt text" />' );
  118. expect( getModelData( document, { withoutSelection: true } ) )
  119. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  120. } );
  121. it( 'should not convert alt attribute on non-img element', () => {
  122. const data = editor.data;
  123. const editing = editor.editing;
  124. document.schema.registerItem( 'div', '$block' );
  125. document.schema.allow( { name: 'div', attributes: 'alt', inside: '$root' } );
  126. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  127. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  128. editor.setData( '<div alt="foo"></div>' );
  129. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<div></div>' );
  130. } );
  131. it( 'should handle figure with two images', () => {
  132. document.schema.allow( { name: '$text', inside: 'image' } );
  133. editor.setData( '<figure class="image"><img src="foo.jpg" /><img src="bar.jpg" />abc</figure>' );
  134. // The foo.jpg image is properly converted using figure converter. The other image was tried to
  135. // be added as a child of foo.jpg and then was autohoisted.
  136. expect( getModelData( document, { withoutSelection: true } ) )
  137. .to.equal( '<image src="bar.jpg"></image><image src="foo.jpg">abc</image>' );
  138. } );
  139. it( 'should convert image with srcset attribute', () => {
  140. editor.setData(
  141. '<figure class="image">' +
  142. '<img src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w" />' +
  143. '</figure>'
  144. );
  145. expect( getModelData( document, { withoutSelection: true } ) )
  146. .to.equal( '<image alt="alt text" src="foo.png" srcset="small.png 148w, big.png 1024w"></image>' );
  147. } );
  148. it( 'should ignore sizes attribute', () => {
  149. editor.setData(
  150. '<figure class="image">' +
  151. '<img src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w" sizes="50vw" />' +
  152. '</figure>'
  153. );
  154. expect( getModelData( document, { withoutSelection: true } ) )
  155. .to.equal( '<image alt="alt text" src="foo.png" srcset="small.png 148w, big.png 1024w"></image>' );
  156. } );
  157. describe( 'should autohoist images', () => {
  158. beforeEach( () => {
  159. document.schema.registerItem( 'div', '$block' );
  160. buildModelConverter()
  161. .for( editor.data.modelToView, editor.editing.modelToView )
  162. .fromElement( 'div' )
  163. .toElement( 'div' );
  164. buildViewConverter()
  165. .for( editor.data.viewToModel )
  166. .fromElement( 'div' )
  167. .toElement( 'div' );
  168. } );
  169. it( 'image between non-hoisted elements', () => {
  170. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />bar</div>' );
  171. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  172. '<div>foo</div>' +
  173. '<image alt="foo" src="foo.jpg"></image>' +
  174. '<div>bar</div>'
  175. );
  176. } );
  177. it( 'multiple images', () => {
  178. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />ba<img src="foo.jpg" alt="foo" />r</div>' );
  179. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  180. '<div>foo</div>' +
  181. '<image alt="foo" src="foo.jpg"></image>' +
  182. '<div>ba</div>' +
  183. '<image alt="foo" src="foo.jpg"></image>' +
  184. '<div>r</div>'
  185. );
  186. } );
  187. it( 'images on borders of parent', () => {
  188. editor.setData( '<div><img src="foo.jpg" alt="foo" />foobar<img src="foo.jpg" alt="foo" /></div>' );
  189. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  190. '<image alt="foo" src="foo.jpg"></image>' +
  191. '<div>foobar</div>' +
  192. '<image alt="foo" src="foo.jpg"></image>'
  193. );
  194. } );
  195. it( 'images are only content of parent', () => {
  196. editor.setData( '<div><img src="foo.jpg" alt="foo" /><img src="foo.jpg" alt="foo" /></div>' );
  197. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  198. '<image alt="foo" src="foo.jpg"></image>' +
  199. '<image alt="foo" src="foo.jpg"></image>'
  200. );
  201. } );
  202. it( 'deep autohoisting #1', () => {
  203. document.schema.allow( { name: 'div', inside: 'div' } );
  204. editor.setData( '<div>foo<div>xx<img src="foo.jpg" alt="foo" /></div>bar</div>' );
  205. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  206. '<div>' +
  207. 'foo' +
  208. '<div>' +
  209. 'xx' +
  210. '</div>' +
  211. '</div>' +
  212. '<image alt="foo" src="foo.jpg"></image>' +
  213. '<div>bar</div>'
  214. );
  215. } );
  216. it( 'deep autohoisting #2', () => {
  217. document.schema.allow( { name: 'div', inside: 'div' } );
  218. editor.setData(
  219. '<div>x</div>' +
  220. '<div><div><div><img src="foo.jpg" alt="foo" /></div></div></div>' +
  221. '<div>y</div>'
  222. );
  223. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  224. '<div>x</div><image alt="foo" src="foo.jpg"></image><div>y</div>'
  225. );
  226. } );
  227. it( 'should not break a limiting element', () => {
  228. document.schema.registerItem( 'limit', '$block' );
  229. document.schema.allow( { name: 'div', inside: 'limit' } );
  230. document.schema.limits.add( 'limit' );
  231. buildModelConverter()
  232. .for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'limit' ).toElement( 'limit' );
  233. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'limit' ).toElement( 'limit' );
  234. editor.setData( '<limit><div>foo<img src="foo.jpg" alt="foo" />bar</div></limit>' );
  235. // <limit> element does not have converters so it is not converted.
  236. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<limit><div>foobar</div></limit>' );
  237. } );
  238. it( 'should not convert and autohoist image element without src attribute (which is not allowed by schema)', () => {
  239. editor.setData( '<div>foo<img alt="foo" />bar</div>' );
  240. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<div>foobar</div>' );
  241. } );
  242. } );
  243. } );
  244. } );
  245. describe( 'conversion in editing pipeline', () => {
  246. describe( 'model to view', () => {
  247. it( 'should convert', () => {
  248. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  249. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  250. '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  251. );
  252. } );
  253. it( 'converted element should be widgetized', () => {
  254. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  255. const figure = viewDocument.getRoot().getChild( 0 );
  256. expect( figure.name ).to.equal( 'figure' );
  257. expect( isImageWidget( figure ) ).to.be.true;
  258. } );
  259. it( 'should convert attribute change', () => {
  260. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  261. const image = document.getRoot().getChild( 0 );
  262. document.enqueueChanges( () => {
  263. const batch = document.batch();
  264. batch.setAttribute( image, 'alt', 'new text' );
  265. } );
  266. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  267. '<figure class="image ck-widget" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>'
  268. );
  269. } );
  270. it( 'should convert attribute removal', () => {
  271. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  272. const image = document.getRoot().getChild( 0 );
  273. document.enqueueChanges( () => {
  274. const batch = document.batch();
  275. batch.removeAttribute( image, 'alt' );
  276. } );
  277. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  278. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>' );
  279. } );
  280. it( 'should not convert change if is already consumed', () => {
  281. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  282. const image = document.getRoot().getChild( 0 );
  283. editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => {
  284. consumable.consume( data.item, 'removeAttribute:alt' );
  285. }, { priority: 'high' } );
  286. document.enqueueChanges( () => {
  287. const batch = document.batch();
  288. batch.removeAttribute( image, 'alt' );
  289. } );
  290. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  291. '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  292. );
  293. } );
  294. it( 'should convert srcset attribute to srcset and sizes', () => {
  295. setModelData( document, '<image src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w"></image>' );
  296. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  297. '<figure class="image ck-widget" contenteditable="false">' +
  298. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w"></img>' +
  299. '</figure>'
  300. );
  301. } );
  302. it( 'should remove sizes attribute when srcset attribute is removed', () => {
  303. setModelData( document, '<image src="foo.png" srcset="small.png 148w, big.png 1024w"></image>' );
  304. const image = document.getRoot().getChild( 0 );
  305. document.enqueueChanges( () => {
  306. const batch = document.batch();
  307. batch.removeAttribute( image, 'srcset' );
  308. } );
  309. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  310. '<figure class="image ck-widget" contenteditable="false">' +
  311. '<img src="foo.png"></img>' +
  312. '</figure>'
  313. );
  314. } );
  315. } );
  316. } );
  317. } );