8
0

imagecaptioningengine.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  7. import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
  8. import ImageCaptioningEngine from '../../src/imagecaptioning/imagecaptioningengine';
  9. import ImageEngine from '../../src/imageengine';
  10. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  12. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  13. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  14. describe( 'ImageCaptioningEngine', () => {
  15. let editor, document, viewDocument;
  16. beforeEach( () => {
  17. return VirtualTestEditor.create( {
  18. plugins: [ ImageCaptioningEngine, ImageEngine ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. document = editor.document;
  23. viewDocument = editor.editing.view;
  24. document.schema.registerItem( 'widget' );
  25. document.schema.allow( { name: 'widget', inside: '$root' } );
  26. document.schema.allow( { name: 'caption', inside: 'widget' } );
  27. document.schema.allow( { name: '$inline', inside: 'widget' } );
  28. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'widget' ).toElement( 'widget' );
  29. buildModelConverter().for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'widget' ).toElement( 'widget' );
  30. } );
  31. } );
  32. it( 'should be loaded', () => {
  33. expect( editor.plugins.get( ImageCaptioningEngine ) ).to.be.instanceOf( ImageCaptioningEngine );
  34. } );
  35. it( 'should set proper schema rules', () => {
  36. expect( document.schema.check( { name: 'caption', iniside: 'image' } ) ).to.be.true;
  37. expect( document.schema.check( { name: '$inline', inside: 'caption' } ) ).to.be.true;
  38. } );
  39. describe( 'data pipeline', () => {
  40. describe( 'view to model', () => {
  41. it( 'should convert figcaption inside image figure', () => {
  42. editor.setData( '<figure class="image"><img src="foo.png"/><figcaption>foo bar</figcaption></figure>' );
  43. expect( getModelData( document, { withoutSelection: true } ) )
  44. .to.equal( '<image src="foo.png"><caption>foo bar</caption></image>' );
  45. } );
  46. it( 'should add empty caption if there is no figcaption', () => {
  47. editor.setData( '<figure class="image"><img src="foo.png"/></figure>' );
  48. expect( getModelData( document, { withoutSelection: true } ) )
  49. .to.equal( '<image src="foo.png"><caption></caption></image>' );
  50. } );
  51. it( 'should not convert figcaption inside other elements than image', () => {
  52. editor.setData( '<widget><figcaption>foobar</figcaption></widget>' );
  53. expect( getModelData( document, { withoutSelection: true } ) )
  54. .to.equal( '<widget>foobar</widget>' );
  55. } );
  56. } );
  57. describe( 'model to view', () => {
  58. it( 'should convert caption element to figcaption', () => {
  59. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  60. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"><figcaption>Foo bar baz.</figcaption></figure>' );
  61. } );
  62. it( 'should not convert caption if it\'s empty', () => {
  63. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  64. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"></figure>' );
  65. } );
  66. it( 'should not convert caption from other elements', () => {
  67. setModelData( document, '<widget>foo bar<caption></caption></widget>' );
  68. expect( editor.getData() ).to.equal( '<widget>foo bar</widget>' );
  69. } );
  70. } );
  71. } );
  72. describe( 'editing pipeline', () => {
  73. describe( 'model to view', () => {
  74. it( 'should convert caption element to figcaption contenteditable', () => {
  75. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  76. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  77. '<figure class="image ck-widget" contenteditable="false">' +
  78. '<img src="img.png"></img>' +
  79. '<figcaption contenteditable="true">Foo bar baz.</figcaption>' +
  80. '</figure>'
  81. );
  82. } );
  83. it( 'should not convert caption if it\'s empty', () => {
  84. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  85. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  86. '<figure class="image ck-widget" contenteditable="false"><img src="img.png"></img></figure>'
  87. );
  88. } );
  89. it( 'should not convert caption from other elements', () => {
  90. setModelData( document, '<widget>foo bar<caption></caption></widget>' );
  91. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  92. } );
  93. } );
  94. } );
  95. describe( 'inserting image to document', () => {
  96. it( 'should add caption element if image does not have it', () => {
  97. const image = new ModelElement( 'image', { src: '', alt: '' } );
  98. const batch = document.batch();
  99. document.enqueueChanges( () => {
  100. batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
  101. } );
  102. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  103. '<image alt="" src=""><caption></caption></image>'
  104. );
  105. } );
  106. it( 'should not add caption element if image does not have it', () => {
  107. const caption = new ModelElement( 'caption', null, 'foo bar' );
  108. const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
  109. const batch = document.batch();
  110. document.enqueueChanges( () => {
  111. batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
  112. } );
  113. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  114. '<image alt="" src=""><caption>foo bar</caption></image>'
  115. );
  116. } );
  117. it( 'should do nothing for other changes than insert', () => {
  118. setModelData( document, '<image src=""><caption>foo bar</caption></image>' );
  119. const image = document.getRoot().getChild( 0 );
  120. const batch = document.batch();
  121. document.enqueueChanges( () => {
  122. batch.setAttribute( image, 'alt', 'alt text' );
  123. } );
  124. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  125. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  126. );
  127. } );
  128. } );
  129. describe( 'editing view', () => {
  130. it( 'image should have empty figcaption element when is selected', () => {
  131. setModelData( document, '[<image src=""><caption></caption></image>]' );
  132. expect( getViewData( viewDocument ) ).to.equal(
  133. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  134. '<img src=""></img>' +
  135. '<figcaption contenteditable="true"></figcaption>' +
  136. '</figure>]'
  137. );
  138. } );
  139. it( 'image should not have empty figcaption element when is not selected', () => {
  140. setModelData( document, '[]<image src=""><caption></caption></image>' );
  141. expect( getViewData( viewDocument ) ).to.equal(
  142. '[]<figure class="image ck-widget" contenteditable="false">' +
  143. '<img src=""></img>' +
  144. '</figure>'
  145. );
  146. } );
  147. it( 'should not add additional figcaption if one is already present', () => {
  148. setModelData( document, '[<image src=""><caption>foo bar</caption></image>]' );
  149. expect( getViewData( viewDocument ) ).to.equal(
  150. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  151. '<img src=""></img>' +
  152. '<figcaption contenteditable="true">foo bar</figcaption>' +
  153. '</figure>]'
  154. );
  155. } );
  156. it( 'should remove figcaption when caption is empty and image is no longer selected', () => {
  157. setModelData( document, '[<image src=""><caption></caption></image>]' );
  158. document.enqueueChanges( () => {
  159. document.selection.removeAllRanges();
  160. } );
  161. expect( getViewData( viewDocument ) ).to.equal(
  162. '[]<figure class="image ck-widget" contenteditable="false">' +
  163. '<img src=""></img>' +
  164. '</figure>'
  165. );
  166. } );
  167. it( 'should not remove figcatpion when selection is inside it even when it is empty', () => {
  168. setModelData( document, '<image src=""><caption>[foo bar]</caption></image>' );
  169. document.enqueueChanges( () => {
  170. document.batch().remove( document.selection.getFirstRange() );
  171. } );
  172. expect( getViewData( viewDocument ) ).to.equal(
  173. '<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  174. '<img src=""></img>' +
  175. '<figcaption contenteditable="true">[]</figcaption>' +
  176. '</figure>'
  177. );
  178. } );
  179. } );
  180. } );