8
0

imagecaptionengine.js 18 KB

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