8
0

imagecaptionengine.js 18 KB

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