imagecaptionengine.js 18 KB

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