imagecaptionengine.js 11 KB

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