8
0

converters.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { viewToModelImage, modelToViewSelection, createImageAttributeConverter } from '../src/converters';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { createImageViewElement } from '../src/imageengine';
  8. import { toImageWidget } from '../src/utils';
  9. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  10. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  11. import { parse as parseView, getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  12. import { stringify as stringifyModel, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. describe( 'Image converters', () => {
  14. let editor, document, viewDocument;
  15. beforeEach( () => {
  16. return VirtualTestEditor.create()
  17. .then( newEditor => {
  18. editor = newEditor;
  19. document = editor.document;
  20. viewDocument = editor.editing.view;
  21. const schema = document.schema;
  22. schema.registerItem( 'image' );
  23. schema.requireAttributes( 'image', [ 'src' ] );
  24. schema.allow( { name: 'image', attributes: [ 'alt', 'src' ], inside: '$root' } );
  25. schema.objects.add( 'image' );
  26. buildModelConverter().for( )
  27. .fromElement( 'image' )
  28. .toElement( () => toImageWidget( createImageViewElement() ) );
  29. editor.editing.modelToView.on( 'selection', modelToViewSelection( editor.t ), { priority: 'lowest' } );
  30. buildModelConverter().for( editor.editing.modelToView )
  31. .fromElement( 'image' )
  32. .toElement( () => toImageWidget( createImageViewElement() ) );
  33. createImageAttributeConverter( [ editor.editing.modelToView ], 'src' );
  34. createImageAttributeConverter( [ editor.editing.modelToView ], 'alt' );
  35. } );
  36. } );
  37. describe( 'viewToModelImage', () => {
  38. let dispatcher, schema;
  39. beforeEach( () => {
  40. schema = document.schema;
  41. dispatcher = editor.data.viewToModel;
  42. dispatcher.on( 'element:figure', viewToModelImage() );
  43. } );
  44. it( 'should convert view figure element', () => {
  45. test(
  46. '<figure class="image"><img src="foo.png" alt="bar baz"></img></figure>',
  47. '<image alt="bar baz" src="foo.png"></image>'
  48. );
  49. } );
  50. it( 'should convert without alt', () => {
  51. test(
  52. '<figure class="image"><img src="foo.png"></img></figure>',
  53. '<image src="foo.png"></image>'
  54. );
  55. } );
  56. it( 'should not convert if figure element is already consumed', () => {
  57. dispatcher.on( 'element:figure', ( evt, data, consumable ) => {
  58. consumable.consume( data.input, { name: true, class: 'image' } );
  59. data.output = new ModelElement( 'not-image' );
  60. }, { priority: 'high' } );
  61. test(
  62. '<figure class="image"><img src="foo.png" alt="bar baz"></img></figure>',
  63. '<not-image></not-image>'
  64. );
  65. } );
  66. it( 'should not convert image if schema disallows it', () => {
  67. schema.disallow( { name: 'image', attributes: [ 'alt', 'src' ], inside: '$root' } );
  68. const element = parseView( '<figure class="image"><img src="foo.png"></img></figure>' );
  69. const model = dispatcher.convert( element );
  70. expect( stringifyModel( model ) ).to.equal( '' );
  71. } );
  72. it( 'should not convert image if there is no img element', () => {
  73. const element = parseView( '<figure class="image"></figure>' );
  74. const model = dispatcher.convert( element );
  75. expect( stringifyModel( model ) ).to.equal( '' );
  76. } );
  77. function test( viewString, modelString ) {
  78. const element = parseView( viewString );
  79. const model = dispatcher.convert( element );
  80. expect( stringifyModel( model ) ).to.equal( modelString );
  81. }
  82. } );
  83. describe( 'modelToViewSelection', () => {
  84. it( 'should convert selection over image to fake selection', () => {
  85. setModelData( document, '[<image src=""></image>]' );
  86. expect( viewDocument.selection.isFake ).to.be.true;
  87. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  88. } );
  89. it( 'should convert fake selection label with alt', () => {
  90. setModelData( document, '[<image src="" alt="foo bar"></image>]' );
  91. expect( viewDocument.selection.isFake ).to.be.true;
  92. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'foo bar image widget' );
  93. } );
  94. it( 'should not convert fake selection if image is not selected', () => {
  95. setModelData( document, '[]<image src="" alt="foo bar"></image>' );
  96. expect( viewDocument.selection.isFake ).to.be.false;
  97. } );
  98. } );
  99. describe( 'modelToViewAttributeConverter', () => {
  100. it( 'should convert adding attribute to image', () => {
  101. setModelData( document, '<image src=""></image>' );
  102. const image = document.getRoot().getChild( 0 );
  103. document.enqueueChanges( () => {
  104. const batch = document.batch();
  105. batch.setAttribute( image, 'alt', 'foo bar' );
  106. } );
  107. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  108. '<figure class="image ck-widget" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  109. );
  110. } );
  111. it( 'should convert removing attribute from image', () => {
  112. setModelData( document, '<image src="" alt="foo bar"></image>' );
  113. const image = document.getRoot().getChild( 0 );
  114. document.enqueueChanges( () => {
  115. const batch = document.batch();
  116. batch.removeAttribute( image, 'alt' );
  117. } );
  118. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  119. '<figure class="image ck-widget" contenteditable="false"><img src=""></img></figure>'
  120. );
  121. } );
  122. it( 'should convert change of attribute image', () => {
  123. setModelData( document, '<image src="" alt="foo bar"></image>' );
  124. const image = document.getRoot().getChild( 0 );
  125. document.enqueueChanges( () => {
  126. const batch = document.batch();
  127. batch.setAttribute( image, 'alt', 'baz quix' );
  128. } );
  129. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  130. '<figure class="image ck-widget" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
  131. );
  132. } );
  133. it( 'should not change attribute if change is already consumed', () => {
  134. editor.editing.modelToView.on( `changeAttribute:alt:image`, ( evt, data, consumable ) => {
  135. consumable.consume( data.item, 'changeAttribute:alt' );
  136. }, { priority: 'high' } );
  137. setModelData( document, '<image src="" alt="foo bar"></image>' );
  138. const image = document.getRoot().getChild( 0 );
  139. document.enqueueChanges( () => {
  140. const batch = document.batch();
  141. batch.setAttribute( image, 'alt', 'baz quix' );
  142. } );
  143. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  144. '<figure class="image ck-widget" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  145. );
  146. } );
  147. } );
  148. } );