converters.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import {
  6. viewFigureToModel,
  7. createImageAttributeConverter,
  8. } from '../../src/image/converters';
  9. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  10. import { createImageViewElement } from '../../src/image/imageengine';
  11. import { toImageWidget } from '../../src/image/utils';
  12. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  13. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  14. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  15. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  16. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. describe( 'Image converters', () => {
  18. let editor, model, document, viewDocument;
  19. beforeEach( () => {
  20. return VirtualTestEditor.create()
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. document = model.document;
  25. viewDocument = editor.editing.view;
  26. const schema = model.schema;
  27. schema.register( 'image', {
  28. allowWhere: '$block',
  29. allowAttributes: [ 'alt', 'src' ],
  30. isObject: true,
  31. isBlock: true
  32. } );
  33. buildModelConverter().for( editor.editing.modelToView )
  34. .fromElement( 'image' )
  35. .toElement( () => toImageWidget( createImageViewElement() ) );
  36. createImageAttributeConverter( [ editor.editing.modelToView ], 'src' );
  37. createImageAttributeConverter( [ editor.editing.modelToView ], 'alt' );
  38. } );
  39. } );
  40. describe( 'viewFigureToModel', () => {
  41. function expectModel( data ) {
  42. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( data );
  43. }
  44. let dispatcher, schema, imgConverterCalled;
  45. beforeEach( () => {
  46. imgConverterCalled = false;
  47. schema = model.schema;
  48. schema.extend( '$text', { allowIn: 'image' } );
  49. dispatcher = editor.data.viewToModel;
  50. dispatcher.on( 'element:figure', viewFigureToModel() );
  51. dispatcher.on( 'element:img', ( evt, data, consumable, conversionApi ) => {
  52. if ( consumable.consume( data.input, { name: true, attribute: 'src' } ) ) {
  53. const image = conversionApi.writer.createElement( 'image', { src: data.input.getAttribute( 'src' ) } );
  54. conversionApi.writer.insert( image, data.position );
  55. data.output = ModelRange.createOn( image );
  56. imgConverterCalled = true;
  57. }
  58. } );
  59. } );
  60. it( 'should find img element among children and convert it using already defined converters', () => {
  61. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  62. expectModel( '<image src="foo.png"></image>' );
  63. expect( imgConverterCalled ).to.be.true;
  64. } );
  65. it( 'should convert non-img children in image context and append them to model image element', () => {
  66. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'foo' ).toElement( 'foo' );
  67. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'bar' ).toElement( 'bar' );
  68. schema.register( 'foo' );
  69. schema.register( 'bar' );
  70. schema.extend( 'foo', { allowIn: 'image' } );
  71. editor.setData( '<figure class="image">x<img src="foo.png" />y<foo></foo><bar></bar></figure>' );
  72. // Element bar not converted because schema does not allow it.
  73. expectModel( '<image src="foo.png">xy<foo></foo></image>' );
  74. } );
  75. it( 'should be possible to overwrite', () => {
  76. dispatcher.on( 'element:figure', ( evt, data, consumable, conversionApi ) => {
  77. consumable.consume( data.input, { name: true } );
  78. consumable.consume( data.input.getChild( 0 ), { name: true } );
  79. const element = conversionApi.writer.createElement( 'myImage', {
  80. data: {
  81. src: data.input.getChild( 0 ).getAttribute( 'src' )
  82. }
  83. } );
  84. conversionApi.writer.insert( element, data.position );
  85. data.output = ModelRange.createOn( element );
  86. }, { priority: 'high' } );
  87. editor.setData( '<figure class="image"><img src="foo.png" />xyz</figure>' );
  88. expectModel( '<myImage data="{"src":"foo.png"}"></myImage>' );
  89. } );
  90. // Test exactly what figure converter does, which is putting it's children element to image element.
  91. // If this has not been done, it means that figure converter was not used.
  92. it( 'should not convert if figure do not have class="image" attribute', () => {
  93. editor.setData( '<figure><img src="foo.png" />xyz</figure>' );
  94. // Default image converter will be fired.
  95. expectModel( '<image src="foo.png"></image>' );
  96. } );
  97. it( 'should not convert if there is no img element among children', () => {
  98. editor.setData( '<figure class="image">xyz</figure>' );
  99. // Figure converter outputs nothing and text is disallowed in root.
  100. expectModel( '' );
  101. } );
  102. it( 'should not convert if img element was not converted', () => {
  103. // Image element missing src attribute.
  104. editor.setData( '<figure class="image"><img alt="abc" />xyz</figure>' );
  105. // Figure converter outputs nothing and text is disallowed in root.
  106. expectModel( '' );
  107. } );
  108. } );
  109. describe( 'modelToViewAttributeConverter', () => {
  110. it( 'should convert adding attribute to image', () => {
  111. setModelData( model, '<image src=""></image>' );
  112. const image = document.getRoot().getChild( 0 );
  113. model.change( writer => {
  114. writer.setAttribute( 'alt', 'foo bar', image );
  115. } );
  116. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  117. '<figure class="ck-widget image" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  118. );
  119. } );
  120. it( 'should convert removing attribute from image', () => {
  121. setModelData( model, '<image src="" alt="foo bar"></image>' );
  122. const image = document.getRoot().getChild( 0 );
  123. model.change( writer => {
  124. writer.removeAttribute( 'alt', image );
  125. } );
  126. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  127. '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
  128. );
  129. } );
  130. it( 'should convert change of attribute image', () => {
  131. setModelData( model, '<image src="" alt="foo bar"></image>' );
  132. const image = document.getRoot().getChild( 0 );
  133. model.change( writer => {
  134. writer.setAttribute( 'alt', 'baz quix', image );
  135. } );
  136. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  137. '<figure class="ck-widget image" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
  138. );
  139. } );
  140. it( 'should not set attribute if change was already consumed', () => {
  141. editor.editing.modelToView.on( 'attribute:alt:image', ( evt, data, consumable ) => {
  142. consumable.consume( data.item, 'attribute:alt' );
  143. }, { priority: 'high' } );
  144. setModelData( model, '<image src="" alt="foo bar"></image>' );
  145. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  146. '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
  147. );
  148. } );
  149. } );
  150. } );