imagecaptionengine.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 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 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 { elementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/two-way-converters';
  17. describe( 'ImageCaptionEngine', () => {
  18. let editor, model, doc, view;
  19. beforeEach( () => {
  20. return VirtualTestEditor
  21. .create( {
  22. plugins: [ ImageCaptionEngine, ImageEngine, UndoEngine, 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. elementToElement( editor.conversion, {
  34. model: 'widget',
  35. view: 'widget'
  36. } );
  37. } );
  38. } );
  39. it( 'should be loaded', () => {
  40. expect( editor.plugins.get( ImageCaptionEngine ) ).to.be.instanceOf( ImageCaptionEngine );
  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-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  93. 'Foo bar baz.' +
  94. '</figcaption>' +
  95. '</figure>'
  96. );
  97. } );
  98. it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
  99. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  100. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  101. '<p>foo</p>' +
  102. '<figure class="ck-widget image" contenteditable="false">' +
  103. '<img src="img.png"></img>' +
  104. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  105. 'contenteditable="true" data-placeholder="Enter image caption">' +
  106. '</figcaption>' +
  107. '</figure>'
  108. );
  109. } );
  110. it( 'should not convert caption from other elements', () => {
  111. setModelData( model, '<widget>foo bar<caption></caption></widget>' );
  112. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  113. } );
  114. it( 'should not convert when element is already consumed', () => {
  115. editor.editing.downcastDispatcher.on(
  116. 'insert:caption',
  117. ( evt, data, consumable, conversionApi ) => {
  118. consumable.consume( data.item, 'insert' );
  119. const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
  120. const viewElement = new ViewAttributeElement( 'span' );
  121. const viewPosition = ViewPosition.createAt( imageFigure, 'end' );
  122. conversionApi.mapper.bindElements( data.item, viewElement );
  123. conversionApi.writer.insert( viewPosition, viewElement );
  124. },
  125. { priority: 'high' }
  126. );
  127. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  128. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  129. '<figure class="ck-widget image" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
  130. );
  131. } );
  132. it( 'should show caption when something is inserted inside', () => {
  133. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  134. const image = doc.getRoot().getChild( 1 );
  135. const caption = image.getChild( 0 );
  136. model.change( writer => {
  137. writer.insertText( 'foo bar', caption );
  138. } );
  139. expect( getViewData( view ) ).to.equal(
  140. '<p>{}foo</p>' +
  141. '<figure class="ck-widget image" contenteditable="false">' +
  142. '<img src="img.png"></img>' +
  143. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  144. 'foo bar' +
  145. '</figcaption>' +
  146. '</figure>'
  147. );
  148. } );
  149. it( 'should hide when everything is removed from caption', () => {
  150. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  151. const image = doc.getRoot().getChild( 1 );
  152. const caption = image.getChild( 0 );
  153. model.change( writer => {
  154. writer.remove( ModelRange.createIn( caption ) );
  155. } );
  156. expect( getViewData( view ) ).to.equal(
  157. '<p>{}foo</p>' +
  158. '<figure class="ck-widget image" contenteditable="false">' +
  159. '<img src="img.png"></img>' +
  160. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  161. 'contenteditable="true" data-placeholder="Enter image caption">' +
  162. '</figcaption>' +
  163. '</figure>'
  164. );
  165. } );
  166. it( 'should show when not everything is removed from caption', () => {
  167. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  168. const image = doc.getRoot().getChild( 1 );
  169. const caption = image.getChild( 0 );
  170. model.change( writer => {
  171. writer.remove( ModelRange.createFromParentsAndOffsets( caption, 0, caption, 8 ) );
  172. } );
  173. expect( getViewData( view ) ).to.equal(
  174. '<p>{}foo</p>' +
  175. '<figure class="ck-widget image" contenteditable="false">' +
  176. '<img src="img.png"></img>' +
  177. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
  178. '</figure>'
  179. );
  180. } );
  181. } );
  182. } );
  183. describe( 'inserting image to the document', () => {
  184. it( 'should add caption element if image does not have it', () => {
  185. model.change( writer => {
  186. writer.insertElement( 'image', { src: '', alt: '' }, doc.getRoot() );
  187. } );
  188. expect( getModelData( model ) ).to.equal(
  189. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  190. );
  191. expect( getViewData( view ) ).to.equal(
  192. '[<figure class="ck-widget image" contenteditable="false">' +
  193. '<img alt="" src=""></img>' +
  194. '<figcaption class="ck-editable ck-placeholder" ' +
  195. 'contenteditable="true" data-placeholder="Enter image caption">' +
  196. '</figcaption>' +
  197. '</figure>]' +
  198. '<p></p>'
  199. );
  200. } );
  201. it( 'should not add caption element if image already have it', () => {
  202. const caption = new ModelElement( 'caption', null, 'foo bar' );
  203. const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
  204. model.change( writer => {
  205. writer.insert( image, doc.getRoot() );
  206. } );
  207. expect( getModelData( model ) ).to.equal(
  208. '[<image alt="" src=""><caption>foo bar</caption></image>]<paragraph></paragraph>'
  209. );
  210. expect( getViewData( view ) ).to.equal(
  211. '[<figure class="ck-widget image" contenteditable="false">' +
  212. '<img alt="" src=""></img>' +
  213. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  214. 'foo bar' +
  215. '</figcaption>' +
  216. '</figure>]' +
  217. '<p></p>'
  218. );
  219. } );
  220. it( 'should not add caption element twice', () => {
  221. const image = new ModelElement( 'image', { src: '', alt: '' } );
  222. const caption = new ModelElement( 'caption' );
  223. model.change( writer => {
  224. // Since we are adding an empty image, this should trigger caption fixer.
  225. writer.insert( image, doc.getRoot() );
  226. // Add caption just after the image is inserted, in same batch.
  227. writer.insert( caption, image );
  228. } );
  229. // Check whether caption fixer added redundant caption.
  230. expect( getModelData( model ) ).to.equal(
  231. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  232. );
  233. expect( getViewData( view ) ).to.equal(
  234. '[<figure class="ck-widget image" contenteditable="false">' +
  235. '<img alt="" src=""></img>' +
  236. '<figcaption class="ck-editable ck-placeholder" ' +
  237. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  238. '</figure>]' +
  239. '<p></p>'
  240. );
  241. } );
  242. it( 'should do nothing for other changes than insert', () => {
  243. setModelData( model, '<image src=""><caption>foo bar</caption></image>' );
  244. const image = doc.getRoot().getChild( 0 );
  245. model.change( writer => {
  246. writer.setAttribute( 'alt', 'alt text', image );
  247. } );
  248. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  249. '<image alt="alt text" src=""><caption>foo bar</caption></image>'
  250. );
  251. } );
  252. } );
  253. describe( 'editing view', () => {
  254. it( 'image should have empty figcaption element when is selected', () => {
  255. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  256. expect( getViewData( view ) ).to.equal(
  257. '<p>foo</p>' +
  258. '[<figure class="ck-widget image" contenteditable="false">' +
  259. '<img src=""></img>' +
  260. '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  261. '</figcaption>' +
  262. '</figure>]'
  263. );
  264. } );
  265. it( 'image should have empty figcaption element with hidden class when not selected', () => {
  266. setModelData( model, '<paragraph>[]foo</paragraph><image src=""><caption></caption></image>' );
  267. expect( getViewData( view ) ).to.equal(
  268. '<p>{}foo</p>' +
  269. '<figure class="ck-widget image" contenteditable="false">' +
  270. '<img src=""></img>' +
  271. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  272. 'contenteditable="true" data-placeholder="Enter image caption">' +
  273. '</figcaption>' +
  274. '</figure>'
  275. );
  276. } );
  277. it( 'should not add additional figcaption if one is already present', () => {
  278. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption>foo bar</caption></image>]' );
  279. expect( getViewData( view ) ).to.equal(
  280. '<p>foo</p>' +
  281. '[<figure class="ck-widget image" contenteditable="false">' +
  282. '<img src=""></img>' +
  283. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  284. '</figure>]'
  285. );
  286. } );
  287. it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
  288. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  289. model.change( writer => {
  290. writer.setSelection( null );
  291. } );
  292. expect( getViewData( view ) ).to.equal(
  293. '<p>{}foo</p>' +
  294. '<figure class="ck-widget image" contenteditable="false">' +
  295. '<img src=""></img>' +
  296. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  297. 'contenteditable="true" data-placeholder="Enter image caption">' +
  298. '</figcaption>' +
  299. '</figure>'
  300. );
  301. } );
  302. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  303. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  304. model.change( writer => {
  305. writer.remove( doc.selection.getFirstRange() );
  306. } );
  307. expect( getViewData( view ) ).to.equal(
  308. '<figure class="ck-widget image" contenteditable="false">' +
  309. '<img src=""></img>' +
  310. '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  311. '[]' +
  312. '</figcaption>' +
  313. '</figure>'
  314. );
  315. } );
  316. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  317. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  318. const image = doc.getRoot().getChild( 0 );
  319. model.change( writer => {
  320. writer.remove( doc.selection.getFirstRange() );
  321. writer.setSelection( ModelRange.createOn( image ) );
  322. } );
  323. expect( getViewData( view ) ).to.equal(
  324. '[<figure class="ck-widget image" contenteditable="false">' +
  325. '<img src=""></img>' +
  326. '<figcaption class="ck-editable ck-placeholder" ' +
  327. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  328. '</figure>]'
  329. );
  330. } );
  331. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  332. setModelData( model, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  333. const image = doc.getRoot().getChild( 1 );
  334. model.change( writer => {
  335. writer.setSelection( ModelRange.createOn( image ) );
  336. } );
  337. expect( getViewData( view ) ).to.equal(
  338. '<figure class="ck-widget image" contenteditable="false">' +
  339. '<img src=""></img>' +
  340. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  341. '</figure>' +
  342. '[<figure class="ck-widget image" contenteditable="false">' +
  343. '<img src=""></img>' +
  344. '<figcaption class="ck-editable ck-placeholder" ' +
  345. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  346. '</figure>]'
  347. );
  348. } );
  349. describe( 'undo/redo integration', () => {
  350. it( 'should create view element after redo', () => {
  351. setModelData( model, '<paragraph>foo</paragraph><image src=""><caption>[foo bar baz]</caption></image>' );
  352. const modelRoot = doc.getRoot();
  353. const modelImage = modelRoot.getChild( 1 );
  354. const modelCaption = modelImage.getChild( 0 );
  355. // Remove text and selection from caption.
  356. model.change( writer => {
  357. writer.remove( ModelRange.createIn( modelCaption ) );
  358. writer.setSelection( null );
  359. } );
  360. // Check if there is no figcaption in the view.
  361. expect( getViewData( view ) ).to.equal(
  362. '<p>{}foo</p>' +
  363. '<figure class="ck-widget image" contenteditable="false">' +
  364. '<img src=""></img>' +
  365. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  366. 'contenteditable="true" data-placeholder="Enter image caption">' +
  367. '</figcaption>' +
  368. '</figure>'
  369. );
  370. editor.execute( 'undo' );
  371. // Check if figcaption is back with contents.
  372. expect( getViewData( view ) ).to.equal(
  373. '<p>foo</p>' +
  374. '<figure class="ck-widget image" contenteditable="false">' +
  375. '<img src=""></img>' +
  376. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  377. '{foo bar baz}' +
  378. '</figcaption>' +
  379. '</figure>'
  380. );
  381. } );
  382. it( 'undo should work after inserting the image', () => {
  383. const image = new ModelElement( 'image' );
  384. image.setAttribute( 'src', '/foo.png' );
  385. setModelData( model, '<paragraph>foo[]</paragraph>' );
  386. model.change( writer => {
  387. writer.insert( image, doc.getRoot() );
  388. } );
  389. expect( getModelData( model ) ).to.equal( '<image src="/foo.png"><caption></caption></image><paragraph>foo[]</paragraph>' );
  390. editor.execute( 'undo' );
  391. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph>' );
  392. } );
  393. } );
  394. } );
  395. } );