imagecaptionengine.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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.itemExtends( 'caption', '$block' ) ).to.be.true;
  44. expect( document.schema.limits.has( 'caption' ) );
  45. } );
  46. describe( 'data pipeline', () => {
  47. describe( 'view to model', () => {
  48. it( 'should convert figcaption inside image figure', () => {
  49. editor.setData( '<figure class="image"><img src="foo.png"/><figcaption>foo bar</figcaption></figure>' );
  50. expect( getModelData( document, { withoutSelection: true } ) )
  51. .to.equal( '<image src="foo.png"><caption>foo bar</caption></image>' );
  52. } );
  53. it( 'should add empty caption if there is no figcaption', () => {
  54. editor.setData( '<figure class="image"><img src="foo.png"/></figure>' );
  55. expect( getModelData( document, { withoutSelection: true } ) )
  56. .to.equal( '<image src="foo.png"><caption></caption></image>' );
  57. } );
  58. it( 'should not convert figcaption inside other elements than image', () => {
  59. editor.setData( '<widget><figcaption>foobar</figcaption></widget>' );
  60. expect( getModelData( document, { withoutSelection: true } ) )
  61. .to.equal( '<widget>foobar</widget>' );
  62. } );
  63. } );
  64. describe( 'model to view', () => {
  65. it( 'should convert caption element to figcaption', () => {
  66. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  67. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"><figcaption>Foo bar baz.</figcaption></figure>' );
  68. } );
  69. it( 'should not convert caption to figcaption if it\'s empty', () => {
  70. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  71. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"></figure>' );
  72. } );
  73. it( 'should not convert caption from other elements', () => {
  74. setModelData( document, '<widget>foo bar<caption></caption></widget>' );
  75. expect( editor.getData() ).to.equal( '<widget>foo bar</widget>' );
  76. } );
  77. } );
  78. } );
  79. describe( 'editing pipeline', () => {
  80. describe( 'model to view', () => {
  81. it( 'should convert caption element to figcaption contenteditable', () => {
  82. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  83. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  84. '<figure class="image ck-widget" contenteditable="false">' +
  85. '<img src="img.png"></img>' +
  86. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  87. 'Foo bar baz.' +
  88. '</figcaption>' +
  89. '</figure>'
  90. );
  91. } );
  92. it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
  93. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  94. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  95. '<figure class="image ck-widget" contenteditable="false">' +
  96. '<img src="img.png"></img>' +
  97. '<figcaption class="ck-placeholder ck-editable ck-hidden" contenteditable="true" data-placeholder="Enter image caption">' +
  98. '</figcaption>' +
  99. '</figure>'
  100. );
  101. } );
  102. it( 'should not convert caption from other elements', () => {
  103. setModelData( document, '<widget>foo bar<caption></caption></widget>' );
  104. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  105. } );
  106. it( 'should not convert when element is already consumed', () => {
  107. editor.editing.modelToView.on(
  108. 'insert:caption',
  109. ( evt, data, consumable, conversionApi ) => {
  110. consumable.consume( data.item, 'insert' );
  111. const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
  112. const viewElement = new ViewAttributeElement( 'span' );
  113. const viewPosition = ViewPosition.createAt( imageFigure, 'end' );
  114. conversionApi.mapper.bindElements( data.item, viewElement );
  115. viewWriter.insert( viewPosition, viewElement );
  116. },
  117. { priority: 'high' }
  118. );
  119. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  120. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  121. '<figure class="image ck-widget" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
  122. );
  123. } );
  124. it( 'should show caption when something is inserted inside', () => {
  125. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  126. const image = document.getRoot().getChild( 0 );
  127. const caption = image.getChild( 0 );
  128. document.enqueueChanges( () => {
  129. const batch = document.batch();
  130. batch.insert( ModelPosition.createAt( caption ), 'foo bar' );
  131. } );
  132. expect( getViewData( viewDocument ) ).to.equal(
  133. '[]<figure class="image ck-widget" contenteditable="false">' +
  134. '<img src="img.png"></img>' +
  135. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  136. 'foo bar' +
  137. '</figcaption>' +
  138. '</figure>'
  139. );
  140. } );
  141. it( 'should hide when everything is removed from caption', () => {
  142. setModelData( document, '<image src="img.png"><caption>foo bar baz</caption></image>' );
  143. const image = document.getRoot().getChild( 0 );
  144. const caption = image.getChild( 0 );
  145. document.enqueueChanges( () => {
  146. const batch = document.batch();
  147. batch.remove( ModelRange.createIn( caption ) );
  148. } );
  149. expect( getViewData( viewDocument ) ).to.equal(
  150. '[]<figure class="image ck-widget" contenteditable="false">' +
  151. '<img src="img.png"></img>' +
  152. '<figcaption class="ck-editable ck-hidden ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  153. '</figcaption>' +
  154. '</figure>'
  155. );
  156. } );
  157. it( 'should show when not everything is removed from caption', () => {
  158. setModelData( document, '<image src="img.png"><caption>foo bar baz</caption></image>' );
  159. const image = document.getRoot().getChild( 0 );
  160. const caption = image.getChild( 0 );
  161. document.enqueueChanges( () => {
  162. const batch = document.batch();
  163. batch.remove( ModelRange.createFromParentsAndOffsets( caption, 0, caption, 8 ) );
  164. } );
  165. expect( getViewData( viewDocument ) ).to.equal(
  166. '[]<figure class="image ck-widget" contenteditable="false">' +
  167. '<img src="img.png"></img>' +
  168. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
  169. '</figure>'
  170. );
  171. } );
  172. } );
  173. } );
  174. describe( 'inserting image to document', () => {
  175. it( 'should add caption element if image does not have it', () => {
  176. const image = new ModelElement( 'image', { src: '', alt: '' } );
  177. const batch = document.batch();
  178. document.enqueueChanges( () => {
  179. batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
  180. } );
  181. expect( getModelData( document ) ).to.equal(
  182. '[]<image alt="" src=""><caption></caption></image>'
  183. );
  184. expect( getViewData( viewDocument ) ).to.equal(
  185. '[]<figure class="image ck-widget" contenteditable="false">' +
  186. '<img alt="" src=""></img>' +
  187. '<figcaption class="ck-placeholder ck-editable ck-hidden" contenteditable="true" data-placeholder="Enter image caption">' +
  188. '</figcaption>' +
  189. '</figure>'
  190. );
  191. } );
  192. it( 'should not add caption element if image already have it', () => {
  193. const caption = new ModelElement( 'caption', null, 'foo bar' );
  194. const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
  195. const batch = document.batch();
  196. document.enqueueChanges( () => {
  197. batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
  198. } );
  199. expect( getModelData( document ) ).to.equal(
  200. '[]<image alt="" src=""><caption>foo bar</caption></image>'
  201. );
  202. expect( getViewData( viewDocument ) ).to.equal(
  203. '[]<figure class="image ck-widget" contenteditable="false">' +
  204. '<img alt="" src=""></img>' +
  205. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  206. 'foo bar' +
  207. '</figcaption>' +
  208. '</figure>'
  209. );
  210. } );
  211. it( 'should not add caption element twice', () => {
  212. const image = new ModelElement( 'image', { src: '', alt: '' } );
  213. const caption = new ModelElement( 'caption' );
  214. const batch = document.batch();
  215. document.enqueueChanges( () => {
  216. batch
  217. // Since we are adding an empty image, this should trigger caption fixer.
  218. .insert( ModelPosition.createAt( document.getRoot() ), image )
  219. // Add caption just after the image is inserted, in same batch and enqueue changes block.
  220. .insert( ModelPosition.createAt( image ), caption );
  221. } );
  222. // Check whether caption fixer added redundant caption.
  223. expect( getModelData( document ) ).to.equal(
  224. '[]<image alt="" src=""><caption></caption></image>'
  225. );
  226. expect( getViewData( viewDocument ) ).to.equal(
  227. '[]<figure class="image ck-widget" contenteditable="false">' +
  228. '<img alt="" src=""></img>' +
  229. '<figcaption class="ck-placeholder ck-editable ck-hidden" contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  230. '</figure>'
  231. );
  232. } );
  233. it( 'should do nothing for other changes than insert', () => {
  234. setModelData( document, '<image src=""><caption>foo bar</caption></image>' );
  235. const image = document.getRoot().getChild( 0 );
  236. const batch = document.batch();
  237. document.enqueueChanges( () => {
  238. batch.setAttribute( image, 'alt', 'alt text' );
  239. } );
  240. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  241. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  242. );
  243. } );
  244. } );
  245. describe( 'editing view', () => {
  246. it( 'image should have empty figcaption element when is selected', () => {
  247. setModelData( document, '[<image src=""><caption></caption></image>]' );
  248. expect( getViewData( viewDocument ) ).to.equal(
  249. '[<figure class="image ck-widget" contenteditable="false">' +
  250. '<img src=""></img>' +
  251. '<figcaption class="ck-placeholder ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  252. '</figcaption>' +
  253. '</figure>]'
  254. );
  255. } );
  256. it( 'image should have empty figcaption element with hidden class when not selected', () => {
  257. setModelData( document, '[]<image src=""><caption></caption></image>' );
  258. expect( getViewData( viewDocument ) ).to.equal(
  259. '[]<figure class="image ck-widget" contenteditable="false">' +
  260. '<img src=""></img>' +
  261. '<figcaption class="ck-placeholder ck-editable ck-hidden" contenteditable="true" data-placeholder="Enter image caption">' +
  262. '</figcaption>' +
  263. '</figure>'
  264. );
  265. } );
  266. it( 'should not add additional figcaption if one is already present', () => {
  267. setModelData( document, '[<image src=""><caption>foo bar</caption></image>]' );
  268. expect( getViewData( viewDocument ) ).to.equal(
  269. '[<figure class="image ck-widget" contenteditable="false">' +
  270. '<img src=""></img>' +
  271. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  272. '</figure>]'
  273. );
  274. } );
  275. it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
  276. setModelData( document, '[<image src=""><caption></caption></image>]' );
  277. document.enqueueChanges( () => {
  278. document.selection.removeAllRanges();
  279. } );
  280. expect( getViewData( viewDocument ) ).to.equal(
  281. '[]<figure class="image ck-widget" contenteditable="false">' +
  282. '<img src=""></img>' +
  283. '<figcaption class="ck-placeholder ck-editable ck-hidden" contenteditable="true" data-placeholder="Enter image caption">' +
  284. '</figcaption>' +
  285. '</figure>'
  286. );
  287. } );
  288. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  289. setModelData( document, '<image src=""><caption>[foo bar]</caption></image>' );
  290. document.enqueueChanges( () => {
  291. document.batch().remove( document.selection.getFirstRange() );
  292. } );
  293. expect( getViewData( viewDocument ) ).to.equal(
  294. '<figure class="image ck-widget" contenteditable="false">' +
  295. '<img src=""></img>' +
  296. '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  297. '[]' +
  298. '</figcaption>' +
  299. '</figure>'
  300. );
  301. } );
  302. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  303. setModelData( document, '<image src=""><caption>[foo bar]</caption></image>' );
  304. const image = document.getRoot().getChild( 0 );
  305. document.enqueueChanges( () => {
  306. document.batch().remove( document.selection.getFirstRange() );
  307. document.selection.setRanges( [ ModelRange.createOn( image ) ] );
  308. } );
  309. expect( getViewData( viewDocument ) ).to.equal(
  310. '[<figure class="image ck-widget" contenteditable="false">' +
  311. '<img src=""></img>' +
  312. '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  313. '</figure>]'
  314. );
  315. } );
  316. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  317. setModelData( document, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  318. const image = document.getRoot().getChild( 1 );
  319. document.enqueueChanges( () => {
  320. document.selection.setRanges( [ ModelRange.createOn( image ) ] );
  321. } );
  322. expect( getViewData( viewDocument ) ).to.equal(
  323. '<figure class="image ck-widget" contenteditable="false">' +
  324. '<img src=""></img>' +
  325. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  326. '</figure>' +
  327. '[<figure class="image ck-widget" contenteditable="false">' +
  328. '<img src=""></img>' +
  329. '<figcaption class="ck-placeholder ck-editable" contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  330. '</figure>]'
  331. );
  332. } );
  333. describe( 'undo/redo integration', () => {
  334. it( 'should create view element after redo', () => {
  335. setModelData( document, '<image src=""><caption>[foo bar baz]</caption></image>' );
  336. const modelRoot = document.getRoot();
  337. const modelImage = modelRoot.getChild( 0 );
  338. const modelCaption = modelImage.getChild( 0 );
  339. // Remove text and selection from caption.
  340. document.enqueueChanges( () => {
  341. const batch = document.batch();
  342. batch.remove( ModelRange.createIn( modelCaption ) );
  343. document.selection.removeAllRanges();
  344. } );
  345. // Check if there is no figcaption in the view.
  346. expect( getViewData( viewDocument ) ).to.equal(
  347. '[]<figure class="image ck-widget" contenteditable="false">' +
  348. '<img src=""></img>' +
  349. '<figcaption class="ck-editable ck-hidden ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  350. '</figcaption>' +
  351. '</figure>'
  352. );
  353. editor.execute( 'undo' );
  354. // Check if figcaption is back with contents.
  355. expect( getViewData( viewDocument ) ).to.equal(
  356. '<figure class="image ck-widget" contenteditable="false">' +
  357. '<img src=""></img>' +
  358. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  359. '{foo bar baz}' +
  360. '</figcaption>' +
  361. '</figure>'
  362. );
  363. } );
  364. it( 'undo should work after inserting the image', () => {
  365. const image = new ModelElement( 'image' );
  366. setModelData( document, '[]' );
  367. document.enqueueChanges( () => {
  368. const batch = document.batch();
  369. batch.insert( document.selection.anchor, image );
  370. } );
  371. editor.execute( 'undo' );
  372. expect( getModelData( document ) ).to.equal(
  373. '[]'
  374. );
  375. } );
  376. } );
  377. } );
  378. } );