converters.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { viewToModelImage, createImageAttributeConverter } from '../../src/image/converters';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { createImageViewElement } from '../../src/image/imageengine';
  8. import { toImageWidget } from '../../src/image/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. buildModelConverter().for( editor.editing.modelToView )
  30. .fromElement( 'image' )
  31. .toElement( () => toImageWidget( createImageViewElement() ) );
  32. createImageAttributeConverter( [ editor.editing.modelToView ], 'src' );
  33. createImageAttributeConverter( [ editor.editing.modelToView ], 'alt' );
  34. } );
  35. } );
  36. describe( 'viewToModelImage', () => {
  37. let dispatcher, schema;
  38. beforeEach( () => {
  39. schema = document.schema;
  40. dispatcher = editor.data.viewToModel;
  41. dispatcher.on( 'element:figure', viewToModelImage() );
  42. } );
  43. it( 'should convert view figure element', () => {
  44. editor.setData( '<figure class="image"><img src="foo.png" alt="bar baz"></img></figure>' );
  45. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image alt="bar baz" src="foo.png"></image>' );
  46. } );
  47. it( 'should convert without alt', () => {
  48. editor.setData( '<figure class="image"><img src="foo.png"></img></figure>' );
  49. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  50. } );
  51. it( 'should not convert if figure element is already consumed', () => {
  52. dispatcher.on( 'element:figure', ( evt, data, consumable ) => {
  53. consumable.consume( data.input, { name: true, class: 'image' } );
  54. data.output = new ModelElement( 'not-image' );
  55. }, { priority: 'high' } );
  56. editor.setData( '<figure class="image"><img src="foo.png" alt="bar baz"></img></figure>' );
  57. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<not-image></not-image>' );
  58. } );
  59. it( 'should not convert image if schema disallows it', () => {
  60. schema.disallow( { name: 'image', attributes: [ 'alt', 'src' ], inside: '$root' } );
  61. editor.setData( '<figure class="image"><img src="foo.png"></img></figure>' );
  62. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' );
  63. } );
  64. it( 'should not convert image if there is no img element', () => {
  65. editor.setData( '<figure class="image"></figure>' );
  66. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' );
  67. } );
  68. } );
  69. describe( 'modelToViewAttributeConverter', () => {
  70. it( 'should convert adding attribute to image', () => {
  71. setModelData( document, '<image src=""></image>' );
  72. const image = document.getRoot().getChild( 0 );
  73. document.enqueueChanges( () => {
  74. const batch = document.batch();
  75. batch.setAttribute( image, 'alt', 'foo bar' );
  76. } );
  77. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  78. '<figure class="image ck-widget" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  79. );
  80. } );
  81. it( 'should convert removing attribute from image', () => {
  82. setModelData( document, '<image src="" alt="foo bar"></image>' );
  83. const image = document.getRoot().getChild( 0 );
  84. document.enqueueChanges( () => {
  85. const batch = document.batch();
  86. batch.removeAttribute( image, 'alt' );
  87. } );
  88. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  89. '<figure class="image ck-widget" contenteditable="false"><img src=""></img></figure>'
  90. );
  91. } );
  92. it( 'should convert change of attribute image', () => {
  93. setModelData( document, '<image src="" alt="foo bar"></image>' );
  94. const image = document.getRoot().getChild( 0 );
  95. document.enqueueChanges( () => {
  96. const batch = document.batch();
  97. batch.setAttribute( image, 'alt', 'baz quix' );
  98. } );
  99. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  100. '<figure class="image ck-widget" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
  101. );
  102. } );
  103. it( 'should not change attribute if change is already consumed', () => {
  104. editor.editing.modelToView.on( `changeAttribute:alt:image`, ( evt, data, consumable ) => {
  105. consumable.consume( data.item, 'changeAttribute:alt' );
  106. }, { priority: 'high' } );
  107. setModelData( document, '<image src="" alt="foo bar"></image>' );
  108. const image = document.getRoot().getChild( 0 );
  109. document.enqueueChanges( () => {
  110. const batch = document.batch();
  111. batch.setAttribute( image, 'alt', 'baz quix' );
  112. } );
  113. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  114. '<figure class="image ck-widget" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  115. );
  116. } );
  117. } );
  118. } );