imagecaptionengine.js 18 KB

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