imageengine.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  13. describe( 'ImageEngine', () => {
  14. let editor, document, viewDocument;
  15. beforeEach( () => {
  16. return VirtualTestEditor.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. } );
  43. describe( 'view to model', () => {
  44. it( 'should convert image figure', () => {
  45. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  46. expect( getModelData( document, { withoutSelection: true } ) )
  47. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  48. } );
  49. it( 'should not convert if there is no image class', () => {
  50. editor.setData( '<figure><img src="foo.png" alt="alt text" /></figure>' );
  51. expect( getModelData( document, { withoutSelection: true } ) )
  52. .to.equal( '' );
  53. } );
  54. it( 'should not convert if there is no img inside #1', () => {
  55. editor.setData( '<figure class="image"></figure>' );
  56. expect( getModelData( document, { withoutSelection: true } ) )
  57. .to.equal( '' );
  58. } );
  59. it( 'should not convert if there is no img inside #2', () => {
  60. editor.setData( '<figure class="image">test</figure>' );
  61. expect( getModelData( document, { withoutSelection: true } ) )
  62. .to.equal( '' );
  63. } );
  64. it( 'should convert without alt attribute', () => {
  65. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  66. expect( getModelData( document, { withoutSelection: true } ) )
  67. .to.equal( '<image src="foo.png"></image>' );
  68. } );
  69. it( 'should not convert without src attribute', () => {
  70. editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
  71. expect( getModelData( document, { withoutSelection: true } ) )
  72. .to.equal( '' );
  73. } );
  74. it( 'should not convert in wrong context', () => {
  75. const data = editor.data;
  76. const editing = editor.editing;
  77. document.schema.registerItem( 'div', '$block' );
  78. document.schema.disallow( { name: 'image', inside: 'div', attributes: [ 'src' ] } );
  79. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  80. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  81. editor.setData( '<div><figure class="image"><img src="foo.png" alt="alt text" /></figure></div>' );
  82. expect( getModelData( document, { withoutSelection: true } ) )
  83. .to.equal( '<div></div>' );
  84. } );
  85. it( 'should not convert if img is already consumed', () => {
  86. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  87. const img = data.input.getChild( 0 );
  88. consumable.consume( img, { name: true } );
  89. }, { priority: 'high' } );
  90. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  91. expect( getModelData( document, { withoutSelection: true } ) )
  92. .to.equal( '' );
  93. } );
  94. it( 'should not convert if figure is already consumed', () => {
  95. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  96. const figure = data.input;
  97. consumable.consume( figure, { name: true, class: 'image' } );
  98. }, { priority: 'high' } );
  99. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  100. expect( getModelData( document, { withoutSelection: true } ) )
  101. .to.equal( '' );
  102. } );
  103. it( 'should dispatch conversion for nested elements', () => {
  104. const conversionSpy = sinon.spy();
  105. editor.data.viewToModel.on( 'element:figcaption', conversionSpy );
  106. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /><figcaption></figcaption></figure>' );
  107. sinon.assert.calledOnce( conversionSpy );
  108. } );
  109. } );
  110. } );
  111. describe( 'conversion in editing pipeline', () => {
  112. describe( 'model to view', () => {
  113. it( 'should convert', () => {
  114. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  115. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  116. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  117. } );
  118. it( 'converted element should be widgetized', () => {
  119. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  120. const figure = viewDocument.getRoot().getChild( 0 );
  121. expect( figure.name ).to.equal( 'figure' );
  122. expect( isImageWidget( figure ) ).to.be.true;
  123. } );
  124. it( 'should convert attribute change', () => {
  125. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  126. const image = document.getRoot().getChild( 0 );
  127. document.enqueueChanges( () => {
  128. const batch = document.batch();
  129. batch.setAttribute( image, 'alt', 'new text' );
  130. } );
  131. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  132. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>' );
  133. } );
  134. it( 'should convert attribute removal', () => {
  135. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  136. const image = document.getRoot().getChild( 0 );
  137. document.enqueueChanges( () => {
  138. const batch = document.batch();
  139. batch.removeAttribute( image, 'alt' );
  140. } );
  141. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  142. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>' );
  143. } );
  144. it( 'should not convert change if is already consumed', () => {
  145. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  146. const image = document.getRoot().getChild( 0 );
  147. editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => {
  148. consumable.consume( data.item, 'removeAttribute:alt' );
  149. }, { priority: 'high' } );
  150. document.enqueueChanges( () => {
  151. const batch = document.batch();
  152. batch.removeAttribute( image, 'alt' );
  153. } );
  154. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  155. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  156. } );
  157. } );
  158. } );
  159. describe( 'selection conversion', () => {
  160. it( 'should convert selection', () => {
  161. setModelData( document, '[<image alt="alt text" src="foo.png"></image>]' );
  162. expect( getViewData( viewDocument ) ).to.equal(
  163. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  164. '<img alt="alt text" src="foo.png"></img>' +
  165. '</figure>]'
  166. );
  167. expect( viewDocument.selection.isFake ).to.be.true;
  168. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'alt text image widget' );
  169. } );
  170. it( 'should create proper fake selection label when alt attribute is empty', () => {
  171. setModelData( document, '[<image src="foo.png" alt=""></image>]' );
  172. expect( getViewData( viewDocument ) ).to.equal(
  173. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  174. '<img alt="" src="foo.png"></img>' +
  175. '</figure>]'
  176. );
  177. expect( viewDocument.selection.isFake ).to.be.true;
  178. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  179. } );
  180. it( 'should remove selected class from previously selected element', () => {
  181. setModelData( document,
  182. '[<image src="foo.png" alt="alt text"></image>]' +
  183. '<image src="foo.png" alt="alt text"></image>'
  184. );
  185. expect( getViewData( viewDocument ) ).to.equal(
  186. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  187. '<img alt="alt text" src="foo.png"></img>' +
  188. '</figure>]' +
  189. '<figure class="image ck-widget" contenteditable="false">' +
  190. '<img alt="alt text" src="foo.png"></img>' +
  191. '</figure>'
  192. );
  193. document.enqueueChanges( () => {
  194. const secondImage = document.getRoot().getChild( 1 );
  195. document.selection.setRanges( [ ModelRange.createOn( secondImage ) ] );
  196. } );
  197. expect( getViewData( viewDocument ) ).to.equal(
  198. '<figure class="image ck-widget" contenteditable="false">' +
  199. '<img alt="alt text" src="foo.png"></img>' +
  200. '</figure>' +
  201. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  202. '<img alt="alt text" src="foo.png"></img>' +
  203. '</figure>]'
  204. );
  205. } );
  206. } );
  207. } );