imageengine.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. } );
  42. describe( 'view to model', () => {
  43. it( 'should convert image figure', () => {
  44. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  45. expect( getModelData( document, { withoutSelection: true } ) )
  46. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  47. } );
  48. it( 'should not convert if there is no image class', () => {
  49. editor.setData( '<figure class="quote">My quote</figure>' );
  50. expect( getModelData( document, { withoutSelection: true } ) )
  51. .to.equal( '' );
  52. } );
  53. it( 'should not convert if there is no img inside #1', () => {
  54. editor.setData( '<figure class="image"></figure>' );
  55. expect( getModelData( document, { withoutSelection: true } ) )
  56. .to.equal( '' );
  57. } );
  58. it( 'should not convert if there is no img inside #2', () => {
  59. editor.setData( '<figure class="image">test</figure>' );
  60. expect( getModelData( document, { withoutSelection: true } ) )
  61. .to.equal( '' );
  62. } );
  63. it( 'should convert without alt attribute', () => {
  64. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  65. expect( getModelData( document, { withoutSelection: true } ) )
  66. .to.equal( '<image src="foo.png"></image>' );
  67. } );
  68. it( 'should not convert without src attribute', () => {
  69. editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
  70. expect( getModelData( document, { withoutSelection: true } ) )
  71. .to.equal( '' );
  72. } );
  73. it( 'should not convert in wrong context', () => {
  74. const data = editor.data;
  75. const editing = editor.editing;
  76. document.schema.registerItem( 'div', '$block' );
  77. document.schema.disallow( { name: 'image', inside: '$root', attributes: 'src' } );
  78. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  79. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  80. editor.setData( '<div><figure class="image"><img src="foo.png" alt="alt text" /></figure></div>' );
  81. expect( getModelData( document, { withoutSelection: true } ) )
  82. .to.equal( '<div></div>' );
  83. } );
  84. it( 'should not convert if img is already consumed', () => {
  85. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  86. const img = data.input.getChild( 0 );
  87. consumable.consume( img, { name: true } );
  88. }, { priority: 'high' } );
  89. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  90. expect( getModelData( document, { withoutSelection: true } ) )
  91. .to.equal( '' );
  92. } );
  93. it( 'should not convert if figure is already consumed', () => {
  94. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  95. const figure = data.input;
  96. consumable.consume( figure, { name: true, class: 'image' } );
  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 dispatch conversion for nested elements', () => {
  103. const conversionSpy = sinon.spy();
  104. editor.data.viewToModel.on( 'element:figcaption', conversionSpy );
  105. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /><figcaption></figcaption></figure>' );
  106. sinon.assert.calledOnce( conversionSpy );
  107. } );
  108. it( 'should convert bare img element', () => {
  109. editor.setData( '<img src="foo.png" alt="alt text" />' );
  110. expect( getModelData( document, { withoutSelection: true } ) )
  111. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  112. } );
  113. it( 'should not convert alt attribute on non-img element', () => {
  114. const data = editor.data;
  115. const editing = editor.editing;
  116. document.schema.registerItem( 'div', '$block' );
  117. document.schema.allow( { name: 'div', attributes: 'alt', inside: '$root' } );
  118. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  119. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  120. editor.setData( '<div alt="foo"></div>' );
  121. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<div></div>' );
  122. } );
  123. it( 'should handle figure with two images', () => {
  124. document.schema.allow( { name: '$text', inside: 'image' } );
  125. editor.setData( '<figure class="image"><img src="foo.jpg" /><img src="bar.jpg" />abc</figure>' );
  126. // The foo.jpg image is properly converted using figure converter. The other image was tried to
  127. // be added as a child of foo.jpg and then was autohoisted.
  128. expect( getModelData( document, { withoutSelection: true } ) )
  129. .to.equal( '<image src="bar.jpg"></image><image src="foo.jpg">abc</image>' );
  130. } );
  131. describe( 'should autohoist images', () => {
  132. beforeEach( () => {
  133. document.schema.registerItem( 'div', '$block' );
  134. buildModelConverter().for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  135. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  136. } );
  137. it( 'image between non-hoisted elements', () => {
  138. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />bar</div>' );
  139. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  140. '<div>foo</div>' +
  141. '<image alt="foo" src="foo.jpg"></image>' +
  142. '<div>bar</div>'
  143. );
  144. } );
  145. it( 'multiple images', () => {
  146. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />ba<img src="foo.jpg" alt="foo" />r</div>' );
  147. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  148. '<div>foo</div>' +
  149. '<image alt="foo" src="foo.jpg"></image>' +
  150. '<div>ba</div>' +
  151. '<image alt="foo" src="foo.jpg"></image>' +
  152. '<div>r</div>'
  153. );
  154. } );
  155. it( 'images on borders of parent', () => {
  156. editor.setData( '<div><img src="foo.jpg" alt="foo" />foobar<img src="foo.jpg" alt="foo" /></div>' );
  157. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  158. '<image alt="foo" src="foo.jpg"></image>' +
  159. '<div>foobar</div>' +
  160. '<image alt="foo" src="foo.jpg"></image>'
  161. );
  162. } );
  163. it( 'images are only content of parent', () => {
  164. editor.setData( '<div><img src="foo.jpg" alt="foo" /><img src="foo.jpg" alt="foo" /></div>' );
  165. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  166. '<image alt="foo" src="foo.jpg"></image>' +
  167. '<image alt="foo" src="foo.jpg"></image>'
  168. );
  169. } );
  170. it( 'deep autohoisting #1', () => {
  171. document.schema.allow( { name: 'div', inside: 'div' } );
  172. editor.setData( '<div>foo<div>xx<img src="foo.jpg" alt="foo" /></div>bar</div>' );
  173. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  174. '<div>' +
  175. 'foo' +
  176. '<div>' +
  177. 'xx' +
  178. '</div>' +
  179. '</div>' +
  180. '<image alt="foo" src="foo.jpg"></image>' +
  181. '<div>bar</div>'
  182. );
  183. } );
  184. it( 'deep autohoisting #2', () => {
  185. document.schema.allow( { name: 'div', inside: 'div' } );
  186. editor.setData(
  187. '<div>x</div>' +
  188. '<div><div><div><img src="foo.jpg" alt="foo" /></div></div></div>' +
  189. '<div>y</div>'
  190. );
  191. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  192. '<div>x</div><image alt="foo" src="foo.jpg"></image><div>y</div>'
  193. );
  194. } );
  195. it( 'should not break a limiting element', () => {
  196. document.schema.registerItem( 'limit', '$block' );
  197. document.schema.allow( { name: 'div', inside: 'limit' } );
  198. document.schema.limits.add( 'limit' );
  199. buildModelConverter()
  200. .for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'limit' ).toElement( 'limit' );
  201. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'limit' ).toElement( 'limit' );
  202. editor.setData( '<limit><div>foo<img src="foo.jpg" alt="foo" />bar</div></limit>' );
  203. // <limit> element does not have converters so it is not converted.
  204. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<limit><div>foobar</div></limit>' );
  205. } );
  206. it( 'should not convert and autohoist image element without src attribute (which is not allowed by schema)', () => {
  207. editor.setData( '<div>foo<img alt="foo" />bar</div>' );
  208. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<div>foobar</div>' );
  209. } );
  210. } );
  211. } );
  212. } );
  213. describe( 'conversion in editing pipeline', () => {
  214. describe( 'model to view', () => {
  215. it( 'should convert', () => {
  216. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  217. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  218. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  219. } );
  220. it( 'converted element should be widgetized', () => {
  221. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  222. const figure = viewDocument.getRoot().getChild( 0 );
  223. expect( figure.name ).to.equal( 'figure' );
  224. expect( isImageWidget( figure ) ).to.be.true;
  225. } );
  226. it( 'should convert attribute change', () => {
  227. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  228. const image = document.getRoot().getChild( 0 );
  229. document.enqueueChanges( () => {
  230. const batch = document.batch();
  231. batch.setAttribute( image, 'alt', 'new text' );
  232. } );
  233. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  234. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>' );
  235. } );
  236. it( 'should convert attribute removal', () => {
  237. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  238. const image = document.getRoot().getChild( 0 );
  239. document.enqueueChanges( () => {
  240. const batch = document.batch();
  241. batch.removeAttribute( image, 'alt' );
  242. } );
  243. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  244. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>' );
  245. } );
  246. it( 'should not convert change if is already consumed', () => {
  247. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  248. const image = document.getRoot().getChild( 0 );
  249. editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => {
  250. consumable.consume( data.item, 'removeAttribute:alt' );
  251. }, { priority: 'high' } );
  252. document.enqueueChanges( () => {
  253. const batch = document.batch();
  254. batch.removeAttribute( image, 'alt' );
  255. } );
  256. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  257. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  258. } );
  259. } );
  260. } );
  261. } );