imagecaptionediting.js 22 KB

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