converters.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. modelToViewAttributeConverter
  8. } from '../../src/image/converters';
  9. import { toImageWidget } from '../../src/image/utils';
  10. import { createImageViewElement } from '../../src/image/imageediting';
  11. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  12. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  13. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  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. import env from '@ckeditor/ckeditor5-utils/src/env';
  18. describe( 'Image converters', () => {
  19. let editor, model, document, viewDocument;
  20. beforeEach( () => {
  21. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  22. sinon.stub( env, 'isEdge' ).get( () => false );
  23. return VirtualTestEditor.create()
  24. .then( newEditor => {
  25. editor = newEditor;
  26. model = editor.model;
  27. document = model.document;
  28. viewDocument = editor.editing.view;
  29. const schema = model.schema;
  30. schema.register( 'image', {
  31. allowWhere: '$block',
  32. allowAttributes: [ 'alt', 'src' ],
  33. isObject: true,
  34. isBlock: true
  35. } );
  36. const editingElementCreator = ( modelElement, viewWriter ) =>
  37. toImageWidget( createImageViewElement( viewWriter ), viewWriter, '' );
  38. editor.conversion.for( 'editingDowncast' ).add( downcastElementToElement( {
  39. model: 'image',
  40. view: editingElementCreator
  41. } ) );
  42. editor.conversion.for( 'downcast' )
  43. .add( modelToViewAttributeConverter( 'src' ) )
  44. .add( modelToViewAttributeConverter( 'alt' ) );
  45. } );
  46. } );
  47. describe( 'viewFigureToModel', () => {
  48. function expectModel( data ) {
  49. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( data );
  50. }
  51. let schema, imgConverterCalled;
  52. beforeEach( () => {
  53. // Since this part of test tests only view->model conversion editing pipeline is not necessary
  54. // so defining model->view converters won't be necessary.
  55. editor.editing.destroy();
  56. schema = model.schema;
  57. schema.extend( '$text', { allowIn: 'image' } );
  58. editor.conversion.for( 'upcast' )
  59. .add( viewFigureToModel() )
  60. .add( upcastElementToElement( {
  61. view: {
  62. name: 'img',
  63. attributes: {
  64. src: true
  65. }
  66. },
  67. model: ( viewImage, writer ) => {
  68. imgConverterCalled = true;
  69. return writer.createElement( 'image', { src: viewImage.getAttribute( 'src' ) } );
  70. }
  71. } ) );
  72. } );
  73. it( 'should find img element among children and convert it using already defined converters', () => {
  74. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  75. expectModel( '<image src="foo.png"></image>' );
  76. expect( imgConverterCalled ).to.be.true;
  77. } );
  78. it( 'should convert children allowed by schema and omit disallowed', () => {
  79. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { view: 'foo', model: 'foo' } ) );
  80. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { view: 'bar', model: 'bar' } ) );
  81. schema.register( 'foo', { allowIn: 'image' } );
  82. // Is allowed in root, but should not try to split image element.
  83. schema.register( 'bar', { allowIn: '$root' } );
  84. editor.setData( '<figure class="image">x<img src="foo.png" />y<foo></foo><bar></bar></figure>' );
  85. // Element bar not converted because schema does not allow it.
  86. expectModel( '<image src="foo.png">xy<foo></foo></image>' );
  87. } );
  88. it( 'should split parent element when image is not allowed - in the middle', () => {
  89. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { view: 'div', model: 'div' } ) );
  90. schema.register( 'div', { inheritAllFrom: '$block' } );
  91. schema.extend( 'image', { disallowIn: 'div' } );
  92. editor.setData(
  93. '<div>' +
  94. 'abc' +
  95. '<figure class="image">' +
  96. '<img src="foo.jpg"/>' +
  97. '</figure>' +
  98. 'def' +
  99. '</div>'
  100. );
  101. expectModel( '<div>abc</div><image src="foo.jpg"></image><div>def</div>' );
  102. } );
  103. it( 'should split parent element when image is not allowed - at the end', () => {
  104. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  105. schema.register( 'div', { inheritAllFrom: '$block' } );
  106. schema.extend( 'image', { disallowIn: 'div' } );
  107. editor.setData(
  108. '<div>' +
  109. 'abc' +
  110. '<figure class="image">' +
  111. '<img src="foo.jpg"/>' +
  112. '</figure>' +
  113. '</div>'
  114. );
  115. expectModel( '<div>abc</div><image src="foo.jpg"></image>' );
  116. } );
  117. it( 'should split parent element when image is not allowed - at the beginning', () => {
  118. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  119. schema.register( 'div', { inheritAllFrom: '$block' } );
  120. schema.extend( 'image', { disallowIn: 'div' } );
  121. editor.setData(
  122. '<div>' +
  123. '<figure class="image">' +
  124. '<img src="foo.jpg"/>' +
  125. '</figure>' +
  126. 'def' +
  127. '</div>'
  128. );
  129. expectModel( '<image src="foo.jpg"></image><div>def</div>' );
  130. } );
  131. it( 'should be possible to overwrite', () => {
  132. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  133. conversionApi.consumable.consume( data.viewItem, { name: true } );
  134. conversionApi.consumable.consume( data.viewItem.getChild( 0 ), { name: true } );
  135. const element = conversionApi.writer.createElement( 'myImage', {
  136. data: {
  137. src: data.viewItem.getChild( 0 ).getAttribute( 'src' )
  138. }
  139. } );
  140. conversionApi.writer.insert( element, data.modelCursor );
  141. data.modelRange = ModelRange.createOn( element );
  142. data.modelCursor = data.modelRange.end;
  143. }, { priority: 'high' } );
  144. editor.setData( '<figure class="image"><img src="foo.png" />xyz</figure>' );
  145. expectModel( '<myImage data="{"src":"foo.png"}"></myImage>' );
  146. } );
  147. // Test exactly what figure converter does, which is putting it's children element to image element.
  148. // If this has not been done, it means that figure converter was not used.
  149. it( 'should not convert if figure do not have class="image" attribute', () => {
  150. editor.setData( '<figure><img src="foo.png" />xyz</figure>' );
  151. // Default image converter will be fired.
  152. expectModel( '<image src="foo.png"></image>' );
  153. } );
  154. it( 'should not convert if there is no img element among children', () => {
  155. editor.setData( '<figure class="image">xyz</figure>' );
  156. // Figure converter outputs nothing and text is disallowed in root.
  157. expectModel( '' );
  158. } );
  159. it( 'should not convert if img element was not converted', () => {
  160. // Image element missing src attribute.
  161. editor.setData( '<figure class="image"><img alt="abc" />xyz</figure>' );
  162. // Figure converter outputs nothing and text is disallowed in root.
  163. expectModel( '' );
  164. } );
  165. } );
  166. describe( 'modelToViewAttributeConverter', () => {
  167. it( 'should convert adding attribute to image', () => {
  168. setModelData( model, '<image src=""></image>' );
  169. const image = document.getRoot().getChild( 0 );
  170. model.change( writer => {
  171. writer.setAttribute( 'alt', 'foo bar', image );
  172. } );
  173. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  174. '<figure class="ck-widget image" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  175. );
  176. } );
  177. it( 'should convert removing attribute from image', () => {
  178. setModelData( model, '<image src="" alt="foo bar"></image>' );
  179. const image = document.getRoot().getChild( 0 );
  180. model.change( writer => {
  181. writer.removeAttribute( 'alt', image );
  182. } );
  183. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  184. '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
  185. );
  186. } );
  187. it( 'should convert change of attribute image', () => {
  188. setModelData( model, '<image src="" alt="foo bar"></image>' );
  189. const image = document.getRoot().getChild( 0 );
  190. model.change( writer => {
  191. writer.setAttribute( 'alt', 'baz quix', image );
  192. } );
  193. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  194. '<figure class="ck-widget image" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
  195. );
  196. } );
  197. it( 'should not set attribute if change was already consumed', () => {
  198. editor.editing.downcastDispatcher.on( 'attribute:alt:image', ( evt, data, conversionApi ) => {
  199. conversionApi.consumable.consume( data.item, 'attribute:alt' );
  200. }, { priority: 'high' } );
  201. setModelData( model, '<image src="" alt="foo bar"></image>' );
  202. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  203. '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
  204. );
  205. } );
  206. } );
  207. } );