imageengine.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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()
  135. .for( editor.data.modelToView, editor.editing.modelToView )
  136. .fromElement( 'div' )
  137. .toElement( 'div' );
  138. buildViewConverter()
  139. .for( editor.data.viewToModel )
  140. .fromElement( 'div' )
  141. .toElement( 'div' );
  142. } );
  143. it( 'image between non-hoisted elements', () => {
  144. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />bar</div>' );
  145. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  146. '<div>foo</div>' +
  147. '<image alt="foo" src="foo.jpg"></image>' +
  148. '<div>bar</div>'
  149. );
  150. } );
  151. it( 'multiple images', () => {
  152. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />ba<img src="foo.jpg" alt="foo" />r</div>' );
  153. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  154. '<div>foo</div>' +
  155. '<image alt="foo" src="foo.jpg"></image>' +
  156. '<div>ba</div>' +
  157. '<image alt="foo" src="foo.jpg"></image>' +
  158. '<div>r</div>'
  159. );
  160. } );
  161. it( 'images on borders of parent', () => {
  162. editor.setData( '<div><img src="foo.jpg" alt="foo" />foobar<img src="foo.jpg" alt="foo" /></div>' );
  163. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  164. '<image alt="foo" src="foo.jpg"></image>' +
  165. '<div>foobar</div>' +
  166. '<image alt="foo" src="foo.jpg"></image>'
  167. );
  168. } );
  169. it( 'images are only content of parent', () => {
  170. editor.setData( '<div><img src="foo.jpg" alt="foo" /><img src="foo.jpg" alt="foo" /></div>' );
  171. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  172. '<image alt="foo" src="foo.jpg"></image>' +
  173. '<image alt="foo" src="foo.jpg"></image>'
  174. );
  175. } );
  176. it( 'deep autohoisting #1', () => {
  177. document.schema.allow( { name: 'div', inside: 'div' } );
  178. editor.setData( '<div>foo<div>xx<img src="foo.jpg" alt="foo" /></div>bar</div>' );
  179. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  180. '<div>' +
  181. 'foo' +
  182. '<div>' +
  183. 'xx' +
  184. '</div>' +
  185. '</div>' +
  186. '<image alt="foo" src="foo.jpg"></image>' +
  187. '<div>bar</div>'
  188. );
  189. } );
  190. it( 'deep autohoisting #2', () => {
  191. document.schema.allow( { name: 'div', inside: 'div' } );
  192. editor.setData(
  193. '<div>x</div>' +
  194. '<div><div><div><img src="foo.jpg" alt="foo" /></div></div></div>' +
  195. '<div>y</div>'
  196. );
  197. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  198. '<div>x</div><image alt="foo" src="foo.jpg"></image><div>y</div>'
  199. );
  200. } );
  201. it( 'should not break a limiting element', () => {
  202. document.schema.registerItem( 'limit', '$block' );
  203. document.schema.allow( { name: 'div', inside: 'limit' } );
  204. document.schema.limits.add( 'limit' );
  205. buildModelConverter()
  206. .for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'limit' ).toElement( 'limit' );
  207. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'limit' ).toElement( 'limit' );
  208. editor.setData( '<limit><div>foo<img src="foo.jpg" alt="foo" />bar</div></limit>' );
  209. // <limit> element does not have converters so it is not converted.
  210. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<limit><div>foobar</div></limit>' );
  211. } );
  212. it( 'should not convert and autohoist image element without src attribute (which is not allowed by schema)', () => {
  213. editor.setData( '<div>foo<img alt="foo" />bar</div>' );
  214. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<div>foobar</div>' );
  215. } );
  216. } );
  217. } );
  218. } );
  219. describe( 'conversion in editing pipeline', () => {
  220. describe( 'model to view', () => {
  221. it( 'should convert', () => {
  222. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  223. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  224. '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  225. );
  226. } );
  227. it( 'converted element should be widgetized', () => {
  228. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  229. const figure = viewDocument.getRoot().getChild( 0 );
  230. expect( figure.name ).to.equal( 'figure' );
  231. expect( isImageWidget( figure ) ).to.be.true;
  232. } );
  233. it( 'should convert attribute change', () => {
  234. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  235. const image = document.getRoot().getChild( 0 );
  236. document.enqueueChanges( () => {
  237. const batch = document.batch();
  238. batch.setAttribute( image, 'alt', 'new text' );
  239. } );
  240. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  241. '<figure class="image ck-widget" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>'
  242. );
  243. } );
  244. it( 'should convert attribute removal', () => {
  245. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  246. const image = document.getRoot().getChild( 0 );
  247. document.enqueueChanges( () => {
  248. const batch = document.batch();
  249. batch.removeAttribute( image, 'alt' );
  250. } );
  251. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  252. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>' );
  253. } );
  254. it( 'should not convert change if is already consumed', () => {
  255. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  256. const image = document.getRoot().getChild( 0 );
  257. editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => {
  258. consumable.consume( data.item, 'removeAttribute:alt' );
  259. }, { priority: 'high' } );
  260. document.enqueueChanges( () => {
  261. const batch = document.batch();
  262. batch.removeAttribute( image, 'alt' );
  263. } );
  264. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  265. '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  266. );
  267. } );
  268. } );
  269. } );
  270. } );