imagecaptioningengine.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 ImageCaptioningEngine from '../../src/imagecaptioning/imagecaptioningengine';
  7. import ImageEngine from '../../src/imageengine';
  8. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  10. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  11. describe( 'ImageCaptioningEngine', () => {
  12. let editor, document, viewDocument;
  13. beforeEach( () => {
  14. return VirtualTestEditor.create( {
  15. plugins: [ ImageCaptioningEngine, ImageEngine ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. document = editor.document;
  20. viewDocument = editor.editing.view;
  21. document.schema.registerItem( 'widget' );
  22. document.schema.allow( { name: 'widget', inside: '$root' } );
  23. document.schema.allow( { name: 'caption', inside: 'widget' } );
  24. document.schema.allow( { name: '$inline', inside: 'widget' } );
  25. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'widget' ).toElement( 'widget' );
  26. buildModelConverter().for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'widget' ).toElement( 'widget' );
  27. } );
  28. } );
  29. it( 'should be loaded', () => {
  30. expect( editor.plugins.get( ImageCaptioningEngine ) ).to.be.instanceOf( ImageCaptioningEngine );
  31. } );
  32. it( 'should set proper schema rules', () => {
  33. expect( document.schema.check( { name: 'caption', iniside: 'image' } ) ).to.be.true;
  34. expect( document.schema.check( { name: '$inline', inside: 'caption' } ) ).to.be.true;
  35. } );
  36. describe( 'data pipeline', () => {
  37. describe( 'view to model', () => {
  38. it( 'should convert figcaption inside image figure', () => {
  39. editor.setData( '<figure class="image"><img src="foo.png"/><figcaption>foo bar</figcaption></figure>' );
  40. expect( getModelData( document, { withoutSelection: true } ) )
  41. .to.equal( '<image src="foo.png"><caption>foo bar</caption></image>' );
  42. } );
  43. it( 'should add empty caption if there is no figcaption', () => {
  44. editor.setData( '<figure class="image"><img src="foo.png"/></figure>' );
  45. expect( getModelData( document, { withoutSelection: true } ) )
  46. .to.equal( '<image src="foo.png"><caption></caption></image>' );
  47. } );
  48. it( 'should not convert figcaption inside other elements than image', () => {
  49. editor.setData( '<widget><figcaption>foobar</figcaption></widget>' );
  50. expect( getModelData( document, { withoutSelection: true } ) )
  51. .to.equal( '<widget>foobar</widget>' );
  52. } );
  53. } );
  54. describe( 'model to view', () => {
  55. it( 'should convert caption element to figcaption', () => {
  56. setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
  57. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"><figcaption>Foo bar baz.</figcaption></figure>' );
  58. } );
  59. it( 'should not convert caption if it\'s empty', () => {
  60. setModelData( document, '<image src="img.png"><caption></caption></image>' );
  61. expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"></figure>' );
  62. } );
  63. it( 'should not convert caption from other elements', () => {
  64. setModelData( document, '<widget>foo bar<caption></caption></widget>' );
  65. expect( editor.getData() ).to.equal( '<widget>foo bar</widget>' );
  66. } );
  67. } );
  68. } );
  69. } );