imagecaptionengine.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/image/imageengine';
  14. import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
  15. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  17. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  18. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  19. describe( 'ImageCaptionEngine', () => {
  20. let editor, document, viewDocument;
  21. beforeEach( () => {
  22. return VirtualTestEditor.create( {
  23. plugins: [ ImageCaptionEngine, ImageEngine, UndoEngine ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. document = editor.document;
  28. viewDocument = editor.editing.view;
  29. document.schema.registerItem( 'widget' );
  30. document.schema.allow( { name: 'widget', inside: '$root' } );
  31. document.schema.allow( { name: 'caption', inside: 'widget' } );
  32. document.schema.allow( { name: '$inline', inside: 'widget' } );
  33. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'widget' ).toElement( 'widget' );
  34. buildModelConverter().for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'widget' ).toElement( 'widget' );
  35. } );
  36. } );
  37. it( 'should be loaded', () => {
  38. expect( editor.plugins.get( ImageCaptionEngine ) ).to.be.instanceOf( ImageCaptionEngine );
  39. } );
  40. it( 'should set proper schema rules', () => {
  41. expect( document.schema.check( { name: 'caption', iniside: 'image' } ) ).to.be.true;
  42. expect( document.schema.check( { name: '$inline', inside: 'caption' } ) ).to.be.true;
  43. expect( document.schema.limits.has( 'caption' ) );
  44. } );
  45. describe( 'data pipeline', () => {
  46. describe( 'view to model', () => {
  47. it( 'should convert figcaption inside image figure', () => {
  48. editor.setData( '<figure class="image"><img src="foo.png"/><figcaption>foo bar</figcaption></figure>' );
  49. expect( getModelData( document, { withoutSelection: true } ) )
  50. .to.equal( '<image src="foo.png"><caption>foo bar</caption></image>' );
  51. } );
  52. it( 'should add empty caption if there is no figcaption', () => {
  53. editor.setData( '<figure class="image"><img src="foo.png"/></figure>' );
  54. expect( getModelData( document, { withoutSelection: true } ) )
  55. .to.equal( '<image src="foo.png"><caption></caption></image>' );
  56. } );
  57. it( 'should not convert figcaption inside other elements than image', () => {
  58. editor.setData( '<widget><figcaption>foobar</figcaption></widget>' );
  59. expect( getModelData( document, { withoutSelection: true } ) )
  60. .to.equal( '<widget>foobar</widget>' );
  61. } );
  62. } );
  63. describe( 'model to view', () => {
  64. it( 'should convert caption element to figcaption', () => {
  65. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  66. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"><figcaption>Foo bar baz.</figcaption></figure>' );
  67. } );
  68. it( 'should convert caption to figcaption with hidden class if it\'s empty', () => {
  69. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  70. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"></figure>' );
  71. } );
  72. it( 'should not convert caption from other elements', () => {
  73. setModelData( document, '<widget>foo bar<caption></caption></widget>' );
  74. expect( editor.getData() ).to.equal( '<widget>foo bar</widget>' );
  75. } );
  76. } );
  77. } );
  78. describe( 'editing pipeline', () => {
  79. describe( 'model to view', () => {
  80. it( 'should convert caption element to figcaption contenteditable', () => {
  81. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  82. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  83. '<figure class="image ck-widget" contenteditable="false">' +
  84. '<img src="img.png"></img>' +
  85. '<figcaption class="ck-editable" contenteditable="true">Foo bar baz.</figcaption>' +
  86. '</figure>'
  87. );
  88. } );
  89. it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
  90. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  91. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  92. '<figure class="image ck-widget" contenteditable="false">' +
  93. '<img src="img.png"></img>' +
  94. '<figcaption class="ck-editable ck-hidden" contenteditable="true"></figcaption>' +
  95. '</figure>'
  96. );
  97. } );
  98. it( 'should not convert caption from other elements', () => {
  99. setModelData( document, '<widget>foo bar<caption></caption></widget>' );
  100. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  101. } );
  102. it( 'should not convert when element is already consumed', () => {
  103. editor.editing.modelToView.on(
  104. 'insert:caption',
  105. ( evt, data, consumable, conversionApi ) => {
  106. consumable.consume( data.item, 'insert' );
  107. const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
  108. const viewElement = new ViewAttributeElement( 'span' );
  109. const viewPosition = ViewPosition.createAt( imageFigure, 'end' );
  110. conversionApi.mapper.bindElements( data.item, viewElement );
  111. viewWriter.insert( viewPosition, viewElement );
  112. },
  113. { priority: 'high' }
  114. );
  115. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  116. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  117. '<figure class="image ck-widget" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
  118. );
  119. } );
  120. } );
  121. } );
  122. describe( 'inserting image to document', () => {
  123. it( 'should add caption element if image does not have it', () => {
  124. const image = new ModelElement( 'image', { src: '', alt: '' } );
  125. const batch = document.batch();
  126. document.enqueueChanges( () => {
  127. batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
  128. } );
  129. expect( getModelData( document ) ).to.equal(
  130. '[]<image alt="" src=""><caption></caption></image>'
  131. );
  132. expect( getViewData( viewDocument ) ).to.equal(
  133. '[]<figure class="image ck-widget" contenteditable="false">' +
  134. '<img alt="" src=""></img>' +
  135. '<figcaption class="ck-editable ck-hidden" contenteditable="true"></figcaption>' +
  136. '</figure>'
  137. );
  138. } );
  139. it( 'should not add caption element if image already have it', () => {
  140. const caption = new ModelElement( 'caption', null, 'foo bar' );
  141. const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
  142. const batch = document.batch();
  143. document.enqueueChanges( () => {
  144. batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
  145. } );
  146. expect( getModelData( document ) ).to.equal(
  147. '[]<image alt="" src=""><caption>foo bar</caption></image>'
  148. );
  149. expect( getViewData( viewDocument ) ).to.equal(
  150. '[]<figure class="image ck-widget" contenteditable="false">' +
  151. '<img alt="" src=""></img>' +
  152. '<figcaption class="ck-editable" contenteditable="true">foo bar</figcaption>' +
  153. '</figure>'
  154. );
  155. } );
  156. it( 'should do nothing for other changes than insert', () => {
  157. setModelData( document, '<image src=""><caption>foo bar</caption></image>' );
  158. const image = document.getRoot().getChild( 0 );
  159. const batch = document.batch();
  160. document.enqueueChanges( () => {
  161. batch.setAttribute( image, 'alt', 'alt text' );
  162. } );
  163. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  164. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  165. );
  166. } );
  167. } );
  168. describe( 'editing view', () => {
  169. it( 'image should have empty figcaption element when is selected', () => {
  170. setModelData( document, '[<image src=""><caption></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 class="ck-editable" contenteditable="true"></figcaption>' +
  175. '</figure>]'
  176. );
  177. } );
  178. it( 'image should have empty figcaption element with hidden class when not selected', () => {
  179. setModelData( document, '[]<image src=""><caption></caption></image>' );
  180. expect( getViewData( viewDocument ) ).to.equal(
  181. '[]<figure class="image ck-widget" contenteditable="false">' +
  182. '<img src=""></img>' +
  183. '<figcaption class="ck-editable ck-hidden" contenteditable="true"></figcaption>' +
  184. '</figure>'
  185. );
  186. } );
  187. it( 'should not add additional figcaption if one is already present', () => {
  188. setModelData( document, '[<image src=""><caption>foo bar</caption></image>]' );
  189. expect( getViewData( viewDocument ) ).to.equal(
  190. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  191. '<img src=""></img>' +
  192. '<figcaption class="ck-editable" contenteditable="true">foo bar</figcaption>' +
  193. '</figure>]'
  194. );
  195. } );
  196. it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
  197. setModelData( document, '[<image src=""><caption></caption></image>]' );
  198. document.enqueueChanges( () => {
  199. document.selection.removeAllRanges();
  200. } );
  201. expect( getViewData( viewDocument ) ).to.equal(
  202. '[]<figure class="image ck-widget" contenteditable="false">' +
  203. '<img src=""></img>' +
  204. '<figcaption class="ck-editable ck-hidden" contenteditable="true"></figcaption>' +
  205. '</figure>'
  206. );
  207. } );
  208. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  209. setModelData( document, '<image src=""><caption>[foo bar]</caption></image>' );
  210. document.enqueueChanges( () => {
  211. document.batch().remove( document.selection.getFirstRange() );
  212. } );
  213. expect( getViewData( viewDocument ) ).to.equal(
  214. '<figure class="image ck-widget" contenteditable="false">' +
  215. '<img src=""></img>' +
  216. '<figcaption class="ck-editable" contenteditable="true">[]</figcaption>' +
  217. '</figure>'
  218. );
  219. } );
  220. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  221. setModelData( document, '<image src=""><caption>[foo bar]</caption></image>' );
  222. const image = document.getRoot().getChild( 0 );
  223. document.enqueueChanges( () => {
  224. document.batch().remove( document.selection.getFirstRange() );
  225. document.selection.setRanges( [ ModelRange.createOn( image ) ] );
  226. } );
  227. expect( getViewData( viewDocument ) ).to.equal(
  228. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  229. '<img src=""></img>' +
  230. '<figcaption class="ck-editable" contenteditable="true"></figcaption>' +
  231. '</figure>]'
  232. );
  233. } );
  234. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  235. setModelData( document, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  236. const image = document.getRoot().getChild( 1 );
  237. document.enqueueChanges( () => {
  238. document.selection.setRanges( [ ModelRange.createOn( image ) ] );
  239. } );
  240. expect( getViewData( viewDocument ) ).to.equal(
  241. '<figure class="image ck-widget" contenteditable="false">' +
  242. '<img src=""></img>' +
  243. '<figcaption class="ck-editable" contenteditable="true">foo bar</figcaption>' +
  244. '</figure>' +
  245. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  246. '<img src=""></img>' +
  247. '<figcaption class="ck-editable" contenteditable="true"></figcaption>' +
  248. '</figure>]'
  249. );
  250. } );
  251. describe( 'undo/redo integration', () => {
  252. it( 'should create view element after redo', () => {
  253. setModelData( document, '<image src=""><caption>[foo bar baz]</caption></image>' );
  254. const modelRoot = document.getRoot();
  255. const modelImage = modelRoot.getChild( 0 );
  256. const modelCaption = modelImage.getChild( 0 );
  257. // Remove text and selection from caption.
  258. document.enqueueChanges( () => {
  259. const batch = document.batch();
  260. batch.remove( ModelRange.createIn( modelCaption ) );
  261. document.selection.removeAllRanges();
  262. } );
  263. // Check if there is no figcaption in the view.
  264. expect( getViewData( viewDocument ) ).to.equal(
  265. '[]<figure class="image ck-widget" contenteditable="false">' +
  266. '<img src=""></img>' +
  267. '<figcaption class="ck-editable ck-hidden" contenteditable="true"></figcaption>' +
  268. '</figure>'
  269. );
  270. editor.execute( 'undo' );
  271. // Check if figcaption is back with contents.
  272. expect( getViewData( viewDocument ) ).to.equal(
  273. '<figure class="image ck-widget" contenteditable="false">' +
  274. '<img src=""></img>' +
  275. '<figcaption class="ck-editable" contenteditable="true">{foo bar baz}</figcaption>' +
  276. '</figure>'
  277. );
  278. } );
  279. it( 'undo should work after inserting the image', () => {
  280. const image = new ModelElement( 'image' );
  281. setModelData( document, '[]' );
  282. document.enqueueChanges( () => {
  283. const batch = document.batch();
  284. batch.insert( document.selection.anchor, image );
  285. } );
  286. editor.execute( 'undo' );
  287. expect( getModelData( document ) ).to.equal(
  288. '[]'
  289. );
  290. } );
  291. } );
  292. } );
  293. } );