imagecaptionediting.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import ImageCaptionEditing from '../../src/imagecaption/imagecaptionediting';
  7. import ImageEditing from '../../src/image/imageediting';
  8. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. describe( 'ImageCaptionEditing', () => {
  14. let editor, model, doc, view;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. return VirtualTestEditor
  18. .create( {
  19. plugins: [ ImageCaptionEditing, ImageEditing, UndoEditing, Paragraph ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. doc = model.document;
  25. view = editor.editing.view;
  26. model.schema.register( 'widget' );
  27. model.schema.extend( 'widget', { allowIn: '$root' } );
  28. model.schema.extend( 'caption', { allowIn: 'widget' } );
  29. model.schema.extend( '$text', { allowIn: 'widget' } );
  30. editor.conversion.elementToElement( {
  31. model: 'widget',
  32. view: 'widget'
  33. } );
  34. } );
  35. } );
  36. it( 'should have pluginName', () => {
  37. expect( ImageCaptionEditing.pluginName ).to.equal( 'ImageCaptionEditing' );
  38. } );
  39. it( 'should be loaded', () => {
  40. expect( editor.plugins.get( ImageCaptionEditing ) ).to.be.instanceOf( ImageCaptionEditing );
  41. } );
  42. it( 'should set proper schema rules', () => {
  43. expect( model.schema.checkChild( [ '$root', 'image' ], 'caption' ) ).to.be.true;
  44. expect( model.schema.checkChild( [ '$root', 'image', 'caption' ], '$text' ) ).to.be.true;
  45. expect( model.schema.isLimit( 'caption' ) ).to.be.true;
  46. expect( model.schema.checkChild( [ '$root', 'image', 'caption' ], 'caption' ) ).to.be.false;
  47. model.schema.extend( '$block', { allowAttributes: 'aligmnent' } );
  48. expect( model.schema.checkAttribute( [ '$root', 'image', 'caption' ], 'alignment' ) ).to.be.false;
  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="/assets/sample.png" /><figcaption>foo bar</figcaption></figure>' );
  54. expect( getModelData( model, { withoutSelection: true } ) )
  55. .to.equal( '<image src="/assets/sample.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="/assets/sample.png" /></figure>' );
  59. expect( getModelData( model, { withoutSelection: true } ) )
  60. .to.equal( '<image src="/assets/sample.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( model, { 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( model, '<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( model, '<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( model, '<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( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  89. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  90. '<figure class="ck-widget image" contenteditable="false">' +
  91. '<img src="img.png"></img>' +
  92. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  93. 'contenteditable="true" data-placeholder="Enter image caption">' +
  94. 'Foo bar baz.' +
  95. '</figcaption>' +
  96. '</figure>'
  97. );
  98. } );
  99. it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
  100. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  101. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  102. '<p>foo</p>' +
  103. '<figure class="ck-widget image" contenteditable="false">' +
  104. '<img src="img.png"></img>' +
  105. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  106. 'contenteditable="true" data-placeholder="Enter image caption">' +
  107. '</figcaption>' +
  108. '</figure>'
  109. );
  110. } );
  111. it( 'should not convert caption from other elements', () => {
  112. setModelData( model, '<widget>foo bar<caption></caption></widget>' );
  113. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  114. } );
  115. it( 'should not convert when element is already consumed', () => {
  116. editor.editing.downcastDispatcher.on(
  117. 'insert:caption',
  118. ( evt, data, conversionApi ) => {
  119. conversionApi.consumable.consume( data.item, 'insert' );
  120. const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
  121. const viewElement = conversionApi.writer.createAttributeElement( 'span' );
  122. const viewPosition = conversionApi.writer.createPositionAt( imageFigure, 'end' );
  123. conversionApi.mapper.bindElements( data.item, viewElement );
  124. conversionApi.writer.insert( viewPosition, viewElement );
  125. },
  126. { priority: 'high' }
  127. );
  128. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  129. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  130. '<figure class="ck-widget image" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
  131. );
  132. } );
  133. it( 'should show caption when something is inserted inside', () => {
  134. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  135. const image = doc.getRoot().getChild( 1 );
  136. const caption = image.getChild( 0 );
  137. model.change( writer => {
  138. writer.insertText( 'foo bar', caption );
  139. } );
  140. expect( getViewData( view ) ).to.equal(
  141. '<p>{}foo</p>' +
  142. '<figure class="ck-widget image" contenteditable="false">' +
  143. '<img src="img.png"></img>' +
  144. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  145. 'contenteditable="true" data-placeholder="Enter image caption">' +
  146. 'foo bar' +
  147. '</figcaption>' +
  148. '</figure>'
  149. );
  150. } );
  151. it( 'should hide when everything is removed from caption', () => {
  152. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  153. const image = doc.getRoot().getChild( 1 );
  154. const caption = image.getChild( 0 );
  155. model.change( writer => {
  156. writer.remove( writer.createRangeIn( caption ) );
  157. } );
  158. expect( getViewData( view ) ).to.equal(
  159. '<p>{}foo</p>' +
  160. '<figure class="ck-widget image" contenteditable="false">' +
  161. '<img src="img.png"></img>' +
  162. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  163. 'contenteditable="true" data-placeholder="Enter image caption">' +
  164. '</figcaption>' +
  165. '</figure>'
  166. );
  167. } );
  168. it( 'should show when not everything is removed from caption', () => {
  169. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  170. const image = doc.getRoot().getChild( 1 );
  171. const caption = image.getChild( 0 );
  172. model.change( writer => {
  173. writer.remove( writer.createRange( writer.createPositionAt( caption, 0 ), writer.createPositionAt( caption, 8 ) ) );
  174. } );
  175. expect( getViewData( view ) ).to.equal(
  176. '<p>{}foo</p>' +
  177. '<figure class="ck-widget image" contenteditable="false">' +
  178. '<img src="img.png"></img>' +
  179. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  180. 'contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
  181. '</figure>'
  182. );
  183. } );
  184. } );
  185. } );
  186. describe( 'inserting image to the document', () => {
  187. it( 'should add caption element if image does not have it', () => {
  188. model.change( writer => {
  189. writer.insertElement( 'image', { src: '', alt: '' }, doc.getRoot() );
  190. } );
  191. expect( getModelData( model ) ).to.equal(
  192. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  193. );
  194. expect( getViewData( view ) ).to.equal(
  195. '[<figure class="ck-widget image" contenteditable="false">' +
  196. '<img alt="" src=""></img>' +
  197. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  198. 'contenteditable="true" data-placeholder="Enter image caption">' +
  199. '</figcaption>' +
  200. '</figure>]' +
  201. '<p></p>'
  202. );
  203. } );
  204. it( 'should add caption element if image does not have it (image is nested in inserted element)', () => {
  205. model.change( writer => {
  206. model.schema.register( 'table', { allowWhere: '$block', isLimit: true, isObject: true, isBlock: true } );
  207. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  208. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true, isSelectable: true } );
  209. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  210. editor.conversion.for( 'downcast' ).elementToElement( { model: 'table', view: 'table' } );
  211. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableRow', view: 'tr' } );
  212. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableCell', view: 'td' } );
  213. const table = writer.createElement( 'table' );
  214. const tableRow = writer.createElement( 'tableRow' );
  215. const tableCell1 = writer.createElement( 'tableCell' );
  216. const tableCell2 = writer.createElement( 'tableCell' );
  217. const image1 = writer.createElement( 'image', { src: '', alt: '' } );
  218. const image2 = writer.createElement( 'image', { src: '', alt: '' } );
  219. writer.insert( tableRow, table );
  220. writer.insert( tableCell1, tableRow );
  221. writer.insert( tableCell2, tableRow );
  222. writer.insert( image1, tableCell1 );
  223. writer.insert( image2, tableCell2 );
  224. writer.insert( table, doc.getRoot() );
  225. } );
  226. expect( getModelData( model ) ).to.equal(
  227. '[<table>' +
  228. '<tableRow>' +
  229. '<tableCell><image alt="" src=""><caption></caption></image></tableCell>' +
  230. '<tableCell><image alt="" src=""><caption></caption></image></tableCell>' +
  231. '</tableRow>' +
  232. '</table>]' +
  233. '<paragraph></paragraph>'
  234. );
  235. expect( getViewData( view ) ).to.equal(
  236. '[<table>' +
  237. '<tr>' +
  238. '<td>' +
  239. '<figure class="ck-widget image" contenteditable="false">' +
  240. '<img alt="" src=""></img>' +
  241. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  242. 'contenteditable="true" data-placeholder="Enter image caption">' +
  243. '</figcaption>' +
  244. '</figure>' +
  245. '</td>' +
  246. '<td>' +
  247. '<figure class="ck-widget image" contenteditable="false">' +
  248. '<img alt="" src=""></img>' +
  249. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  250. 'contenteditable="true" data-placeholder="Enter image caption">' +
  251. '</figcaption>' +
  252. '</figure>' +
  253. '</td>' +
  254. '</tr>' +
  255. '</table>]' +
  256. '<p></p>'
  257. );
  258. } );
  259. it( 'should not add caption element if image already have it', () => {
  260. model.change( writer => {
  261. const caption = writer.createElement( 'caption' );
  262. const image = writer.createElement( 'image', { src: '', alt: '' } );
  263. writer.insertText( 'foo bar', caption );
  264. writer.insert( caption, image );
  265. writer.insert( image, doc.getRoot() );
  266. } );
  267. expect( getModelData( model ) ).to.equal(
  268. '[<image alt="" src=""><caption>foo bar</caption></image>]<paragraph></paragraph>'
  269. );
  270. expect( getViewData( view ) ).to.equal(
  271. '[<figure class="ck-widget image" contenteditable="false">' +
  272. '<img alt="" src=""></img>' +
  273. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  274. 'contenteditable="true" data-placeholder="Enter image caption">' +
  275. 'foo bar' +
  276. '</figcaption>' +
  277. '</figure>]' +
  278. '<p></p>'
  279. );
  280. } );
  281. it( 'should not add caption element twice', () => {
  282. model.change( writer => {
  283. const image = writer.createElement( 'image', { src: '', alt: '' } );
  284. const caption = writer.createElement( 'caption' );
  285. // Since we are adding an empty image, this should trigger caption fixer.
  286. writer.insert( image, doc.getRoot() );
  287. // Add caption just after the image is inserted, in same batch.
  288. writer.insert( caption, image );
  289. } );
  290. // Check whether caption fixer added redundant caption.
  291. expect( getModelData( model ) ).to.equal(
  292. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  293. );
  294. expect( getViewData( view ) ).to.equal(
  295. '[<figure class="ck-widget image" contenteditable="false">' +
  296. '<img alt="" src=""></img>' +
  297. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  298. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  299. '</figure>]' +
  300. '<p></p>'
  301. );
  302. } );
  303. it( 'should do nothing for other changes than insert', () => {
  304. setModelData( model, '<image src=""><caption>foo bar</caption></image>' );
  305. const image = doc.getRoot().getChild( 0 );
  306. model.change( writer => {
  307. writer.setAttribute( 'alt', 'alt text', image );
  308. } );
  309. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  310. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  311. );
  312. } );
  313. it( 'should do nothing on $text insert', () => {
  314. setModelData( model, '<image src=""><caption>foo bar</caption></image><paragraph>[]</paragraph>' );
  315. const paragraph = doc.getRoot().getChild( 1 );
  316. // Simulate typing behavior - second input will generate input change without entry.item in change entry.
  317. const batch = model.createBatch();
  318. model.enqueueChange( batch, writer => {
  319. writer.insertText( 'f', paragraph, 0 );
  320. } );
  321. model.enqueueChange( batch, writer => {
  322. writer.insertText( 'oo', paragraph, 1 );
  323. } );
  324. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  325. '<image src=""><caption>foo bar</caption></image><paragraph>foo</paragraph>'
  326. );
  327. } );
  328. } );
  329. describe( 'editing view', () => {
  330. it( 'image should have empty figcaption element when is selected', () => {
  331. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  332. expect( getViewData( view ) ).to.equal(
  333. '<p>foo</p>' +
  334. '[<figure class="ck-widget image" contenteditable="false">' +
  335. '<img src=""></img>' +
  336. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  337. 'contenteditable="true" data-placeholder="Enter image caption">' +
  338. '</figcaption>' +
  339. '</figure>]'
  340. );
  341. } );
  342. it( 'image should have empty figcaption element with hidden class when not selected', () => {
  343. setModelData( model, '<paragraph>[]foo</paragraph><image src=""><caption></caption></image>' );
  344. expect( getViewData( view ) ).to.equal(
  345. '<p>{}foo</p>' +
  346. '<figure class="ck-widget image" contenteditable="false">' +
  347. '<img src=""></img>' +
  348. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  349. 'contenteditable="true" data-placeholder="Enter image caption">' +
  350. '</figcaption>' +
  351. '</figure>'
  352. );
  353. } );
  354. it( 'should not add additional figcaption if one is already present', () => {
  355. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption>foo bar</caption></image>]' );
  356. expect( getViewData( view ) ).to.equal(
  357. '<p>foo</p>' +
  358. '[<figure class="ck-widget image" contenteditable="false">' +
  359. '<img src=""></img>' +
  360. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  361. 'contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  362. '</figure>]'
  363. );
  364. } );
  365. it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
  366. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  367. model.change( writer => {
  368. writer.setSelection( null );
  369. } );
  370. expect( getViewData( view ) ).to.equal(
  371. '<p>{}foo</p>' +
  372. '<figure class="ck-widget image" contenteditable="false">' +
  373. '<img src=""></img>' +
  374. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  375. 'contenteditable="true" data-placeholder="Enter image caption">' +
  376. '</figcaption>' +
  377. '</figure>'
  378. );
  379. } );
  380. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  381. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  382. model.change( writer => {
  383. writer.remove( doc.selection.getFirstRange() );
  384. } );
  385. expect( getViewData( view ) ).to.equal(
  386. '<figure class="ck-widget image" contenteditable="false">' +
  387. '<img src=""></img>' +
  388. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  389. 'contenteditable="true" data-placeholder="Enter image caption">' +
  390. '[]' +
  391. '</figcaption>' +
  392. '</figure>'
  393. );
  394. } );
  395. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  396. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  397. const image = doc.getRoot().getChild( 0 );
  398. model.change( writer => {
  399. writer.remove( doc.selection.getFirstRange() );
  400. writer.setSelection( writer.createRangeOn( image ) );
  401. } );
  402. expect( getViewData( view ) ).to.equal(
  403. '[<figure class="ck-widget image" contenteditable="false">' +
  404. '<img src=""></img>' +
  405. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  406. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  407. '</figure>]'
  408. );
  409. } );
  410. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  411. setModelData( model, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  412. const image = doc.getRoot().getChild( 1 );
  413. model.change( writer => {
  414. writer.setSelection( writer.createRangeOn( image ) );
  415. } );
  416. expect( getViewData( view ) ).to.equal(
  417. '<figure class="ck-widget image" contenteditable="false">' +
  418. '<img src=""></img>' +
  419. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  420. 'contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  421. '</figure>' +
  422. '[<figure class="ck-widget image" contenteditable="false">' +
  423. '<img src=""></img>' +
  424. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  425. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  426. '</figure>]'
  427. );
  428. } );
  429. describe( 'undo/redo integration', () => {
  430. it( 'should create view element after redo', () => {
  431. setModelData( model, '<paragraph>foo</paragraph><image src=""><caption>[foo bar baz]</caption></image>' );
  432. const modelRoot = doc.getRoot();
  433. const modelImage = modelRoot.getChild( 1 );
  434. const modelCaption = modelImage.getChild( 0 );
  435. // Remove text and selection from caption.
  436. model.change( writer => {
  437. writer.remove( writer.createRangeIn( modelCaption ) );
  438. writer.setSelection( null );
  439. } );
  440. // Check if there is no figcaption in the view.
  441. expect( getViewData( view ) ).to.equal(
  442. '<p>{}foo</p>' +
  443. '<figure class="ck-widget image" contenteditable="false">' +
  444. '<img src=""></img>' +
  445. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  446. 'contenteditable="true" data-placeholder="Enter image caption">' +
  447. '</figcaption>' +
  448. '</figure>'
  449. );
  450. editor.execute( 'undo' );
  451. // Check if figcaption is back with contents.
  452. expect( getViewData( view ) ).to.equal(
  453. '<p>foo</p>' +
  454. '<figure class="ck-widget image" contenteditable="false">' +
  455. '<img src=""></img>' +
  456. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  457. 'contenteditable="true" data-placeholder="Enter image caption">' +
  458. '{foo bar baz}' +
  459. '</figcaption>' +
  460. '</figure>'
  461. );
  462. } );
  463. it( 'undo should work after inserting the image', () => {
  464. setModelData( model, '<paragraph>foo[]</paragraph>' );
  465. model.change( writer => {
  466. const image = writer.createElement( 'image', { src: '/assets/sample.png' } );
  467. writer.insert( image, doc.getRoot() );
  468. } );
  469. expect( getModelData( model ) ).to.equal(
  470. '<image src="/assets/sample.png"><caption></caption></image><paragraph>foo[]</paragraph>'
  471. );
  472. editor.execute( 'undo' );
  473. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph>' );
  474. } );
  475. } );
  476. } );
  477. } );