converters.js 5.7 KB

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