imageengine.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/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/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. } );
  104. } );
  105. describe( 'conversion in editing pipeline', () => {
  106. describe( 'model to view', () => {
  107. it( 'should convert', () => {
  108. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  109. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  110. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  111. } );
  112. it( 'converted element should be widgetized', () => {
  113. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  114. const figure = viewDocument.getRoot().getChild( 0 );
  115. expect( figure.name ).to.equal( 'figure' );
  116. expect( isImageWidget( figure ) ).to.be.true;
  117. } );
  118. it( 'should convert attribute change', () => {
  119. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  120. const image = document.getRoot().getChild( 0 );
  121. document.enqueueChanges( () => {
  122. const batch = document.batch();
  123. batch.setAttribute( image, 'alt', 'new text' );
  124. } );
  125. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  126. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>' );
  127. } );
  128. it( 'should convert attribute removal', () => {
  129. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  130. const image = document.getRoot().getChild( 0 );
  131. document.enqueueChanges( () => {
  132. const batch = document.batch();
  133. batch.removeAttribute( image, 'alt' );
  134. } );
  135. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  136. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>' );
  137. } );
  138. it( 'should not convert change if is already consumed', () => {
  139. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  140. const image = document.getRoot().getChild( 0 );
  141. editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => {
  142. consumable.consume( data.item, 'removeAttribute:alt' );
  143. }, { priority: 'high' } );
  144. document.enqueueChanges( () => {
  145. const batch = document.batch();
  146. batch.removeAttribute( image, 'alt' );
  147. } );
  148. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  149. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  150. } );
  151. } );
  152. } );
  153. describe( 'selection conversion', () => {
  154. it( 'should convert selection', () => {
  155. setModelData( document, '[<image alt="alt text" src="foo.png"></image>]' );
  156. expect( getViewData( viewDocument ) ).to.equal(
  157. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  158. '<img alt="alt text" src="foo.png"></img>' +
  159. '</figure>]'
  160. );
  161. expect( viewDocument.selection.isFake ).to.be.true;
  162. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'alt text image widget' );
  163. } );
  164. it( 'should create proper fake selection label when alt attribute is empty', () => {
  165. setModelData( document, '[<image src="foo.png" alt=""></image>]' );
  166. expect( getViewData( viewDocument ) ).to.equal(
  167. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  168. '<img alt="" src="foo.png"></img>' +
  169. '</figure>]'
  170. );
  171. expect( viewDocument.selection.isFake ).to.be.true;
  172. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  173. } );
  174. it( 'should remove selected class from previously selected element', () => {
  175. setModelData( document,
  176. '[<image src="foo.png" alt="alt text"></image>]' +
  177. '<image src="foo.png" alt="alt text"></image>'
  178. );
  179. expect( getViewData( viewDocument ) ).to.equal(
  180. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  181. '<img alt="alt text" src="foo.png"></img>' +
  182. '</figure>]' +
  183. '<figure class="image ck-widget" contenteditable="false">' +
  184. '<img alt="alt text" src="foo.png"></img>' +
  185. '</figure>'
  186. );
  187. document.enqueueChanges( () => {
  188. const secondImage = document.getRoot().getChild( 1 );
  189. document.selection.setRanges( [ ModelRange.createOn( secondImage ) ] );
  190. } );
  191. expect( getViewData( viewDocument ) ).to.equal(
  192. '<figure class="image ck-widget" contenteditable="false">' +
  193. '<img alt="alt text" src="foo.png"></img>' +
  194. '</figure>' +
  195. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  196. '<img alt="alt text" src="foo.png"></img>' +
  197. '</figure>]'
  198. );
  199. } );
  200. } );
  201. } );