imagestyleengine.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import ImageStyleEngine from '../../src/imagestyle/imagestyleengine';
  7. import ImageEngine from '../../src/image/imageengine';
  8. import ImageStyleCommand from '../../src/imagestyle/imagestylecommand';
  9. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  11. describe( 'ImageStyleEngine', () => {
  12. let editor, document, viewDocument;
  13. beforeEach( () => {
  14. return VirtualTestEditor.create( {
  15. plugins: [ ImageStyleEngine ],
  16. image: {
  17. styles: [
  18. { name: 'fullStyle', title: 'foo', icon: 'object-center', value: null },
  19. { name: 'sideStyle', title: 'bar', icon: 'object-right', value: 'side', className: 'side-class' },
  20. { name: 'dummyStyle', title: 'baz', icon: 'object-dummy', value: 'dummy', className: 'dummy-class' }
  21. ]
  22. }
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. document = editor.document;
  27. viewDocument = editor.editing.view;
  28. } );
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( ImageStyleEngine ) ).to.be.instanceOf( ImageStyleEngine );
  32. } );
  33. it( 'should load image engine', () => {
  34. expect( editor.plugins.get( ImageEngine ) ).to.be.instanceOf( ImageEngine );
  35. } );
  36. it( 'should set schema rules for image style', () => {
  37. const schema = document.schema;
  38. expect( schema.check( { name: 'image', attributes: [ 'imageStyle', 'src' ], inside: '$root' } ) ).to.be.true;
  39. } );
  40. it( 'should register separate command for each style', () => {
  41. expect( editor.commands.get( 'fullStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  42. expect( editor.commands.get( 'sideStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  43. expect( editor.commands.get( 'dummyStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  44. } );
  45. it( 'should convert from view to model', () => {
  46. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  47. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image imageStyle="side" src="foo.png"></image>' );
  48. } );
  49. it( 'should not convert from view to model if class is not defined', () => {
  50. editor.setData( '<figure class="image foo-bar"><img src="foo.png" /></figure>' );
  51. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  52. } );
  53. it( 'should not convert from view to model when not in image figure', () => {
  54. editor.setData( '<figure class="side-class"></figure>' );
  55. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' );
  56. } );
  57. it( 'should not convert from view to model if schema prevents it', () => {
  58. document.schema.disallow( { name: 'image', attributes: 'imageStyle' } );
  59. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  60. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  61. } );
  62. it( 'should convert model to view: adding attribute', () => {
  63. setModelData( document, '<image src="foo.png"></image>' );
  64. const image = document.getRoot().getChild( 0 );
  65. const batch = document.batch();
  66. document.enqueueChanges( () => {
  67. batch.setAttribute( image, 'imageStyle', 'side' );
  68. } );
  69. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  70. } );
  71. it( 'should convert model to view: removing attribute', () => {
  72. setModelData( document, '<image src="foo.png" imageStyle="side"></image>' );
  73. const image = document.getRoot().getChild( 0 );
  74. const batch = document.batch();
  75. document.enqueueChanges( () => {
  76. batch.setAttribute( image, 'imageStyle', null );
  77. } );
  78. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  79. } );
  80. it( 'should convert model to view: change attribute', () => {
  81. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  82. const image = document.getRoot().getChild( 0 );
  83. const batch = document.batch();
  84. document.enqueueChanges( () => {
  85. batch.setAttribute( image, 'imageStyle', 'side' );
  86. } );
  87. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  88. } );
  89. it( 'should not convert from model to view if already consumed: adding attribute', () => {
  90. editor.editing.modelToView.on( 'addAttribute:imageStyle', ( evt, data, consumable ) => {
  91. consumable.consume( data.item, 'addAttribute:imageStyle' );
  92. }, { priority: 'high' } );
  93. setModelData( document, '<image src="foo.png"></image>' );
  94. const image = document.getRoot().getChild( 0 );
  95. const batch = document.batch();
  96. document.enqueueChanges( () => {
  97. batch.setAttribute( image, 'imageStyle', 'side' );
  98. } );
  99. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  100. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  101. );
  102. } );
  103. it( 'should not convert from model to view if already consumed: removing attribute', () => {
  104. editor.editing.modelToView.on( 'removeAttribute:imageStyle', ( evt, data, consumable ) => {
  105. consumable.consume( data.item, 'removeAttribute:imageStyle' );
  106. }, { priority: 'high' } );
  107. setModelData( document, '<image src="foo.png" imageStyle="side"></image>' );
  108. const image = document.getRoot().getChild( 0 );
  109. const batch = document.batch();
  110. document.enqueueChanges( () => {
  111. batch.setAttribute( image, 'imageStyle', null );
  112. } );
  113. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  114. '<figure class="image ck-widget side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  115. );
  116. } );
  117. it( 'should not convert from model to view if already consumed: change attribute', () => {
  118. editor.editing.modelToView.on( 'changeAttribute:imageStyle', ( evt, data, consumable ) => {
  119. consumable.consume( data.item, 'changeAttribute:imageStyle' );
  120. }, { priority: 'high' } );
  121. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  122. const image = document.getRoot().getChild( 0 );
  123. const batch = document.batch();
  124. document.enqueueChanges( () => {
  125. batch.setAttribute( image, 'imageStyle', 'side' );
  126. } );
  127. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  128. '<figure class="image ck-widget dummy-class" contenteditable="false"><img src="foo.png"></img></figure>'
  129. );
  130. } );
  131. it( 'should not convert from model to view if style is not present: adding attribute', () => {
  132. setModelData( document, '<image src="foo.png"></image>' );
  133. const image = document.getRoot().getChild( 0 );
  134. const batch = document.batch();
  135. document.enqueueChanges( () => {
  136. batch.setAttribute( image, 'imageStyle', 'foo' );
  137. } );
  138. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  139. } );
  140. it( 'should not convert from model to view if style is not present: change attribute', () => {
  141. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  142. const image = document.getRoot().getChild( 0 );
  143. const batch = document.batch();
  144. document.enqueueChanges( () => {
  145. batch.setAttribute( image, 'imageStyle', 'foo' );
  146. } );
  147. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  148. } );
  149. it( 'should not convert from model to view if style is not present: remove attribute', () => {
  150. setModelData( document, '<image src="foo.png" imageStyle="foo"></image>' );
  151. const image = document.getRoot().getChild( 0 );
  152. const batch = document.batch();
  153. document.enqueueChanges( () => {
  154. batch.setAttribute( image, 'imageStyle', null );
  155. } );
  156. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  157. } );
  158. } );