converters.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, conversionApi ) => {
  52. if ( conversionApi.consumable.consume( data.viewItem, { name: true, attribute: 'src' } ) ) {
  53. const image = conversionApi.writer.createElement( 'image', { src: data.viewItem.getAttribute( 'src' ) } );
  54. conversionApi.writer.insert( image, data.modelCursor );
  55. data.modelRange = ModelRange.createOn( image );
  56. data.modelCursor = data.modelRange.end;
  57. imgConverterCalled = true;
  58. }
  59. } );
  60. } );
  61. it( 'should find img element among children and convert it using already defined converters', () => {
  62. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  63. expectModel( '<image src="foo.png"></image>' );
  64. expect( imgConverterCalled ).to.be.true;
  65. } );
  66. it( 'should convert children allowed by schema and omit disallowed', () => {
  67. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'foo' ).toElement( 'foo' );
  68. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'bar' ).toElement( 'bar' );
  69. schema.register( 'foo' );
  70. schema.register( 'bar' );
  71. schema.extend( 'foo', { allowIn: 'image' } );
  72. editor.setData( '<figure class="image">x<img src="foo.png" />y<foo></foo><bar></bar></figure>' );
  73. // Element bar not converted because schema does not allow it.
  74. expectModel( '<image src="foo.png">xy<foo></foo></image>' );
  75. } );
  76. it( 'should be possible to overwrite', () => {
  77. dispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  78. conversionApi.consumable.consume( data.viewItem, { name: true } );
  79. conversionApi.consumable.consume( data.viewItem.getChild( 0 ), { name: true } );
  80. const element = conversionApi.writer.createElement( 'myImage', {
  81. data: {
  82. src: data.viewItem.getChild( 0 ).getAttribute( 'src' )
  83. }
  84. } );
  85. conversionApi.writer.insert( element, data.modelCursor );
  86. data.modelRange = ModelRange.createOn( element );
  87. data.modelCursor = data.modelRange.end;
  88. }, { priority: 'high' } );
  89. editor.setData( '<figure class="image"><img src="foo.png" />xyz</figure>' );
  90. expectModel( '<myImage data="{"src":"foo.png"}"></myImage>' );
  91. } );
  92. // Test exactly what figure converter does, which is putting it's children element to image element.
  93. // If this has not been done, it means that figure converter was not used.
  94. it( 'should not convert if figure do not have class="image" attribute', () => {
  95. editor.setData( '<figure><img src="foo.png" />xyz</figure>' );
  96. // Default image converter will be fired.
  97. expectModel( '<image src="foo.png"></image>' );
  98. } );
  99. it( 'should not convert if there is no img element among children', () => {
  100. editor.setData( '<figure class="image">xyz</figure>' );
  101. // Figure converter outputs nothing and text is disallowed in root.
  102. expectModel( '' );
  103. } );
  104. it( 'should not convert if img element was not converted', () => {
  105. // Image element missing src attribute.
  106. editor.setData( '<figure class="image"><img alt="abc" />xyz</figure>' );
  107. // Figure converter outputs nothing and text is disallowed in root.
  108. expectModel( '' );
  109. } );
  110. } );
  111. describe( 'modelToViewAttributeConverter', () => {
  112. it( 'should convert adding attribute to image', () => {
  113. setModelData( model, '<image src=""></image>' );
  114. const image = document.getRoot().getChild( 0 );
  115. model.change( writer => {
  116. writer.setAttribute( 'alt', 'foo bar', image );
  117. } );
  118. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  119. '<figure class="ck-widget image" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  120. );
  121. } );
  122. it( 'should convert removing attribute from image', () => {
  123. setModelData( model, '<image src="" alt="foo bar"></image>' );
  124. const image = document.getRoot().getChild( 0 );
  125. model.change( writer => {
  126. writer.removeAttribute( 'alt', image );
  127. } );
  128. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  129. '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
  130. );
  131. } );
  132. it( 'should convert change of attribute image', () => {
  133. setModelData( model, '<image src="" alt="foo bar"></image>' );
  134. const image = document.getRoot().getChild( 0 );
  135. model.change( writer => {
  136. writer.setAttribute( 'alt', 'baz quix', image );
  137. } );
  138. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  139. '<figure class="ck-widget image" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
  140. );
  141. } );
  142. it( 'should not set attribute if change was already consumed', () => {
  143. editor.editing.modelToView.on( 'attribute:alt:image', ( evt, data, consumable ) => {
  144. consumable.consume( data.item, 'attribute:alt' );
  145. }, { priority: 'high' } );
  146. setModelData( model, '<image src="" alt="foo bar"></image>' );
  147. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  148. '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
  149. );
  150. } );
  151. } );
  152. } );