imagecaptionediting.js 18 KB

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