imagecaptionengine.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 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. 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( viewDocument, { withoutSelection: true } ) ).to.equal(
  96. '<figure class="ck-widget image" contenteditable="false">' +
  97. '<img src="img.png"></img>' +
  98. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  99. 'Foo bar baz.' +
  100. '</figcaption>' +
  101. '</figure>'
  102. );
  103. } );
  104. it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
  105. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  106. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  107. '<p>foo</p>' +
  108. '<figure class="ck-widget image" contenteditable="false">' +
  109. '<img src="img.png"></img>' +
  110. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  111. 'contenteditable="true" data-placeholder="Enter image caption">' +
  112. '</figcaption>' +
  113. '</figure>'
  114. );
  115. } );
  116. it( 'should not convert caption from other elements', () => {
  117. setModelData( model, '<widget>foo bar<caption></caption></widget>' );
  118. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
  119. } );
  120. it( 'should not convert when element is already consumed', () => {
  121. editor.editing.modelToView.on(
  122. 'insert:caption',
  123. ( evt, data, consumable, conversionApi ) => {
  124. consumable.consume( data.item, 'insert' );
  125. const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
  126. const viewElement = new ViewAttributeElement( 'span' );
  127. const viewPosition = ViewPosition.createAt( imageFigure, 'end' );
  128. conversionApi.mapper.bindElements( data.item, viewElement );
  129. viewWriter.insert( viewPosition, viewElement );
  130. },
  131. { priority: 'high' }
  132. );
  133. setModelData( model, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  134. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  135. '<figure class="ck-widget image" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
  136. );
  137. } );
  138. it( 'should show caption when something is inserted inside', () => {
  139. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
  140. const image = doc.getRoot().getChild( 1 );
  141. const caption = image.getChild( 0 );
  142. model.change( writer => {
  143. writer.insertText( 'foo bar', caption );
  144. } );
  145. expect( getViewData( viewDocument ) ).to.equal(
  146. '<p>{}foo</p>' +
  147. '<figure class="ck-widget image" contenteditable="false">' +
  148. '<img src="img.png"></img>' +
  149. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  150. 'foo bar' +
  151. '</figcaption>' +
  152. '</figure>'
  153. );
  154. } );
  155. it( 'should hide when everything is removed from caption', () => {
  156. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  157. const image = doc.getRoot().getChild( 1 );
  158. const caption = image.getChild( 0 );
  159. model.change( writer => {
  160. writer.remove( ModelRange.createIn( caption ) );
  161. } );
  162. expect( getViewData( viewDocument ) ).to.equal(
  163. '<p>{}foo</p>' +
  164. '<figure class="ck-widget image" contenteditable="false">' +
  165. '<img src="img.png"></img>' +
  166. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  167. 'contenteditable="true" data-placeholder="Enter image caption">' +
  168. '</figcaption>' +
  169. '</figure>'
  170. );
  171. } );
  172. it( 'should show when not everything is removed from caption', () => {
  173. setModelData( model, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
  174. const image = doc.getRoot().getChild( 1 );
  175. const caption = image.getChild( 0 );
  176. model.change( writer => {
  177. writer.remove( ModelRange.createFromParentsAndOffsets( caption, 0, caption, 8 ) );
  178. } );
  179. expect( getViewData( viewDocument ) ).to.equal(
  180. '<p>{}foo</p>' +
  181. '<figure class="ck-widget image" contenteditable="false">' +
  182. '<img src="img.png"></img>' +
  183. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
  184. '</figure>'
  185. );
  186. } );
  187. } );
  188. } );
  189. describe( 'inserting image to the document', () => {
  190. it( 'should add caption element if image does not have it', () => {
  191. model.change( writer => {
  192. writer.insertElement( 'image', { src: '', alt: '' }, doc.getRoot() );
  193. } );
  194. expect( getModelData( model ) ).to.equal(
  195. '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
  196. );
  197. expect( getViewData( viewDocument ) ).to.equal(
  198. '[<figure class="ck-widget image" contenteditable="false">' +
  199. '<img alt="" src=""></img>' +
  200. '<figcaption class="ck-editable ck-placeholder" ' +
  201. 'contenteditable="true" data-placeholder="Enter image caption">' +
  202. '</figcaption>' +
  203. '</figure>]' +
  204. '<p></p>'
  205. );
  206. } );
  207. it( 'should not add caption element if image already have it', () => {
  208. const caption = new ModelElement( 'caption', null, 'foo bar' );
  209. const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
  210. model.change( writer => {
  211. writer.insert( image, doc.getRoot() );
  212. } );
  213. expect( getModelData( model ) ).to.equal(
  214. '[<image alt="" src=""><caption>foo bar</caption></image>]<paragraph></paragraph>'
  215. );
  216. expect( getViewData( viewDocument ) ).to.equal(
  217. '[<figure class="ck-widget image" contenteditable="false">' +
  218. '<img alt="" src=""></img>' +
  219. '<figcaption class="ck-editable" 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. const image = new ModelElement( 'image', { src: '', alt: '' } );
  228. const caption = new ModelElement( 'caption' );
  229. model.change( writer => {
  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( viewDocument ) ).to.equal(
  240. '[<figure class="ck-widget image" contenteditable="false">' +
  241. '<img alt="" src=""></img>' +
  242. '<figcaption class="ck-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( viewDocument ) ).to.equal(
  263. '<p>foo</p>' +
  264. '[<figure class="ck-widget image" contenteditable="false">' +
  265. '<img src=""></img>' +
  266. '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  267. '</figcaption>' +
  268. '</figure>]'
  269. );
  270. } );
  271. it( 'image should have empty figcaption element with hidden class when not selected', () => {
  272. setModelData( model, '<paragraph>[]foo</paragraph><image src=""><caption></caption></image>' );
  273. expect( getViewData( viewDocument ) ).to.equal(
  274. '<p>{}foo</p>' +
  275. '<figure class="ck-widget image" contenteditable="false">' +
  276. '<img src=""></img>' +
  277. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  278. 'contenteditable="true" data-placeholder="Enter image caption">' +
  279. '</figcaption>' +
  280. '</figure>'
  281. );
  282. } );
  283. it( 'should not add additional figcaption if one is already present', () => {
  284. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption>foo bar</caption></image>]' );
  285. expect( getViewData( viewDocument ) ).to.equal(
  286. '<p>foo</p>' +
  287. '[<figure class="ck-widget image" contenteditable="false">' +
  288. '<img src=""></img>' +
  289. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  290. '</figure>]'
  291. );
  292. } );
  293. it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
  294. setModelData( model, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
  295. model.change( writer => {
  296. writer.setSelection( null );
  297. } );
  298. expect( getViewData( viewDocument ) ).to.equal(
  299. '<p>{}foo</p>' +
  300. '<figure class="ck-widget image" contenteditable="false">' +
  301. '<img src=""></img>' +
  302. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  303. 'contenteditable="true" data-placeholder="Enter image caption">' +
  304. '</figcaption>' +
  305. '</figure>'
  306. );
  307. } );
  308. it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
  309. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  310. model.change( writer => {
  311. writer.remove( doc.selection.getFirstRange() );
  312. } );
  313. expect( getViewData( viewDocument ) ).to.equal(
  314. '<figure class="ck-widget image" contenteditable="false">' +
  315. '<img src=""></img>' +
  316. '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
  317. '[]' +
  318. '</figcaption>' +
  319. '</figure>'
  320. );
  321. } );
  322. it( 'should not remove figcaption when selection is moved from it to its image', () => {
  323. setModelData( model, '<image src=""><caption>[foo bar]</caption></image>' );
  324. const image = doc.getRoot().getChild( 0 );
  325. model.change( writer => {
  326. writer.remove( doc.selection.getFirstRange() );
  327. writer.setSelection( ModelRange.createOn( image ) );
  328. } );
  329. expect( getViewData( viewDocument ) ).to.equal(
  330. '[<figure class="ck-widget image" contenteditable="false">' +
  331. '<img src=""></img>' +
  332. '<figcaption class="ck-editable ck-placeholder" ' +
  333. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  334. '</figure>]'
  335. );
  336. } );
  337. it( 'should not remove figcaption when selection is moved from it to other image', () => {
  338. setModelData( model, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
  339. const image = doc.getRoot().getChild( 1 );
  340. model.change( writer => {
  341. writer.setSelection( ModelRange.createOn( image ) );
  342. } );
  343. expect( getViewData( viewDocument ) ).to.equal(
  344. '<figure class="ck-widget image" contenteditable="false">' +
  345. '<img src=""></img>' +
  346. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
  347. '</figure>' +
  348. '[<figure class="ck-widget image" contenteditable="false">' +
  349. '<img src=""></img>' +
  350. '<figcaption class="ck-editable ck-placeholder" ' +
  351. 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
  352. '</figure>]'
  353. );
  354. } );
  355. describe( 'undo/redo integration', () => {
  356. it( 'should create view element after redo', () => {
  357. setModelData( model, '<paragraph>foo</paragraph><image src=""><caption>[foo bar baz]</caption></image>' );
  358. const modelRoot = doc.getRoot();
  359. const modelImage = modelRoot.getChild( 1 );
  360. const modelCaption = modelImage.getChild( 0 );
  361. // Remove text and selection from caption.
  362. model.change( writer => {
  363. writer.remove( ModelRange.createIn( modelCaption ) );
  364. writer.setSelection( null );
  365. } );
  366. // Check if there is no figcaption in the view.
  367. expect( getViewData( viewDocument ) ).to.equal(
  368. '<p>{}foo</p>' +
  369. '<figure class="ck-widget image" contenteditable="false">' +
  370. '<img src=""></img>' +
  371. '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
  372. 'contenteditable="true" data-placeholder="Enter image caption">' +
  373. '</figcaption>' +
  374. '</figure>'
  375. );
  376. editor.execute( 'undo' );
  377. // Check if figcaption is back with contents.
  378. expect( getViewData( viewDocument ) ).to.equal(
  379. '<p>foo</p>' +
  380. '<figure class="ck-widget image" contenteditable="false">' +
  381. '<img src=""></img>' +
  382. '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
  383. '{foo bar baz}' +
  384. '</figcaption>' +
  385. '</figure>'
  386. );
  387. } );
  388. it( 'undo should work after inserting the image', () => {
  389. const image = new ModelElement( 'image' );
  390. image.setAttribute( 'src', '/foo.png' );
  391. setModelData( model, '<paragraph>foo[]</paragraph>' );
  392. model.change( writer => {
  393. writer.insert( image, doc.getRoot() );
  394. } );
  395. expect( getModelData( model ) ).to.equal( '<image src="/foo.png"><caption></caption></image><paragraph>foo[]</paragraph>' );
  396. editor.execute( 'undo' );
  397. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph>' );
  398. } );
  399. } );
  400. } );
  401. } );