8
0

converters.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  12. import { setData as setModelData, getData as getModelData } 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. editor.setData( '<figure class="image"><img src="foo.png" alt="bar baz"></img></figure>' );
  46. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image alt="bar baz" src="foo.png"></image>' );
  47. } );
  48. it( 'should convert without alt', () => {
  49. editor.setData( '<figure class="image"><img src="foo.png"></img></figure>' );
  50. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  51. } );
  52. it( 'should not convert if figure element is already consumed', () => {
  53. dispatcher.on( 'element:figure', ( evt, data, consumable ) => {
  54. consumable.consume( data.input, { name: true, class: 'image' } );
  55. data.output = new ModelElement( 'not-image' );
  56. }, { priority: 'high' } );
  57. editor.setData( '<figure class="image"><img src="foo.png" alt="bar baz"></img></figure>' );
  58. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<not-image></not-image>' );
  59. } );
  60. it( 'should not convert image if schema disallows it', () => {
  61. schema.disallow( { name: 'image', attributes: [ 'alt', 'src' ], inside: '$root' } );
  62. editor.setData( '<figure class="image"><img src="foo.png"></img></figure>' );
  63. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' );
  64. } );
  65. it( 'should not convert image if there is no img element', () => {
  66. editor.setData( '<figure class="image"></figure>' );
  67. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' );
  68. } );
  69. } );
  70. describe( 'modelToViewSelection', () => {
  71. it( 'should convert selection over image to fake selection', () => {
  72. setModelData( document, '[<image src=""></image>]' );
  73. expect( viewDocument.selection.isFake ).to.be.true;
  74. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  75. } );
  76. it( 'should convert fake selection label with alt', () => {
  77. setModelData( document, '[<image src="" alt="foo bar"></image>]' );
  78. expect( viewDocument.selection.isFake ).to.be.true;
  79. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'foo bar image widget' );
  80. } );
  81. it( 'should not convert fake selection if image is not selected', () => {
  82. setModelData( document, '[]<image src="" alt="foo bar"></image>' );
  83. expect( viewDocument.selection.isFake ).to.be.false;
  84. } );
  85. } );
  86. describe( 'modelToViewAttributeConverter', () => {
  87. it( 'should convert adding attribute to image', () => {
  88. setModelData( document, '<image src=""></image>' );
  89. const image = document.getRoot().getChild( 0 );
  90. document.enqueueChanges( () => {
  91. const batch = document.batch();
  92. batch.setAttribute( image, 'alt', 'foo bar' );
  93. } );
  94. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  95. '<figure class="image ck-widget" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  96. );
  97. } );
  98. it( 'should convert removing attribute from image', () => {
  99. setModelData( document, '<image src="" alt="foo bar"></image>' );
  100. const image = document.getRoot().getChild( 0 );
  101. document.enqueueChanges( () => {
  102. const batch = document.batch();
  103. batch.removeAttribute( image, 'alt' );
  104. } );
  105. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  106. '<figure class="image ck-widget" contenteditable="false"><img src=""></img></figure>'
  107. );
  108. } );
  109. it( 'should convert change of attribute image', () => {
  110. setModelData( document, '<image src="" alt="foo bar"></image>' );
  111. const image = document.getRoot().getChild( 0 );
  112. document.enqueueChanges( () => {
  113. const batch = document.batch();
  114. batch.setAttribute( image, 'alt', 'baz quix' );
  115. } );
  116. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  117. '<figure class="image ck-widget" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
  118. );
  119. } );
  120. it( 'should not change attribute if change is already consumed', () => {
  121. editor.editing.modelToView.on( `changeAttribute:alt:image`, ( evt, data, consumable ) => {
  122. consumable.consume( data.item, 'changeAttribute:alt' );
  123. }, { priority: 'high' } );
  124. setModelData( document, '<image src="" alt="foo bar"></image>' );
  125. const image = document.getRoot().getChild( 0 );
  126. document.enqueueChanges( () => {
  127. const batch = document.batch();
  128. batch.setAttribute( image, 'alt', 'baz quix' );
  129. } );
  130. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  131. '<figure class="image ck-widget" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  132. );
  133. } );
  134. } );
  135. } );