imagecaptionediting.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  14. import env from '@ckeditor/ckeditor5-utils/src/env';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. describe( 'ImageCaptionEditing', () => {
  17. let editor, model, doc, view;
  18. testUtils.createSinonSandbox();
  19. beforeEach( () => {
  20. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  21. testUtils.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( writer.createRangeIn( 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( writer.createRange( writer.createPositionAt( caption, 0 ), writer.createPositionAt( 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. model.change( writer => {
  208. const caption = writer.createElement( 'caption' );
  209. const image = writer.createElement( 'image', { src: '', alt: '' } );
  210. writer.insertText( 'foo bar', caption );
  211. writer.insert( caption, image );
  212. writer.insert( image, doc.getRoot() );
  213. } );
  214. expect( getModelData( model ) ).to.equal(
  215. '[<image alt="" src=""><caption>foo bar</caption></image>]<paragraph></paragraph>'
  216. );
  217. expect( getViewData( view ) ).to.equal(
  218. '[<figure class="ck-widget image" contenteditable="false">' +
  219. '<img alt="" src=""></img>' +
  220. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  221. 'contenteditable="true" data-placeholder="Enter image caption">' +
  222. 'foo bar' +
  223. '</figcaption>' +
  224. '</figure>]' +
  225. '<p></p>'
  226. );
  227. } );
  228. it( 'should not add caption element twice', () => {
  229. model.change( writer => {
  230. const image = writer.createElement( 'image', { src: '', alt: '' } );
  231. const caption = writer.createElement( 'caption' );
  232. // Since we are adding an empty image, this should trigger caption fixer.
  233. writer.insert( image, doc.getRoot() );
  234. // Add caption just after the image is inserted, in same batch.
  235. writer.insert( caption, image );
  236. } );
  237. // Check whether caption fixer added redundant caption.
  238. expect( getModelData( model ) ).to.equal(
  239. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  240. );
  241. expect( getViewData( view ) ).to.equal(
  242. '[<figure class="ck-widget image" contenteditable="false">' +
  243. '<img alt="" src=""></img>' +
  244. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  245. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  246. '</figure>]' +
  247. '<p></p>'
  248. );
  249. } );
  250. it( 'should do nothing for other changes than insert', () => {
  251. setModelData( model, '<image src=""><caption>foo bar</caption></image>' );
  252. const image = doc.getRoot().getChild( 0 );
  253. model.change( writer => {
  254. writer.setAttribute( 'alt', 'alt text', image );
  255. } );
  256. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  257. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  258. );
  259. } );
  260. } );
  261. describe( 'editing view', () => {
  262. it( 'image should have empty figcaption element when is selected', () => {
  263. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  264. expect( getViewData( view ) ).to.equal(
  265. '<p>foo</p>' +
  266. '[<figure class="ck-widget image" contenteditable="false">' +
  267. '<img src=""></img>' +
  268. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  269. 'contenteditable="true" data-placeholder="Enter image caption">' +
  270. '</figcaption>' +
  271. '</figure>]'
  272. );
  273. } );
  274. it( 'image should have empty figcaption element with hidden class when not selected', () => {
  275. setModelData( model, '<paragraph>[]foo</paragraph><image src=""><caption></caption></image>' );
  276. expect( getViewData( view ) ).to.equal(
  277. '<p>{}foo</p>' +
  278. '<figure class="ck-widget image" contenteditable="false">' +
  279. '<img src=""></img>' +
  280. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  281. 'contenteditable="true" data-placeholder="Enter image caption">' +
  282. '</figcaption>' +
  283. '</figure>'
  284. );
  285. } );
  286. it( 'should not add additional figcaption if one is already present', () => {
  287. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption>foo bar</caption></image>]' );
  288. expect( getViewData( view ) ).to.equal(
  289. '<p>foo</p>' +
  290. '[<figure class="ck-widget image" contenteditable="false">' +
  291. '<img src=""></img>' +
  292. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  293. 'contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  294. '</figure>]'
  295. );
  296. } );
  297. it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
  298. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  299. model.change( writer => {
  300. writer.setSelection( null );
  301. } );
  302. expect( getViewData( view ) ).to.equal(
  303. '<p>{}foo</p>' +
  304. '<figure class="ck-widget image" contenteditable="false">' +
  305. '<img src=""></img>' +
  306. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  307. 'contenteditable="true" data-placeholder="Enter image caption">' +
  308. '</figcaption>' +
  309. '</figure>'
  310. );
  311. } );
  312. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  313. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  314. model.change( writer => {
  315. writer.remove( doc.selection.getFirstRange() );
  316. } );
  317. expect( getViewData( view ) ).to.equal(
  318. '<figure class="ck-widget image" contenteditable="false">' +
  319. '<img src=""></img>' +
  320. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  321. 'contenteditable="true" data-placeholder="Enter image caption">' +
  322. '[]' +
  323. '</figcaption>' +
  324. '</figure>'
  325. );
  326. } );
  327. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  328. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  329. const image = doc.getRoot().getChild( 0 );
  330. model.change( writer => {
  331. writer.remove( doc.selection.getFirstRange() );
  332. writer.setSelection( writer.createRangeOn( image ) );
  333. } );
  334. expect( getViewData( view ) ).to.equal(
  335. '[<figure class="ck-widget image" contenteditable="false">' +
  336. '<img src=""></img>' +
  337. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  338. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  339. '</figure>]'
  340. );
  341. } );
  342. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  343. setModelData( model, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  344. const image = doc.getRoot().getChild( 1 );
  345. model.change( writer => {
  346. writer.setSelection( writer.createRangeOn( image ) );
  347. } );
  348. expect( getViewData( view ) ).to.equal(
  349. '<figure class="ck-widget image" contenteditable="false">' +
  350. '<img src=""></img>' +
  351. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  352. 'contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  353. '</figure>' +
  354. '[<figure class="ck-widget image" contenteditable="false">' +
  355. '<img src=""></img>' +
  356. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  357. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  358. '</figure>]'
  359. );
  360. } );
  361. describe( 'undo/redo integration', () => {
  362. it( 'should create view element after redo', () => {
  363. setModelData( model, '<paragraph>foo</paragraph><image src=""><caption>[foo bar baz]</caption></image>' );
  364. const modelRoot = doc.getRoot();
  365. const modelImage = modelRoot.getChild( 1 );
  366. const modelCaption = modelImage.getChild( 0 );
  367. // Remove text and selection from caption.
  368. model.change( writer => {
  369. writer.remove( writer.createRangeIn( modelCaption ) );
  370. writer.setSelection( null );
  371. } );
  372. // Check if there is no figcaption in the view.
  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. editor.execute( 'undo' );
  383. // Check if figcaption is back with contents.
  384. expect( getViewData( view ) ).to.equal(
  385. '<p>foo</p>' +
  386. '<figure class="ck-widget image" contenteditable="false">' +
  387. '<img src=""></img>' +
  388. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  389. 'contenteditable="true" data-placeholder="Enter image caption">' +
  390. '{foo bar baz}' +
  391. '</figcaption>' +
  392. '</figure>'
  393. );
  394. } );
  395. it( 'undo should work after inserting the image', () => {
  396. setModelData( model, '<paragraph>foo[]</paragraph>' );
  397. model.change( writer => {
  398. const image = writer.createElement( 'image', { src: '/foo.png' } );
  399. writer.insert( image, doc.getRoot() );
  400. } );
  401. expect( getModelData( model ) ).to.equal( '<image src="/foo.png"><caption></caption></image><paragraph>foo[]</paragraph>' );
  402. editor.execute( 'undo' );
  403. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph>' );
  404. } );
  405. } );
  406. } );
  407. } );