imagecaptionediting.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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-widget image" contenteditable="false">' +
  90. '<img src="img.png"></img>' +
  91. '<figcaption class="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-widget image" contenteditable="false">' +
  102. '<img src="img.png"></img>' +
  103. '<figcaption class="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-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-widget image" contenteditable="false">' +
  141. '<img src="img.png"></img>' +
  142. '<figcaption class="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-widget image" contenteditable="false">' +
  158. '<img src="img.png"></img>' +
  159. '<figcaption class="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-widget image" contenteditable="false">' +
  175. '<img src="img.png"></img>' +
  176. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
  177. '</figure>'
  178. );
  179. } );
  180. } );
  181. } );
  182. describe( 'inserting image to the document', () => {
  183. it( 'should add caption element if image does not have it', () => {
  184. model.change( writer => {
  185. writer.insertElement( 'image', { src: '', alt: '' }, doc.getRoot() );
  186. } );
  187. expect( getModelData( model ) ).to.equal(
  188. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  189. );
  190. expect( getViewData( view ) ).to.equal(
  191. '[<figure class="ck-widget image" contenteditable="false">' +
  192. '<img alt="" src=""></img>' +
  193. '<figcaption class="ck-editable ck-placeholder" ' +
  194. 'contenteditable="true" data-placeholder="Enter image caption">' +
  195. '</figcaption>' +
  196. '</figure>]' +
  197. '<p></p>'
  198. );
  199. } );
  200. it( 'should not add caption element if image already have it', () => {
  201. const caption = new ModelElement( 'caption', null, 'foo bar' );
  202. const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
  203. model.change( writer => {
  204. writer.insert( image, doc.getRoot() );
  205. } );
  206. expect( getModelData( model ) ).to.equal(
  207. '[<image alt="" src=""><caption>foo bar</caption></image>]<paragraph></paragraph>'
  208. );
  209. expect( getViewData( view ) ).to.equal(
  210. '[<figure class="ck-widget image" contenteditable="false">' +
  211. '<img alt="" src=""></img>' +
  212. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  213. 'foo bar' +
  214. '</figcaption>' +
  215. '</figure>]' +
  216. '<p></p>'
  217. );
  218. } );
  219. it( 'should not add caption element twice', () => {
  220. const image = new ModelElement( 'image', { src: '', alt: '' } );
  221. const caption = new ModelElement( 'caption' );
  222. model.change( writer => {
  223. // Since we are adding an empty image, this should trigger caption fixer.
  224. writer.insert( image, doc.getRoot() );
  225. // Add caption just after the image is inserted, in same batch.
  226. writer.insert( caption, image );
  227. } );
  228. // Check whether caption fixer added redundant caption.
  229. expect( getModelData( model ) ).to.equal(
  230. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  231. );
  232. expect( getViewData( view ) ).to.equal(
  233. '[<figure class="ck-widget image" contenteditable="false">' +
  234. '<img alt="" src=""></img>' +
  235. '<figcaption class="ck-editable ck-placeholder" ' +
  236. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  237. '</figure>]' +
  238. '<p></p>'
  239. );
  240. } );
  241. it( 'should do nothing for other changes than insert', () => {
  242. setModelData( model, '<image src=""><caption>foo bar</caption></image>' );
  243. const image = doc.getRoot().getChild( 0 );
  244. model.change( writer => {
  245. writer.setAttribute( 'alt', 'alt text', image );
  246. } );
  247. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  248. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  249. );
  250. } );
  251. } );
  252. describe( 'editing view', () => {
  253. it( 'image should have empty figcaption element when is selected', () => {
  254. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  255. expect( getViewData( view ) ).to.equal(
  256. '<p>foo</p>' +
  257. '[<figure class="ck-widget image" contenteditable="false">' +
  258. '<img src=""></img>' +
  259. '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  260. '</figcaption>' +
  261. '</figure>]'
  262. );
  263. } );
  264. it( 'image should have empty figcaption element with hidden class when not selected', () => {
  265. setModelData( model, '<paragraph>[]foo</paragraph><image src=""><caption></caption></image>' );
  266. expect( getViewData( view ) ).to.equal(
  267. '<p>{}foo</p>' +
  268. '<figure class="ck-widget image" contenteditable="false">' +
  269. '<img src=""></img>' +
  270. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  271. 'contenteditable="true" data-placeholder="Enter image caption">' +
  272. '</figcaption>' +
  273. '</figure>'
  274. );
  275. } );
  276. it( 'should not add additional figcaption if one is already present', () => {
  277. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption>foo bar</caption></image>]' );
  278. expect( getViewData( view ) ).to.equal(
  279. '<p>foo</p>' +
  280. '[<figure class="ck-widget image" contenteditable="false">' +
  281. '<img src=""></img>' +
  282. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  283. '</figure>]'
  284. );
  285. } );
  286. it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
  287. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  288. model.change( writer => {
  289. writer.setSelection( null );
  290. } );
  291. expect( getViewData( view ) ).to.equal(
  292. '<p>{}foo</p>' +
  293. '<figure class="ck-widget image" contenteditable="false">' +
  294. '<img src=""></img>' +
  295. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  296. 'contenteditable="true" data-placeholder="Enter image caption">' +
  297. '</figcaption>' +
  298. '</figure>'
  299. );
  300. } );
  301. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  302. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  303. model.change( writer => {
  304. writer.remove( doc.selection.getFirstRange() );
  305. } );
  306. expect( getViewData( view ) ).to.equal(
  307. '<figure class="ck-widget image" contenteditable="false">' +
  308. '<img src=""></img>' +
  309. '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  310. '[]' +
  311. '</figcaption>' +
  312. '</figure>'
  313. );
  314. } );
  315. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  316. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  317. const image = doc.getRoot().getChild( 0 );
  318. model.change( writer => {
  319. writer.remove( doc.selection.getFirstRange() );
  320. writer.setSelection( ModelRange.createOn( image ) );
  321. } );
  322. expect( getViewData( view ) ).to.equal(
  323. '[<figure class="ck-widget image" contenteditable="false">' +
  324. '<img src=""></img>' +
  325. '<figcaption class="ck-editable ck-placeholder" ' +
  326. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  327. '</figure>]'
  328. );
  329. } );
  330. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  331. setModelData( model, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  332. const image = doc.getRoot().getChild( 1 );
  333. model.change( writer => {
  334. writer.setSelection( ModelRange.createOn( image ) );
  335. } );
  336. expect( getViewData( view ) ).to.equal(
  337. '<figure class="ck-widget image" contenteditable="false">' +
  338. '<img src=""></img>' +
  339. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  340. '</figure>' +
  341. '[<figure class="ck-widget image" contenteditable="false">' +
  342. '<img src=""></img>' +
  343. '<figcaption class="ck-editable ck-placeholder" ' +
  344. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  345. '</figure>]'
  346. );
  347. } );
  348. describe( 'undo/redo integration', () => {
  349. it( 'should create view element after redo', () => {
  350. setModelData( model, '<paragraph>foo</paragraph><image src=""><caption>[foo bar baz]</caption></image>' );
  351. const modelRoot = doc.getRoot();
  352. const modelImage = modelRoot.getChild( 1 );
  353. const modelCaption = modelImage.getChild( 0 );
  354. // Remove text and selection from caption.
  355. model.change( writer => {
  356. writer.remove( ModelRange.createIn( modelCaption ) );
  357. writer.setSelection( null );
  358. } );
  359. // Check if there is no figcaption in the view.
  360. expect( getViewData( view ) ).to.equal(
  361. '<p>{}foo</p>' +
  362. '<figure class="ck-widget image" contenteditable="false">' +
  363. '<img src=""></img>' +
  364. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  365. 'contenteditable="true" data-placeholder="Enter image caption">' +
  366. '</figcaption>' +
  367. '</figure>'
  368. );
  369. editor.execute( 'undo' );
  370. // Check if figcaption is back with contents.
  371. expect( getViewData( view ) ).to.equal(
  372. '<p>foo</p>' +
  373. '<figure class="ck-widget image" contenteditable="false">' +
  374. '<img src=""></img>' +
  375. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  376. '{foo bar baz}' +
  377. '</figcaption>' +
  378. '</figure>'
  379. );
  380. } );
  381. it( 'undo should work after inserting the image', () => {
  382. setModelData( model, '<paragraph>foo[]</paragraph>' );
  383. model.change( writer => {
  384. const image = writer.createElement( 'image', { src: '/foo.png' } );
  385. writer.insert( image, doc.getRoot() );
  386. } );
  387. expect( getModelData( model ) ).to.equal( '<image src="/foo.png"><caption></caption></image><paragraph>foo[]</paragraph>' );
  388. editor.execute( 'undo' );
  389. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph>' );
  390. } );
  391. } );
  392. } );
  393. } );