8
0

imagecaptionengine.js 18 KB

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