imagecaptionengine.js 18 KB

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