8
0

imageengine.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
  6. import ImageEngine from 'ckeditor5/image/imageengine.js';
  7. import { getData as getModelData, setData as setModelData } from 'ckeditor5/engine/dev-utils/model.js';
  8. import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js';
  9. import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.js';
  10. import buildModelConverter from 'ckeditor5/engine/conversion/buildmodelconverter.js';
  11. import ModelRange from 'ckeditor5/engine/model/range.js';
  12. describe( `ImageEngine`, () => {
  13. let editor, document, viewDocument;
  14. beforeEach( () => {
  15. return VirtualTestEditor.create( {
  16. features: [ 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' ] } ) ).to.be.true;
  29. } );
  30. describe( 'conversion in data pipeline', () => {
  31. describe( 'model to view', () => {
  32. it( 'should convert', () => {
  33. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  34. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png" alt="alt text"></figure>' );
  35. } );
  36. it( 'should convert without alt attribute', () => {
  37. setModelData( document, '<image src="foo.png"></image>' );
  38. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  39. } );
  40. it( 'should convert without src attribute', () => {
  41. setModelData( document, '<image alt="alt text"></image>' );
  42. expect( editor.getData() ).to.equal( '<figure class="image"><img alt="alt text"></figure>' );
  43. } );
  44. } );
  45. describe( 'view to model', () => {
  46. it( 'should convert image figure', () => {
  47. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  48. expect( getModelData( document, { withoutSelection: true } ) )
  49. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  50. } );
  51. it( 'should not convert if there is no image class', () => {
  52. editor.setData( '<figure><img src="foo.png" alt="alt text" /></figure>' );
  53. expect( getModelData( document, { withoutSelection: true } ) )
  54. .to.equal( '' );
  55. } );
  56. it( 'should not convert if there is no img inside #1', () => {
  57. editor.setData( '<figure class="image"></figure>' );
  58. expect( getModelData( document, { withoutSelection: true } ) )
  59. .to.equal( '' );
  60. } );
  61. it( 'should not convert if there is no img inside #2', () => {
  62. editor.setData( '<figure class="image">test</figure>' );
  63. expect( getModelData( document, { withoutSelection: true } ) )
  64. .to.equal( '' );
  65. } );
  66. it( 'should convert without alt attribute', () => {
  67. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  68. expect( getModelData( document, { withoutSelection: true } ) )
  69. .to.equal( '<image src="foo.png"></image>' );
  70. } );
  71. it( 'should convert without src attribute', () => {
  72. editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
  73. expect( getModelData( document, { withoutSelection: true } ) )
  74. .to.equal( '<image alt="alt text"></image>' );
  75. } );
  76. it( 'should not convert in wrong context', () => {
  77. const data = editor.data;
  78. const editing = editor.editing;
  79. document.schema.registerItem( 'div', '$block' );
  80. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  81. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  82. document.schema.disallow( { name: 'image', inside: 'div' } );
  83. editor.setData( '<div><figure class="image"><img src="foo.png" alt="alt text" /></figure></div>' );
  84. expect( getModelData( document, { withoutSelection: true } ) )
  85. .to.equal( '<div></div>' );
  86. } );
  87. it( 'should not convert if img is already consumed', () => {
  88. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  89. const img = data.input.getChild( 0 );
  90. consumable.consume( img, { name: true } );
  91. }, { priority: 'high' } );
  92. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  93. expect( getModelData( document, { withoutSelection: true } ) )
  94. .to.equal( '' );
  95. } );
  96. it( 'should not convert if figure is already consumed', () => {
  97. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  98. const figure = data.input;
  99. consumable.consume( figure, { name: true, class: 'image' } );
  100. }, { priority: 'high' } );
  101. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  102. expect( getModelData( document, { withoutSelection: true } ) )
  103. .to.equal( '' );
  104. } );
  105. } );
  106. } );
  107. describe( 'conversion in editing pipeline', () => {
  108. describe( 'model to view', () => {
  109. it( 'should convert', () => {
  110. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  111. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  112. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  113. } );
  114. it( 'should convert without alt attribute', () => {
  115. setModelData( document, '<image src="foo.png"></image>' );
  116. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  117. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>' );
  118. } );
  119. it( 'should convert without src attribute', () => {
  120. setModelData( document, '<image alt="alt text"></image>' );
  121. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  122. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text"></img></figure>' );
  123. } );
  124. it( 'should widgetize element', () => {
  125. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  126. const figure = viewDocument.getRoot().getChild( 0 );
  127. expect( figure.name ).to.equal( 'figure' );
  128. expect( figure.isWidget ).to.be.true;
  129. expect( figure.getFillerOffset() ).to.be.null;
  130. expect( figure.hasClass( 'ck-widget' ) ).to.be.true;
  131. } );
  132. } );
  133. } );
  134. describe( 'selection conversion', () => {
  135. it( 'should convert selection', () => {
  136. setModelData( document, '[<image alt="alt text" src="foo.png"></image>]' );
  137. expect( getViewData( viewDocument ) ).to.equal(
  138. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  139. '<img alt="alt text" src="foo.png"></img>' +
  140. '</figure>]'
  141. );
  142. expect( viewDocument.selection.isFake ).to.be.true;
  143. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'alt text image widget' );
  144. } );
  145. it( 'should create proper fake selection label when alt attribute is not present', () => {
  146. setModelData( document, '[<image src="foo.png"></image>]' );
  147. expect( getViewData( viewDocument ) ).to.equal(
  148. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  149. '<img src="foo.png"></img>' +
  150. '</figure>]'
  151. );
  152. expect( viewDocument.selection.isFake ).to.be.true;
  153. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  154. } );
  155. it( 'should remove selected class from previously selected element', () => {
  156. setModelData( document,
  157. '[<image src="foo.png" alt="alt text"></image>]' +
  158. '<image src="foo.png" alt="alt text"></image>'
  159. );
  160. expect( getViewData( viewDocument ) ).to.equal(
  161. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  162. '<img alt="alt text" src="foo.png"></img>' +
  163. '</figure>]' +
  164. '<figure class="image ck-widget" contenteditable="false">' +
  165. '<img alt="alt text" src="foo.png"></img>' +
  166. '</figure>'
  167. );
  168. document.enqueueChanges( () => {
  169. const secondImage = document.getRoot().getChild( 1 );
  170. document.selection.setRanges( [ ModelRange.createOn( secondImage ) ] );
  171. } );
  172. expect( getViewData( viewDocument ) ).to.equal(
  173. '<figure class="image ck-widget" contenteditable="false">' +
  174. '<img alt="alt text" src="foo.png"></img>' +
  175. '</figure>' +
  176. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  177. '<img alt="alt text" src="foo.png"></img>' +
  178. '</figure>]'
  179. );
  180. } );
  181. } );
  182. } );