imagestyleengine.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from 'ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import ImageStyleEngine from 'ckeditor5-image/src/imagestyle/imagestyleengine';
  7. import ImageEngine from 'ckeditor5-image/src/imageengine';
  8. import ImageStyleCommand from 'ckeditor5-image/src/imagestyle/imagestylecommand';
  9. import { getData as getModelData, setData as setModelData } from 'ckeditor5-engine/src/dev-utils/model';
  10. import { getData as getViewData } from '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.has( 'fullStyle' ) ).to.be.true;
  42. expect( editor.commands.has( 'sideStyle' ) ).to.be.true;
  43. expect( editor.commands.has( 'dummyStyle' ) ).to.be.true;
  44. expect( editor.commands.get( 'fullStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  45. expect( editor.commands.get( 'sideStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  46. expect( editor.commands.get( 'dummyStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  47. } );
  48. it( 'should convert from view to model', () => {
  49. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  50. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image imageStyle="side" src="foo.png"></image>' );
  51. } );
  52. it( 'should not convert from view to model if class is not defined', () => {
  53. editor.setData( '<figure class="image foo-bar"><img src="foo.png" /></figure>' );
  54. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  55. } );
  56. it( 'should not convert from view to model when not in image figure', () => {
  57. editor.setData( '<figure class="side-class"></figure>' );
  58. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' );
  59. } );
  60. it( 'should not convert from view to model if schema prevents it', () => {
  61. document.schema.disallow( { name: 'image', attributes: 'imageStyle' } );
  62. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  63. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  64. } );
  65. it( 'should convert model to view: adding attribute', () => {
  66. setModelData( document, '<image src="foo.png"></image>' );
  67. const image = document.getRoot().getChild( 0 );
  68. const batch = document.batch();
  69. document.enqueueChanges( () => {
  70. batch.setAttribute( image, 'imageStyle', 'side' );
  71. } );
  72. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  73. } );
  74. it( 'should convert model to view: removing attribute', () => {
  75. setModelData( document, '<image src="foo.png" imageStyle="side"></image>' );
  76. const image = document.getRoot().getChild( 0 );
  77. const batch = document.batch();
  78. document.enqueueChanges( () => {
  79. batch.setAttribute( image, 'imageStyle', null );
  80. } );
  81. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  82. } );
  83. it( 'should convert model to view: change attribute', () => {
  84. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  85. const image = document.getRoot().getChild( 0 );
  86. const batch = document.batch();
  87. document.enqueueChanges( () => {
  88. batch.setAttribute( image, 'imageStyle', 'side' );
  89. } );
  90. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  91. } );
  92. it( 'should not convert from model to view if already consumed: adding attribute', () => {
  93. editor.editing.modelToView.on( 'addAttribute:imageStyle', ( evt, data, consumable ) => {
  94. consumable.consume( data.item, 'addAttribute:imageStyle' );
  95. }, { priority: 'high' } );
  96. setModelData( document, '<image src="foo.png"></image>' );
  97. const image = document.getRoot().getChild( 0 );
  98. const batch = document.batch();
  99. document.enqueueChanges( () => {
  100. batch.setAttribute( image, 'imageStyle', 'side' );
  101. } );
  102. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  103. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  104. );
  105. } );
  106. it( 'should not convert from model to view if already consumed: removing attribute', () => {
  107. editor.editing.modelToView.on( 'removeAttribute:imageStyle', ( evt, data, consumable ) => {
  108. consumable.consume( data.item, 'removeAttribute:imageStyle' );
  109. }, { priority: 'high' } );
  110. setModelData( document, '<image src="foo.png" imageStyle="side"></image>' );
  111. const image = document.getRoot().getChild( 0 );
  112. const batch = document.batch();
  113. document.enqueueChanges( () => {
  114. batch.setAttribute( image, 'imageStyle', null );
  115. } );
  116. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  117. '<figure class="image ck-widget side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  118. );
  119. } );
  120. it( 'should not convert from model to view if already consumed: change attribute', () => {
  121. editor.editing.modelToView.on( 'changeAttribute:imageStyle', ( evt, data, consumable ) => {
  122. consumable.consume( data.item, 'changeAttribute:imageStyle' );
  123. }, { priority: 'high' } );
  124. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  125. const image = document.getRoot().getChild( 0 );
  126. const batch = document.batch();
  127. document.enqueueChanges( () => {
  128. batch.setAttribute( image, 'imageStyle', 'side' );
  129. } );
  130. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  131. '<figure class="image ck-widget dummy-class" contenteditable="false"><img src="foo.png"></img></figure>'
  132. );
  133. } );
  134. it( 'should not convert from model to view if style is not present: adding attribute', () => {
  135. setModelData( document, '<image src="foo.png"></image>' );
  136. const image = document.getRoot().getChild( 0 );
  137. const batch = document.batch();
  138. document.enqueueChanges( () => {
  139. batch.setAttribute( image, 'imageStyle', 'foo' );
  140. } );
  141. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  142. } );
  143. it( 'should not convert from model to view if style is not present: change attribute', () => {
  144. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  145. const image = document.getRoot().getChild( 0 );
  146. const batch = document.batch();
  147. document.enqueueChanges( () => {
  148. batch.setAttribute( image, 'imageStyle', 'foo' );
  149. } );
  150. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  151. } );
  152. it( 'should not convert from model to view if style is not present: remove attribute', () => {
  153. setModelData( document, '<image src="foo.png" imageStyle="foo"></image>' );
  154. const image = document.getRoot().getChild( 0 );
  155. const batch = document.batch();
  156. document.enqueueChanges( () => {
  157. batch.setAttribute( image, 'imageStyle', null );
  158. } );
  159. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  160. } );
  161. } );