imagecaptioningengine.js 10 KB

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