imageengine.js 15 KB

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