imageengine.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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: 'div', 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. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.jpg">abc</image>' );
  127. } );
  128. } );
  129. } );
  130. describe( 'conversion in editing pipeline', () => {
  131. describe( 'model to view', () => {
  132. it( 'should convert', () => {
  133. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  134. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  135. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  136. } );
  137. it( 'converted element should be widgetized', () => {
  138. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  139. const figure = viewDocument.getRoot().getChild( 0 );
  140. expect( figure.name ).to.equal( 'figure' );
  141. expect( isImageWidget( figure ) ).to.be.true;
  142. } );
  143. it( 'should convert attribute change', () => {
  144. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  145. const image = document.getRoot().getChild( 0 );
  146. document.enqueueChanges( () => {
  147. const batch = document.batch();
  148. batch.setAttribute( image, 'alt', 'new text' );
  149. } );
  150. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  151. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>' );
  152. } );
  153. it( 'should convert attribute removal', () => {
  154. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  155. const image = document.getRoot().getChild( 0 );
  156. document.enqueueChanges( () => {
  157. const batch = document.batch();
  158. batch.removeAttribute( image, 'alt' );
  159. } );
  160. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  161. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>' );
  162. } );
  163. it( 'should not convert change if is already consumed', () => {
  164. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  165. const image = document.getRoot().getChild( 0 );
  166. editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => {
  167. consumable.consume( data.item, 'removeAttribute:alt' );
  168. }, { priority: 'high' } );
  169. document.enqueueChanges( () => {
  170. const batch = document.batch();
  171. batch.removeAttribute( image, 'alt' );
  172. } );
  173. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  174. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  175. } );
  176. } );
  177. } );
  178. } );