8
0

imageengine.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. } );
  41. describe( 'view to model', () => {
  42. it( 'should convert image figure', () => {
  43. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  44. expect( getModelData( document, { withoutSelection: true } ) )
  45. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  46. } );
  47. it( 'should not convert if there is no image class', () => {
  48. editor.setData( '<figure><img src="foo.png" alt="alt text" /></figure>' );
  49. expect( getModelData( document, { withoutSelection: true } ) )
  50. .to.equal( '' );
  51. } );
  52. it( 'should not convert if there is no img inside #1', () => {
  53. editor.setData( '<figure class="image"></figure>' );
  54. expect( getModelData( document, { withoutSelection: true } ) )
  55. .to.equal( '' );
  56. } );
  57. it( 'should not convert if there is no img inside #2', () => {
  58. editor.setData( '<figure class="image">test</figure>' );
  59. expect( getModelData( document, { withoutSelection: true } ) )
  60. .to.equal( '' );
  61. } );
  62. it( 'should convert without alt attribute', () => {
  63. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  64. expect( getModelData( document, { withoutSelection: true } ) )
  65. .to.equal( '<image src="foo.png"></image>' );
  66. } );
  67. it( 'should not convert without src attribute', () => {
  68. editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
  69. expect( getModelData( document, { withoutSelection: true } ) )
  70. .to.equal( '' );
  71. } );
  72. it( 'should not convert in wrong context', () => {
  73. const data = editor.data;
  74. const editing = editor.editing;
  75. document.schema.registerItem( 'div', '$block' );
  76. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  77. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  78. document.schema.disallow( { name: 'image', inside: 'div' } );
  79. editor.setData( '<div><figure class="image"><img src="foo.png" alt="alt text" /></figure></div>' );
  80. expect( getModelData( document, { withoutSelection: true } ) )
  81. .to.equal( '<div></div>' );
  82. } );
  83. it( 'should not convert if img is already consumed', () => {
  84. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  85. const img = data.input.getChild( 0 );
  86. consumable.consume( img, { name: true } );
  87. }, { priority: 'high' } );
  88. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  89. expect( getModelData( document, { withoutSelection: true } ) )
  90. .to.equal( '' );
  91. } );
  92. it( 'should not convert if figure is already consumed', () => {
  93. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  94. const figure = data.input;
  95. consumable.consume( figure, { name: true, class: 'image' } );
  96. }, { priority: 'high' } );
  97. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  98. expect( getModelData( document, { withoutSelection: true } ) )
  99. .to.equal( '' );
  100. } );
  101. } );
  102. } );
  103. describe( 'conversion in editing pipeline', () => {
  104. describe( 'model to view', () => {
  105. it( 'should convert', () => {
  106. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  107. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  108. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  109. } );
  110. it( 'should widgetize element', () => {
  111. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  112. const figure = viewDocument.getRoot().getChild( 0 );
  113. expect( figure.name ).to.equal( 'figure' );
  114. expect( figure.isWidget ).to.be.true;
  115. expect( figure.getFillerOffset() ).to.be.null;
  116. expect( figure.hasClass( 'ck-widget' ) ).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. } );