imagecaptionediting.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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-editor__editable ck-editor__nested-editable" ' +
  92. 'contenteditable="true" data-placeholder="Enter image caption">' +
  93. 'Foo bar baz.' +
  94. '</figcaption>' +
  95. '</figure>'
  96. );
  97. } );
  98. it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
  99. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  100. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  101. '<p>foo</p>' +
  102. '<figure class="ck ck-widget image" contenteditable="false">' +
  103. '<img src="img.png"></img>' +
  104. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  105. 'contenteditable="true" data-placeholder="Enter image caption">' +
  106. '</figcaption>' +
  107. '</figure>'
  108. );
  109. } );
  110. it( 'should not convert caption from other elements', () => {
  111. setModelData( model, '<widget>foo bar<caption></caption></widget>' );
  112. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  113. } );
  114. it( 'should not convert when element is already consumed', () => {
  115. editor.editing.downcastDispatcher.on(
  116. 'insert:caption',
  117. ( evt, data, conversionApi ) => {
  118. conversionApi.consumable.consume( data.item, 'insert' );
  119. const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
  120. const viewElement = new ViewAttributeElement( 'span' );
  121. const viewPosition = ViewPosition.createAt( imageFigure, 'end' );
  122. conversionApi.mapper.bindElements( data.item, viewElement );
  123. conversionApi.writer.insert( viewPosition, viewElement );
  124. },
  125. { priority: 'high' }
  126. );
  127. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  128. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  129. '<figure class="ck ck-widget image" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
  130. );
  131. } );
  132. it( 'should show caption when something is inserted inside', () => {
  133. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  134. const image = doc.getRoot().getChild( 1 );
  135. const caption = image.getChild( 0 );
  136. model.change( writer => {
  137. writer.insertText( 'foo bar', caption );
  138. } );
  139. expect( getViewData( view ) ).to.equal(
  140. '<p>{}foo</p>' +
  141. '<figure class="ck ck-widget image" contenteditable="false">' +
  142. '<img src="img.png"></img>' +
  143. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable" ' +
  144. 'contenteditable="true" data-placeholder="Enter image caption">' +
  145. 'foo bar' +
  146. '</figcaption>' +
  147. '</figure>'
  148. );
  149. } );
  150. it( 'should hide when everything is removed from caption', () => {
  151. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  152. const image = doc.getRoot().getChild( 1 );
  153. const caption = image.getChild( 0 );
  154. model.change( writer => {
  155. writer.remove( ModelRange.createIn( caption ) );
  156. } );
  157. expect( getViewData( view ) ).to.equal(
  158. '<p>{}foo</p>' +
  159. '<figure class="ck ck-widget image" contenteditable="false">' +
  160. '<img src="img.png"></img>' +
  161. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  162. 'contenteditable="true" data-placeholder="Enter image caption">' +
  163. '</figcaption>' +
  164. '</figure>'
  165. );
  166. } );
  167. it( 'should show when not everything is removed from caption', () => {
  168. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  169. const image = doc.getRoot().getChild( 1 );
  170. const caption = image.getChild( 0 );
  171. model.change( writer => {
  172. writer.remove( ModelRange.createFromParentsAndOffsets( caption, 0, caption, 8 ) );
  173. } );
  174. expect( getViewData( view ) ).to.equal(
  175. '<p>{}foo</p>' +
  176. '<figure class="ck ck-widget image" contenteditable="false">' +
  177. '<img src="img.png"></img>' +
  178. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable" ' +
  179. 'contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
  180. '</figure>'
  181. );
  182. } );
  183. } );
  184. } );
  185. describe( 'inserting image to the document', () => {
  186. it( 'should add caption element if image does not have it', () => {
  187. model.change( writer => {
  188. writer.insertElement( 'image', { src: '', alt: '' }, doc.getRoot() );
  189. } );
  190. expect( getModelData( model ) ).to.equal(
  191. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  192. );
  193. expect( getViewData( view ) ).to.equal(
  194. '[<figure class="ck ck-widget image" contenteditable="false">' +
  195. '<img alt="" src=""></img>' +
  196. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  197. 'contenteditable="true" data-placeholder="Enter image caption">' +
  198. '</figcaption>' +
  199. '</figure>]' +
  200. '<p></p>'
  201. );
  202. } );
  203. it( 'should not add caption element if image already have it', () => {
  204. const caption = new ModelElement( 'caption', null, 'foo bar' );
  205. const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
  206. model.change( writer => {
  207. writer.insert( image, doc.getRoot() );
  208. } );
  209. expect( getModelData( model ) ).to.equal(
  210. '[<image alt="" src=""><caption>foo bar</caption></image>]<paragraph></paragraph>'
  211. );
  212. expect( getViewData( view ) ).to.equal(
  213. '[<figure class="ck ck-widget image" contenteditable="false">' +
  214. '<img alt="" src=""></img>' +
  215. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable" ' +
  216. 'contenteditable="true" data-placeholder="Enter image caption">' +
  217. 'foo bar' +
  218. '</figcaption>' +
  219. '</figure>]' +
  220. '<p></p>'
  221. );
  222. } );
  223. it( 'should not add caption element twice', () => {
  224. const image = new ModelElement( 'image', { src: '', alt: '' } );
  225. const caption = new ModelElement( 'caption' );
  226. model.change( writer => {
  227. // Since we are adding an empty image, this should trigger caption fixer.
  228. writer.insert( image, doc.getRoot() );
  229. // Add caption just after the image is inserted, in same batch.
  230. writer.insert( caption, image );
  231. } );
  232. // Check whether caption fixer added redundant caption.
  233. expect( getModelData( model ) ).to.equal(
  234. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  235. );
  236. expect( getViewData( view ) ).to.equal(
  237. '[<figure class="ck ck-widget image" contenteditable="false">' +
  238. '<img alt="" src=""></img>' +
  239. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  240. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  241. '</figure>]' +
  242. '<p></p>'
  243. );
  244. } );
  245. it( 'should do nothing for other changes than insert', () => {
  246. setModelData( model, '<image src=""><caption>foo bar</caption></image>' );
  247. const image = doc.getRoot().getChild( 0 );
  248. model.change( writer => {
  249. writer.setAttribute( 'alt', 'alt text', image );
  250. } );
  251. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  252. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  253. );
  254. } );
  255. } );
  256. describe( 'editing view', () => {
  257. it( 'image should have empty figcaption element when is selected', () => {
  258. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  259. expect( getViewData( view ) ).to.equal(
  260. '<p>foo</p>' +
  261. '[<figure class="ck ck-widget image" contenteditable="false">' +
  262. '<img src=""></img>' +
  263. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  264. 'contenteditable="true" data-placeholder="Enter image caption">' +
  265. '</figcaption>' +
  266. '</figure>]'
  267. );
  268. } );
  269. it( 'image should have empty figcaption element with hidden class when not selected', () => {
  270. setModelData( model, '<paragraph>[]foo</paragraph><image src=""><caption></caption></image>' );
  271. expect( getViewData( view ) ).to.equal(
  272. '<p>{}foo</p>' +
  273. '<figure class="ck ck-widget image" contenteditable="false">' +
  274. '<img src=""></img>' +
  275. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  276. 'contenteditable="true" data-placeholder="Enter image caption">' +
  277. '</figcaption>' +
  278. '</figure>'
  279. );
  280. } );
  281. it( 'should not add additional figcaption if one is already present', () => {
  282. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption>foo bar</caption></image>]' );
  283. expect( getViewData( view ) ).to.equal(
  284. '<p>foo</p>' +
  285. '[<figure class="ck ck-widget image" contenteditable="false">' +
  286. '<img src=""></img>' +
  287. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable" ' +
  288. 'contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  289. '</figure>]'
  290. );
  291. } );
  292. it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
  293. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  294. model.change( writer => {
  295. writer.setSelection( null );
  296. } );
  297. expect( getViewData( view ) ).to.equal(
  298. '<p>{}foo</p>' +
  299. '<figure class="ck ck-widget image" contenteditable="false">' +
  300. '<img src=""></img>' +
  301. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  302. 'contenteditable="true" data-placeholder="Enter image caption">' +
  303. '</figcaption>' +
  304. '</figure>'
  305. );
  306. } );
  307. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  308. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  309. model.change( writer => {
  310. writer.remove( doc.selection.getFirstRange() );
  311. } );
  312. expect( getViewData( view ) ).to.equal(
  313. '<figure class="ck ck-widget image" contenteditable="false">' +
  314. '<img src=""></img>' +
  315. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  316. 'contenteditable="true" data-placeholder="Enter image caption">' +
  317. '[]' +
  318. '</figcaption>' +
  319. '</figure>'
  320. );
  321. } );
  322. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  323. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  324. const image = doc.getRoot().getChild( 0 );
  325. model.change( writer => {
  326. writer.remove( doc.selection.getFirstRange() );
  327. writer.setSelection( ModelRange.createOn( image ) );
  328. } );
  329. expect( getViewData( view ) ).to.equal(
  330. '[<figure class="ck ck-widget image" contenteditable="false">' +
  331. '<img src=""></img>' +
  332. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  333. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  334. '</figure>]'
  335. );
  336. } );
  337. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  338. setModelData( model, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  339. const image = doc.getRoot().getChild( 1 );
  340. model.change( writer => {
  341. writer.setSelection( ModelRange.createOn( image ) );
  342. } );
  343. expect( getViewData( view ) ).to.equal(
  344. '<figure class="ck ck-widget image" contenteditable="false">' +
  345. '<img src=""></img>' +
  346. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable" ' +
  347. 'contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  348. '</figure>' +
  349. '[<figure class="ck ck-widget image" contenteditable="false">' +
  350. '<img src=""></img>' +
  351. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  352. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  353. '</figure>]'
  354. );
  355. } );
  356. describe( 'undo/redo integration', () => {
  357. it( 'should create view element after redo', () => {
  358. setModelData( model, '<paragraph>foo</paragraph><image src=""><caption>[foo bar baz]</caption></image>' );
  359. const modelRoot = doc.getRoot();
  360. const modelImage = modelRoot.getChild( 1 );
  361. const modelCaption = modelImage.getChild( 0 );
  362. // Remove text and selection from caption.
  363. model.change( writer => {
  364. writer.remove( ModelRange.createIn( modelCaption ) );
  365. writer.setSelection( null );
  366. } );
  367. // Check if there is no figcaption in the view.
  368. expect( getViewData( view ) ).to.equal(
  369. '<p>{}foo</p>' +
  370. '<figure class="ck ck-widget image" contenteditable="false">' +
  371. '<img src=""></img>' +
  372. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  373. 'contenteditable="true" data-placeholder="Enter image caption">' +
  374. '</figcaption>' +
  375. '</figure>'
  376. );
  377. editor.execute( 'undo' );
  378. // Check if figcaption is back with contents.
  379. expect( getViewData( view ) ).to.equal(
  380. '<p>foo</p>' +
  381. '<figure class="ck ck-widget image" contenteditable="false">' +
  382. '<img src=""></img>' +
  383. '<figcaption class="ck ck-editor__editable ck-editor__nested-editable" ' +
  384. 'contenteditable="true" data-placeholder="Enter image caption">' +
  385. '{foo bar baz}' +
  386. '</figcaption>' +
  387. '</figure>'
  388. );
  389. } );
  390. it( 'undo should work after inserting the image', () => {
  391. setModelData( model, '<paragraph>foo[]</paragraph>' );
  392. model.change( writer => {
  393. const image = writer.createElement( 'image', { src: '/foo.png' } );
  394. writer.insert( image, doc.getRoot() );
  395. } );
  396. expect( getModelData( model ) ).to.equal( '<image src="/foo.png"><caption></caption></image><paragraph>foo[]</paragraph>' );
  397. editor.execute( 'undo' );
  398. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph>' );
  399. } );
  400. } );
  401. } );
  402. } );