8
0

imageengine.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. } );
  37. describe( 'view to model', () => {
  38. it( 'should convert image figure', () => {
  39. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  40. expect( getModelData( document, { withoutSelection: true } ) )
  41. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  42. } );
  43. it( 'should not convert if there is no image class', () => {
  44. editor.setData( '<figure><img src="foo.png" alt="alt text" /></figure>' );
  45. expect( getModelData( document, { withoutSelection: true } ) )
  46. .to.equal( '' );
  47. } );
  48. it( 'should not convert if there is no img inside #1', () => {
  49. editor.setData( '<figure class="image"></figure>' );
  50. expect( getModelData( document, { withoutSelection: true } ) )
  51. .to.equal( '' );
  52. } );
  53. it( 'should not convert if there is no img inside #2', () => {
  54. editor.setData( '<figure class="image">test</figure>' );
  55. expect( getModelData( document, { withoutSelection: true } ) )
  56. .to.equal( '' );
  57. } );
  58. it( 'should not convert without alt attribute', () => {
  59. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  60. expect( getModelData( document, { withoutSelection: true } ) )
  61. .to.equal( '' );
  62. } );
  63. it( 'should not convert without src attribute', () => {
  64. editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
  65. expect( getModelData( document, { withoutSelection: true } ) )
  66. .to.equal( '' );
  67. } );
  68. it( 'should not convert in wrong context', () => {
  69. const data = editor.data;
  70. const editing = editor.editing;
  71. document.schema.registerItem( 'div', '$block' );
  72. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  73. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  74. document.schema.disallow( { name: 'image', inside: 'div' } );
  75. editor.setData( '<div><figure class="image"><img src="foo.png" alt="alt text" /></figure></div>' );
  76. expect( getModelData( document, { withoutSelection: true } ) )
  77. .to.equal( '<div></div>' );
  78. } );
  79. it( 'should not convert if img is already consumed', () => {
  80. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  81. const img = data.input.getChild( 0 );
  82. consumable.consume( img, { name: true } );
  83. }, { priority: 'high' } );
  84. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  85. expect( getModelData( document, { withoutSelection: true } ) )
  86. .to.equal( '' );
  87. } );
  88. it( 'should not convert if figure is already consumed', () => {
  89. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  90. const figure = data.input;
  91. consumable.consume( figure, { name: true, class: 'image' } );
  92. }, { priority: 'high' } );
  93. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  94. expect( getModelData( document, { withoutSelection: true } ) )
  95. .to.equal( '' );
  96. } );
  97. } );
  98. } );
  99. describe( 'conversion in editing pipeline', () => {
  100. describe( 'model to view', () => {
  101. it( 'should convert', () => {
  102. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  103. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  104. .to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
  105. } );
  106. it( 'should widgetize element', () => {
  107. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  108. const figure = viewDocument.getRoot().getChild( 0 );
  109. expect( figure.name ).to.equal( 'figure' );
  110. expect( figure.isWidget ).to.be.true;
  111. expect( figure.getFillerOffset() ).to.be.null;
  112. expect( figure.hasClass( 'ck-widget' ) ).to.be.true;
  113. } );
  114. } );
  115. } );
  116. describe( 'selection conversion', () => {
  117. it( 'should convert selection', () => {
  118. setModelData( document, '[<image alt="alt text" src="foo.png"></image>]' );
  119. expect( getViewData( viewDocument ) ).to.equal(
  120. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  121. '<img alt="alt text" src="foo.png"></img>' +
  122. '</figure>]'
  123. );
  124. expect( viewDocument.selection.isFake ).to.be.true;
  125. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'alt text image widget' );
  126. } );
  127. it( 'should create proper fake selection label when alt attribute is empty', () => {
  128. setModelData( document, '[<image src="foo.png" alt=""></image>]' );
  129. expect( getViewData( viewDocument ) ).to.equal(
  130. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  131. '<img alt="" src="foo.png"></img>' +
  132. '</figure>]'
  133. );
  134. expect( viewDocument.selection.isFake ).to.be.true;
  135. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  136. } );
  137. it( 'should remove selected class from previously selected element', () => {
  138. setModelData( document,
  139. '[<image src="foo.png" alt="alt text"></image>]' +
  140. '<image src="foo.png" alt="alt text"></image>'
  141. );
  142. expect( getViewData( viewDocument ) ).to.equal(
  143. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  144. '<img alt="alt text" src="foo.png"></img>' +
  145. '</figure>]' +
  146. '<figure class="image ck-widget" contenteditable="false">' +
  147. '<img alt="alt text" src="foo.png"></img>' +
  148. '</figure>'
  149. );
  150. document.enqueueChanges( () => {
  151. const secondImage = document.getRoot().getChild( 1 );
  152. document.selection.setRanges( [ ModelRange.createOn( secondImage ) ] );
  153. } );
  154. expect( getViewData( viewDocument ) ).to.equal(
  155. '<figure class="image ck-widget" contenteditable="false">' +
  156. '<img alt="alt text" src="foo.png"></img>' +
  157. '</figure>' +
  158. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  159. '<img alt="alt text" src="foo.png"></img>' +
  160. '</figure>]'
  161. );
  162. } );
  163. } );
  164. } );