imageengine.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from 'ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import ImageEngine from 'ckeditor5-image/src/imageengine';
  7. import { getData as getModelData, setData as setModelData } from 'ckeditor5-engine/src/dev-utils/model';
  8. import { getData as getViewData } from 'ckeditor5-engine/src/dev-utils/view';
  9. import buildViewConverter from 'ckeditor5-engine/src/conversion/buildviewconverter';
  10. import buildModelConverter from 'ckeditor5-engine/src/conversion/buildmodelconverter';
  11. import { isImageWidget } from 'ckeditor5-image/src/utils';
  12. import ModelRange from '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 src="foo.png" alt="alt text"></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. } );
  119. } );
  120. describe( 'selection conversion', () => {
  121. it( 'should convert selection', () => {
  122. setModelData( document, '[<image alt="alt text" src="foo.png"></image>]' );
  123. expect( getViewData( viewDocument ) ).to.equal(
  124. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  125. '<img alt="alt text" src="foo.png"></img>' +
  126. '</figure>]'
  127. );
  128. expect( viewDocument.selection.isFake ).to.be.true;
  129. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'alt text image widget' );
  130. } );
  131. it( 'should create proper fake selection label when alt attribute is empty', () => {
  132. setModelData( document, '[<image src="foo.png" alt=""></image>]' );
  133. expect( getViewData( viewDocument ) ).to.equal(
  134. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  135. '<img alt="" src="foo.png"></img>' +
  136. '</figure>]'
  137. );
  138. expect( viewDocument.selection.isFake ).to.be.true;
  139. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  140. } );
  141. it( 'should remove selected class from previously selected element', () => {
  142. setModelData( document,
  143. '[<image src="foo.png" alt="alt text"></image>]' +
  144. '<image src="foo.png" alt="alt text"></image>'
  145. );
  146. expect( getViewData( viewDocument ) ).to.equal(
  147. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  148. '<img alt="alt text" src="foo.png"></img>' +
  149. '</figure>]' +
  150. '<figure class="image ck-widget" contenteditable="false">' +
  151. '<img alt="alt text" src="foo.png"></img>' +
  152. '</figure>'
  153. );
  154. document.enqueueChanges( () => {
  155. const secondImage = document.getRoot().getChild( 1 );
  156. document.selection.setRanges( [ ModelRange.createOn( secondImage ) ] );
  157. } );
  158. expect( getViewData( viewDocument ) ).to.equal(
  159. '<figure class="image ck-widget" contenteditable="false">' +
  160. '<img alt="alt text" src="foo.png"></img>' +
  161. '</figure>' +
  162. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  163. '<img alt="alt text" src="foo.png"></img>' +
  164. '</figure>]'
  165. );
  166. } );
  167. } );
  168. } );