8
0

imagecaptionediting.js 18 KB

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