imagecaptionengine.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. expect( document.schema.limits.has( 'caption' ) );
  43. } );
  44. describe( 'data pipeline', () => {
  45. describe( 'view to model', () => {
  46. it( 'should convert figcaption inside image figure', () => {
  47. editor.setData( '<figure class="image"><img src="foo.png"/><figcaption>foo bar</figcaption></figure>' );
  48. expect( getModelData( document, { withoutSelection: true } ) )
  49. .to.equal( '<image src="foo.png"><caption>foo bar</caption></image>' );
  50. } );
  51. it( 'should add empty caption if there is no figcaption', () => {
  52. editor.setData( '<figure class="image"><img src="foo.png"/></figure>' );
  53. expect( getModelData( document, { withoutSelection: true } ) )
  54. .to.equal( '<image src="foo.png"><caption></caption></image>' );
  55. } );
  56. it( 'should not convert figcaption inside other elements than image', () => {
  57. editor.setData( '<widget><figcaption>foobar</figcaption></widget>' );
  58. expect( getModelData( document, { withoutSelection: true } ) )
  59. .to.equal( '<widget>foobar</widget>' );
  60. } );
  61. } );
  62. describe( 'model to view', () => {
  63. it( 'should convert caption element to figcaption', () => {
  64. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  65. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"><figcaption>Foo bar baz.</figcaption></figure>' );
  66. } );
  67. it( 'should not convert caption if it\'s empty', () => {
  68. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  69. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"></figure>' );
  70. } );
  71. it( 'should not convert caption from other elements', () => {
  72. setModelData( document, '<widget>foo bar<caption></caption></widget>' );
  73. expect( editor.getData() ).to.equal( '<widget>foo bar</widget>' );
  74. } );
  75. } );
  76. } );
  77. describe( 'editing pipeline', () => {
  78. describe( 'model to view', () => {
  79. it( 'should convert caption element to figcaption contenteditable', () => {
  80. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  81. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  82. '<figure class="image ck-widget" contenteditable="false">' +
  83. '<img src="img.png"></img>' +
  84. '<figcaption contenteditable="true">Foo bar baz.</figcaption>' +
  85. '</figure>'
  86. );
  87. } );
  88. it( 'should not convert caption if it\'s empty', () => {
  89. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  90. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  91. '<figure class="image ck-widget" contenteditable="false"><img src="img.png"></img></figure>'
  92. );
  93. } );
  94. it( 'should not convert caption from other elements', () => {
  95. setModelData( document, '<widget>foo bar<caption></caption></widget>' );
  96. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  97. } );
  98. it( 'should not convert when element is already consumed', () => {
  99. editor.editing.modelToView.on(
  100. 'insert:caption',
  101. ( evt, data, consumable, conversionApi ) => {
  102. consumable.consume( data.item, 'insert' );
  103. const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
  104. const viewElement = new ViewAttributeElement( 'span' );
  105. const viewPosition = ViewPosition.createAt( imageFigure, 'end' );
  106. conversionApi.mapper.bindElements( data.item, viewElement );
  107. viewWriter.insert( viewPosition, viewElement );
  108. },
  109. { priority: 'high' }
  110. );
  111. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  112. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  113. '<figure class="image ck-widget" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
  114. );
  115. } );
  116. } );
  117. } );
  118. describe( 'inserting image to document', () => {
  119. it( 'should add caption element if image does not have it', () => {
  120. const image = new ModelElement( 'image', { src: '', alt: '' } );
  121. const batch = document.batch();
  122. document.enqueueChanges( () => {
  123. batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
  124. } );
  125. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  126. '<image alt="" src=""><caption></caption></image>'
  127. );
  128. } );
  129. it( 'should not add caption element if image does not have it', () => {
  130. const caption = new ModelElement( 'caption', null, 'foo bar' );
  131. const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
  132. const batch = document.batch();
  133. document.enqueueChanges( () => {
  134. batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
  135. } );
  136. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  137. '<image alt="" src=""><caption>foo bar</caption></image>'
  138. );
  139. } );
  140. it( 'should do nothing for other changes than insert', () => {
  141. setModelData( document, '<image src=""><caption>foo bar</caption></image>' );
  142. const image = document.getRoot().getChild( 0 );
  143. const batch = document.batch();
  144. document.enqueueChanges( () => {
  145. batch.setAttribute( image, 'alt', 'alt text' );
  146. } );
  147. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  148. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  149. );
  150. } );
  151. } );
  152. describe( 'editing view', () => {
  153. it( 'image should have empty figcaption element when is selected', () => {
  154. setModelData( document, '[<image src=""><caption></caption></image>]' );
  155. expect( getViewData( viewDocument ) ).to.equal(
  156. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  157. '<img src=""></img>' +
  158. '<figcaption contenteditable="true"></figcaption>' +
  159. '</figure>]'
  160. );
  161. } );
  162. it( 'image should not have empty figcaption element when is not selected', () => {
  163. setModelData( document, '[]<image src=""><caption></caption></image>' );
  164. expect( getViewData( viewDocument ) ).to.equal(
  165. '[]<figure class="image ck-widget" contenteditable="false">' +
  166. '<img src=""></img>' +
  167. '</figure>'
  168. );
  169. } );
  170. it( 'should not add additional figcaption if one is already present', () => {
  171. setModelData( document, '[<image src=""><caption>foo bar</caption></image>]' );
  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">foo bar</figcaption>' +
  176. '</figure>]'
  177. );
  178. } );
  179. it( 'should remove figcaption when caption is empty and image is no longer selected', () => {
  180. setModelData( document, '[<image src=""><caption></caption></image>]' );
  181. document.enqueueChanges( () => {
  182. document.selection.removeAllRanges();
  183. } );
  184. expect( getViewData( viewDocument ) ).to.equal(
  185. '[]<figure class="image ck-widget" contenteditable="false">' +
  186. '<img src=""></img>' +
  187. '</figure>'
  188. );
  189. } );
  190. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  191. setModelData( document, '<image src=""><caption>[foo bar]</caption></image>' );
  192. document.enqueueChanges( () => {
  193. document.batch().remove( document.selection.getFirstRange() );
  194. } );
  195. expect( getViewData( viewDocument ) ).to.equal(
  196. '<figure class="image ck-widget" contenteditable="false">' +
  197. '<img src=""></img>' +
  198. '<figcaption contenteditable="true">[]</figcaption>' +
  199. '</figure>'
  200. );
  201. } );
  202. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  203. setModelData( document, '<image src=""><caption>[foo bar]</caption></image>' );
  204. const image = document.getRoot().getChild( 0 );
  205. document.enqueueChanges( () => {
  206. document.batch().remove( document.selection.getFirstRange() );
  207. document.selection.setRanges( [ ModelRange.createOn( image ) ] );
  208. } );
  209. expect( getViewData( viewDocument ) ).to.equal(
  210. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  211. '<img src=""></img>' +
  212. '<figcaption contenteditable="true"></figcaption>' +
  213. '</figure>]'
  214. );
  215. } );
  216. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  217. setModelData( document, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  218. const image = document.getRoot().getChild( 1 );
  219. document.enqueueChanges( () => {
  220. document.selection.setRanges( [ ModelRange.createOn( image ) ] );
  221. } );
  222. expect( getViewData( viewDocument ) ).to.equal(
  223. '<figure class="image ck-widget" contenteditable="false">' +
  224. '<img src=""></img>' +
  225. '<figcaption contenteditable="true">foo bar</figcaption>' +
  226. '</figure>' +
  227. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  228. '<img src=""></img>' +
  229. '<figcaption contenteditable="true"></figcaption>' +
  230. '</figure>]'
  231. );
  232. } );
  233. } );
  234. } );