imagecaptionengine.js 18 KB

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