imagecaptionengine.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 ImageCaptionEngine from '../../src/imagecaption/imagecaptionengine';
  7. import ImageEngine from '../../src/image/imageengine';
  8. import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
  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 viewWriter from '@ckeditor/ckeditor5-engine/src/view/writer';
  13. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  14. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  15. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  17. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  18. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  19. describe( 'ImageCaptionEngine', () => {
  20. let editor, model, doc, viewDocument;
  21. beforeEach( () => {
  22. return VirtualTestEditor
  23. .create( {
  24. plugins: [ ImageCaptionEngine, ImageEngine, UndoEngine, Paragraph ]
  25. } )
  26. .then( newEditor => {
  27. editor = newEditor;
  28. model = editor.model;
  29. doc = model.document;
  30. viewDocument = 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. buildViewConverter()
  36. .for( editor.data.viewToModel )
  37. .fromElement( 'widget' )
  38. .toElement( 'widget' );
  39. buildModelConverter()
  40. .for( editor.data.modelToView, editor.editing.modelToView )
  41. .fromElement( 'widget' )
  42. .toElement( 'widget' );
  43. } );
  44. } );
  45. it( 'should be loaded', () => {
  46. expect( editor.plugins.get( ImageCaptionEngine ) ).to.be.instanceOf( ImageCaptionEngine );
  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. } );
  54. describe( 'data pipeline', () => {
  55. describe( 'view to model', () => {
  56. it( 'should convert figcaption inside image figure', () => {
  57. editor.setData( '<figure class="image"><img src="foo.png" /><figcaption>foo bar</figcaption></figure>' );
  58. expect( getModelData( model, { withoutSelection: true } ) )
  59. .to.equal( '<image src="foo.png"><caption>foo bar</caption></image>' );
  60. } );
  61. it( 'should add empty caption if there is no figcaption', () => {
  62. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  63. expect( getModelData( model, { withoutSelection: true } ) )
  64. .to.equal( '<image src="foo.png"><caption></caption></image>' );
  65. } );
  66. it( 'should not convert figcaption inside other elements than image', () => {
  67. editor.setData( '<widget><figcaption>foobar</figcaption></widget>' );
  68. expect( getModelData( model, { withoutSelection: true } ) )
  69. .to.equal( '<widget>foobar</widget>' );
  70. } );
  71. } );
  72. describe( 'model to view', () => {
  73. it( 'should convert caption element to figcaption', () => {
  74. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  75. expect( editor.getData() ).to.equal(
  76. '<figure class="image"><img src="img.png"><figcaption>Foo bar baz.</figcaption></figure>'
  77. );
  78. } );
  79. it( 'should not convert caption to figcaption if it\'s empty', () => {
  80. setModelData( model, '<image src="img.png"><caption></caption></image>' );
  81. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"></figure>' );
  82. } );
  83. it( 'should not convert caption from other elements', () => {
  84. setModelData( model, '<widget>foo bar<caption></caption></widget>' );
  85. expect( editor.getData() ).to.equal( '<widget>foo bar</widget>' );
  86. } );
  87. } );
  88. } );
  89. describe( 'editing pipeline', () => {
  90. describe( 'model to view', () => {
  91. it( 'should convert caption element to figcaption contenteditable', () => {
  92. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  93. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  94. '<figure class="ck-widget image" contenteditable="false">' +
  95. '<img src="img.png"></img>' +
  96. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  97. 'Foo bar baz.' +
  98. '</figcaption>' +
  99. '</figure>'
  100. );
  101. } );
  102. it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
  103. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  104. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  105. '<p>foo</p>' +
  106. '<figure class="ck-widget image" contenteditable="false">' +
  107. '<img src="img.png"></img>' +
  108. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  109. 'contenteditable="true" data-placeholder="Enter image caption">' +
  110. '</figcaption>' +
  111. '</figure>'
  112. );
  113. } );
  114. it( 'should not convert caption from other elements', () => {
  115. setModelData( model, '<widget>foo bar<caption></caption></widget>' );
  116. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  117. } );
  118. it( 'should not convert when element is already consumed', () => {
  119. editor.editing.modelToView.on(
  120. 'insert:caption',
  121. ( evt, data, consumable, conversionApi ) => {
  122. consumable.consume( data.item, 'insert' );
  123. const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
  124. const viewElement = new ViewAttributeElement( 'span' );
  125. const viewPosition = ViewPosition.createAt( imageFigure, 'end' );
  126. conversionApi.mapper.bindElements( data.item, viewElement );
  127. viewWriter.insert( viewPosition, viewElement );
  128. },
  129. { priority: 'high' }
  130. );
  131. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  132. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  133. '<figure class="ck-widget image" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
  134. );
  135. } );
  136. it( 'should show caption when something is inserted inside', () => {
  137. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  138. const image = doc.getRoot().getChild( 1 );
  139. const caption = image.getChild( 0 );
  140. model.change( writer => {
  141. writer.insertText( 'foo bar', caption );
  142. } );
  143. expect( getViewData( viewDocument ) ).to.equal(
  144. '<p>{}foo</p>' +
  145. '<figure class="ck-widget image" contenteditable="false">' +
  146. '<img src="img.png"></img>' +
  147. '<figcaption class="ck-editable" 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( viewDocument ) ).to.equal(
  161. '<p>{}foo</p>' +
  162. '<figure class="ck-widget image" contenteditable="false">' +
  163. '<img src="img.png"></img>' +
  164. '<figcaption class="ck-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( viewDocument ) ).to.equal(
  178. '<p>{}foo</p>' +
  179. '<figure class="ck-widget image" contenteditable="false">' +
  180. '<img src="img.png"></img>' +
  181. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
  182. '</figure>'
  183. );
  184. } );
  185. } );
  186. } );
  187. describe( 'inserting image to the document', () => {
  188. it( 'should add caption element if image does not have it', () => {
  189. model.change( writer => {
  190. writer.insertElement( 'image', { src: '', alt: '' }, doc.getRoot() );
  191. } );
  192. expect( getModelData( model ) ).to.equal(
  193. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  194. );
  195. expect( getViewData( viewDocument ) ).to.equal(
  196. '[<figure class="ck-widget image" contenteditable="false">' +
  197. '<img alt="" src=""></img>' +
  198. '<figcaption class="ck-editable ck-placeholder" ' +
  199. 'contenteditable="true" data-placeholder="Enter image caption">' +
  200. '</figcaption>' +
  201. '</figure>]' +
  202. '<p></p>'
  203. );
  204. } );
  205. it( 'should not add caption element if image already have it', () => {
  206. const caption = new ModelElement( 'caption', null, 'foo bar' );
  207. const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
  208. model.change( writer => {
  209. writer.insert( image, doc.getRoot() );
  210. } );
  211. expect( getModelData( model ) ).to.equal(
  212. '[<image alt="" src=""><caption>foo bar</caption></image>]<paragraph></paragraph>'
  213. );
  214. expect( getViewData( viewDocument ) ).to.equal(
  215. '[<figure class="ck-widget image" contenteditable="false">' +
  216. '<img alt="" src=""></img>' +
  217. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  218. 'foo bar' +
  219. '</figcaption>' +
  220. '</figure>]' +
  221. '<p></p>'
  222. );
  223. } );
  224. it( 'should not add caption element twice', () => {
  225. const image = new ModelElement( 'image', { src: '', alt: '' } );
  226. const caption = new ModelElement( 'caption' );
  227. model.change( writer => {
  228. // Since we are adding an empty image, this should trigger caption fixer.
  229. writer.insert( image, doc.getRoot() );
  230. // Add caption just after the image is inserted, in same batch.
  231. writer.insert( caption, image );
  232. } );
  233. // Check whether caption fixer added redundant caption.
  234. expect( getModelData( model ) ).to.equal(
  235. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  236. );
  237. expect( getViewData( viewDocument ) ).to.equal(
  238. '[<figure class="ck-widget image" contenteditable="false">' +
  239. '<img alt="" src=""></img>' +
  240. '<figcaption class="ck-editable ck-placeholder" ' +
  241. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  242. '</figure>]' +
  243. '<p></p>'
  244. );
  245. } );
  246. it( 'should do nothing for other changes than insert', () => {
  247. setModelData( model, '<image src=""><caption>foo bar</caption></image>' );
  248. const image = doc.getRoot().getChild( 0 );
  249. model.change( writer => {
  250. writer.setAttribute( 'alt', 'alt text', image );
  251. } );
  252. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  253. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  254. );
  255. } );
  256. } );
  257. describe( 'editing view', () => {
  258. it( 'image should have empty figcaption element when is selected', () => {
  259. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  260. expect( getViewData( viewDocument ) ).to.equal(
  261. '<p>foo</p>' +
  262. '[<figure class="ck-widget image" contenteditable="false">' +
  263. '<img src=""></img>' +
  264. '<figcaption class="ck-editable ck-placeholder" 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( viewDocument ) ).to.equal(
  272. '<p>{}foo</p>' +
  273. '<figure class="ck-widget image" contenteditable="false">' +
  274. '<img src=""></img>' +
  275. '<figcaption class="ck-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( viewDocument ) ).to.equal(
  284. '<p>foo</p>' +
  285. '[<figure class="ck-widget image" contenteditable="false">' +
  286. '<img src=""></img>' +
  287. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  288. '</figure>]'
  289. );
  290. } );
  291. it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
  292. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  293. model.change( () => {
  294. doc.selection.removeAllRanges();
  295. } );
  296. expect( getViewData( viewDocument ) ).to.equal(
  297. '<p>{}foo</p>' +
  298. '<figure class="ck-widget image" contenteditable="false">' +
  299. '<img src=""></img>' +
  300. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  301. 'contenteditable="true" data-placeholder="Enter image caption">' +
  302. '</figcaption>' +
  303. '</figure>'
  304. );
  305. } );
  306. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  307. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  308. model.change( writer => {
  309. writer.remove( doc.selection.getFirstRange() );
  310. } );
  311. expect( getViewData( viewDocument ) ).to.equal(
  312. '<figure class="ck-widget image" contenteditable="false">' +
  313. '<img src=""></img>' +
  314. '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  315. '[]' +
  316. '</figcaption>' +
  317. '</figure>'
  318. );
  319. } );
  320. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  321. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  322. const image = doc.getRoot().getChild( 0 );
  323. model.change( writer => {
  324. writer.remove( doc.selection.getFirstRange() );
  325. doc.selection.setRanges( [ ModelRange.createOn( image ) ] );
  326. } );
  327. expect( getViewData( viewDocument ) ).to.equal(
  328. '[<figure class="ck-widget image" contenteditable="false">' +
  329. '<img src=""></img>' +
  330. '<figcaption class="ck-editable ck-placeholder" ' +
  331. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  332. '</figure>]'
  333. );
  334. } );
  335. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  336. setModelData( model, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  337. const image = doc.getRoot().getChild( 1 );
  338. model.change( () => {
  339. doc.selection.setRanges( [ ModelRange.createOn( image ) ] );
  340. } );
  341. expect( getViewData( viewDocument ) ).to.equal(
  342. '<figure class="ck-widget image" contenteditable="false">' +
  343. '<img src=""></img>' +
  344. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  345. '</figure>' +
  346. '[<figure class="ck-widget image" contenteditable="false">' +
  347. '<img src=""></img>' +
  348. '<figcaption class="ck-editable ck-placeholder" ' +
  349. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  350. '</figure>]'
  351. );
  352. } );
  353. describe( 'undo/redo integration', () => {
  354. it( 'should create view element after redo', () => {
  355. setModelData( model, '<paragraph>foo</paragraph><image src=""><caption>[foo bar baz]</caption></image>' );
  356. const modelRoot = doc.getRoot();
  357. const modelImage = modelRoot.getChild( 1 );
  358. const modelCaption = modelImage.getChild( 0 );
  359. // Remove text and selection from caption.
  360. model.change( writer => {
  361. writer.remove( ModelRange.createIn( modelCaption ) );
  362. doc.selection.removeAllRanges();
  363. } );
  364. // Check if there is no figcaption in the view.
  365. expect( getViewData( viewDocument ) ).to.equal(
  366. '<p>{}foo</p>' +
  367. '<figure class="ck-widget image" contenteditable="false">' +
  368. '<img src=""></img>' +
  369. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  370. 'contenteditable="true" data-placeholder="Enter image caption">' +
  371. '</figcaption>' +
  372. '</figure>'
  373. );
  374. editor.execute( 'undo' );
  375. // Check if figcaption is back with contents.
  376. expect( getViewData( viewDocument ) ).to.equal(
  377. '<p>foo</p>' +
  378. '<figure class="ck-widget image" contenteditable="false">' +
  379. '<img src=""></img>' +
  380. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  381. '{foo bar baz}' +
  382. '</figcaption>' +
  383. '</figure>'
  384. );
  385. } );
  386. it( 'undo should work after inserting the image', () => {
  387. const image = new ModelElement( 'image' );
  388. image.setAttribute( 'src', '/foo.png' );
  389. setModelData( model, '<paragraph>foo[]</paragraph>' );
  390. model.change( writer => {
  391. writer.insert( image, doc.getRoot() );
  392. } );
  393. expect( getModelData( model ) ).to.equal( '<image src="/foo.png"><caption></caption></image><paragraph>foo[]</paragraph>' );
  394. editor.execute( 'undo' );
  395. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph>' );
  396. } );
  397. } );
  398. } );
  399. } );