imagestyleengine.js 7.8 KB

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