imagecaptionediting.js 18 KB

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