8
0

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 { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  12. import env from '@ckeditor/ckeditor5-utils/src/env';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. describe( 'ImageCaptionEditing', () => {
  15. let editor, model, doc, view;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  19. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  20. return VirtualTestEditor
  21. .create( {
  22. plugins: [ ImageCaptionEditing, ImageEditing, UndoEditing, Paragraph ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. model = editor.model;
  27. doc = model.document;
  28. view = editor.editing.view;
  29. model.schema.register( 'widget' );
  30. model.schema.extend( 'widget', { allowIn: '$root' } );
  31. model.schema.extend( 'caption', { allowIn: 'widget' } );
  32. model.schema.extend( '$text', { allowIn: 'widget' } );
  33. editor.conversion.elementToElement( {
  34. model: 'widget',
  35. view: 'widget'
  36. } );
  37. } );
  38. } );
  39. it( 'should be loaded', () => {
  40. expect( editor.plugins.get( ImageCaptionEditing ) ).to.be.instanceOf( ImageCaptionEditing );
  41. } );
  42. it( 'should set proper schema rules', () => {
  43. expect( model.schema.checkChild( [ '$root', 'image' ], 'caption' ) ).to.be.true;
  44. expect( model.schema.checkChild( [ '$root', 'image', 'caption' ], '$text' ) ).to.be.true;
  45. expect( model.schema.isLimit( 'caption' ) ).to.be.true;
  46. expect( model.schema.checkChild( [ '$root', 'image', 'caption' ], 'caption' ) ).to.be.false;
  47. model.schema.extend( '$block', { allowAttributes: 'aligmnent' } );
  48. expect( model.schema.checkAttribute( [ '$root', 'image', 'caption' ], 'alignment' ) ).to.be.false;
  49. } );
  50. describe( 'data pipeline', () => {
  51. describe( 'view to model', () => {
  52. it( 'should convert figcaption inside image figure', () => {
  53. editor.setData( '<figure class="image"><img src="foo.png" /><figcaption>foo bar</figcaption></figure>' );
  54. expect( getModelData( model, { withoutSelection: true } ) )
  55. .to.equal( '<image src="foo.png"><caption>foo bar</caption></image>' );
  56. } );
  57. it( 'should add empty caption if there is no figcaption', () => {
  58. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  59. expect( getModelData( model, { withoutSelection: true } ) )
  60. .to.equal( '<image src="foo.png"><caption></caption></image>' );
  61. } );
  62. it( 'should not convert figcaption inside other elements than image', () => {
  63. editor.setData( '<widget><figcaption>foobar</figcaption></widget>' );
  64. expect( getModelData( model, { withoutSelection: true } ) )
  65. .to.equal( '<widget>foobar</widget>' );
  66. } );
  67. } );
  68. describe( 'model to view', () => {
  69. it( 'should convert caption element to figcaption', () => {
  70. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  71. expect( editor.getData() ).to.equal(
  72. '<figure class="image"><img src="img.png"><figcaption>Foo bar baz.</figcaption></figure>'
  73. );
  74. } );
  75. it( 'should not convert caption to figcaption if it\'s empty', () => {
  76. setModelData( model, '<image src="img.png"><caption></caption></image>' );
  77. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"></figure>' );
  78. } );
  79. it( 'should not convert caption from other elements', () => {
  80. setModelData( model, '<widget>foo bar<caption></caption></widget>' );
  81. expect( editor.getData() ).to.equal( '<widget>foo bar</widget>' );
  82. } );
  83. } );
  84. } );
  85. describe( 'editing pipeline', () => {
  86. describe( 'model to view', () => {
  87. it( 'should convert caption element to figcaption contenteditable', () => {
  88. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  89. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  90. '<figure class="ck-widget image" contenteditable="false">' +
  91. '<img src="img.png"></img>' +
  92. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  93. 'contenteditable="true" data-placeholder="Enter image caption">' +
  94. 'Foo bar baz.' +
  95. '</figcaption>' +
  96. '</figure>'
  97. );
  98. } );
  99. it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
  100. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  101. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  102. '<p>foo</p>' +
  103. '<figure class="ck-widget image" contenteditable="false">' +
  104. '<img src="img.png"></img>' +
  105. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  106. 'contenteditable="true" data-placeholder="Enter image caption">' +
  107. '</figcaption>' +
  108. '</figure>'
  109. );
  110. } );
  111. it( 'should not convert caption from other elements', () => {
  112. setModelData( model, '<widget>foo bar<caption></caption></widget>' );
  113. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  114. } );
  115. it( 'should not convert when element is already consumed', () => {
  116. editor.editing.downcastDispatcher.on(
  117. 'insert:caption',
  118. ( evt, data, conversionApi ) => {
  119. conversionApi.consumable.consume( data.item, 'insert' );
  120. const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
  121. const viewElement = conversionApi.writer.createAttributeElement( 'span' );
  122. const viewPosition = conversionApi.writer.createPositionAt( imageFigure, 'end' );
  123. conversionApi.mapper.bindElements( data.item, viewElement );
  124. conversionApi.writer.insert( viewPosition, viewElement );
  125. },
  126. { priority: 'high' }
  127. );
  128. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  129. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  130. '<figure class="ck-widget image" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
  131. );
  132. } );
  133. it( 'should show caption when something is inserted inside', () => {
  134. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  135. const image = doc.getRoot().getChild( 1 );
  136. const caption = image.getChild( 0 );
  137. model.change( writer => {
  138. writer.insertText( 'foo bar', caption );
  139. } );
  140. expect( getViewData( view ) ).to.equal(
  141. '<p>{}foo</p>' +
  142. '<figure class="ck-widget image" contenteditable="false">' +
  143. '<img src="img.png"></img>' +
  144. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  145. 'contenteditable="true" data-placeholder="Enter image caption">' +
  146. 'foo bar' +
  147. '</figcaption>' +
  148. '</figure>'
  149. );
  150. } );
  151. it( 'should hide when everything is removed from caption', () => {
  152. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  153. const image = doc.getRoot().getChild( 1 );
  154. const caption = image.getChild( 0 );
  155. model.change( writer => {
  156. writer.remove( writer.createRangeIn( caption ) );
  157. } );
  158. expect( getViewData( view ) ).to.equal(
  159. '<p>{}foo</p>' +
  160. '<figure class="ck-widget image" contenteditable="false">' +
  161. '<img src="img.png"></img>' +
  162. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-hidden ck-placeholder" ' +
  163. 'contenteditable="true" data-placeholder="Enter image caption">' +
  164. '</figcaption>' +
  165. '</figure>'
  166. );
  167. } );
  168. it( 'should show when not everything is removed from caption', () => {
  169. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  170. const image = doc.getRoot().getChild( 1 );
  171. const caption = image.getChild( 0 );
  172. model.change( writer => {
  173. writer.remove( writer.createRange( writer.createPositionAt( caption, 0 ), writer.createPositionAt( caption, 8 ) ) );
  174. } );
  175. expect( getViewData( view ) ).to.equal(
  176. '<p>{}foo</p>' +
  177. '<figure class="ck-widget image" contenteditable="false">' +
  178. '<img src="img.png"></img>' +
  179. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  180. 'contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
  181. '</figure>'
  182. );
  183. } );
  184. } );
  185. } );
  186. describe( 'inserting image to the document', () => {
  187. it( 'should add caption element if image does not have it', () => {
  188. model.change( writer => {
  189. writer.insertElement( 'image', { src: '', alt: '' }, doc.getRoot() );
  190. } );
  191. expect( getModelData( model ) ).to.equal(
  192. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  193. );
  194. expect( getViewData( view ) ).to.equal(
  195. '[<figure class="ck-widget image" contenteditable="false">' +
  196. '<img alt="" src=""></img>' +
  197. '<figcaption class="ck-editor__editable ck-editor__nested-editable ck-placeholder" ' +
  198. 'contenteditable="true" data-placeholder="Enter image caption">' +
  199. '</figcaption>' +
  200. '</figure>]' +
  201. '<p></p>'
  202. );
  203. } );
  204. it( 'should not add caption element if image already have it', () => {
  205. model.change( writer => {
  206. const caption = writer.createElement( 'caption' );
  207. const image = writer.createElement( 'image', { src: '', alt: '' } );
  208. writer.insertText( 'foo bar', caption );
  209. writer.insert( caption, image );
  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. model.change( writer => {
  228. const image = writer.createElement( 'image', { src: '', alt: '' } );
  229. const caption = writer.createElement( 'caption' );
  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( writer.createRangeOn( 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( writer.createRangeOn( 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( writer.createRangeIn( 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. } );