imagestyleengine.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // https://github.com/ckeditor/ckeditor5-image/issues/132
  90. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  91. '<figure class="image ck-widget side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  92. );
  93. document.enqueueChanges( () => {
  94. batch.setAttribute( image, 'imageStyle', 'dummy' );
  95. } );
  96. expect( editor.getData() ).to.equal( '<figure class="image dummy-class"><img src="foo.png"></figure>' );
  97. // https://github.com/ckeditor/ckeditor5-image/issues/132
  98. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  99. '<figure class="image ck-widget dummy-class" contenteditable="false"><img src="foo.png"></img></figure>'
  100. );
  101. } );
  102. it( 'should not convert from model to view if already consumed: adding attribute', () => {
  103. editor.editing.modelToView.on( 'addAttribute:imageStyle', ( evt, data, consumable ) => {
  104. consumable.consume( data.item, 'addAttribute:imageStyle' );
  105. }, { priority: 'high' } );
  106. setModelData( document, '<image src="foo.png"></image>' );
  107. const image = document.getRoot().getChild( 0 );
  108. const batch = document.batch();
  109. document.enqueueChanges( () => {
  110. batch.setAttribute( image, 'imageStyle', 'side' );
  111. } );
  112. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  113. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  114. );
  115. } );
  116. it( 'should not convert from model to view if already consumed: removing attribute', () => {
  117. editor.editing.modelToView.on( 'removeAttribute:imageStyle', ( evt, data, consumable ) => {
  118. consumable.consume( data.item, 'removeAttribute:imageStyle' );
  119. }, { priority: 'high' } );
  120. setModelData( document, '<image src="foo.png" imageStyle="side"></image>' );
  121. const image = document.getRoot().getChild( 0 );
  122. const batch = document.batch();
  123. document.enqueueChanges( () => {
  124. batch.setAttribute( image, 'imageStyle', null );
  125. } );
  126. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  127. '<figure class="image ck-widget side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  128. );
  129. } );
  130. it( 'should not convert from model to view if already consumed: change attribute', () => {
  131. editor.editing.modelToView.on( 'changeAttribute:imageStyle', ( evt, data, consumable ) => {
  132. consumable.consume( data.item, 'changeAttribute:imageStyle' );
  133. }, { priority: 'high' } );
  134. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  135. const image = document.getRoot().getChild( 0 );
  136. const batch = document.batch();
  137. document.enqueueChanges( () => {
  138. batch.setAttribute( image, 'imageStyle', 'side' );
  139. } );
  140. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  141. '<figure class="image ck-widget dummy-class" contenteditable="false"><img src="foo.png"></img></figure>'
  142. );
  143. } );
  144. it( 'should not convert from model to view if style is not present: adding attribute', () => {
  145. setModelData( document, '<image src="foo.png"></image>' );
  146. const image = document.getRoot().getChild( 0 );
  147. const batch = document.batch();
  148. document.enqueueChanges( () => {
  149. batch.setAttribute( image, 'imageStyle', 'foo' );
  150. } );
  151. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  152. } );
  153. it( 'should not convert from model to view if style is not present: change attribute', () => {
  154. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  155. const image = document.getRoot().getChild( 0 );
  156. const batch = document.batch();
  157. document.enqueueChanges( () => {
  158. batch.setAttribute( image, 'imageStyle', 'foo' );
  159. } );
  160. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  161. } );
  162. it( 'should not convert from model to view if style is not present: remove attribute', () => {
  163. setModelData( document, '<image src="foo.png" imageStyle="foo"></image>' );
  164. const image = document.getRoot().getChild( 0 );
  165. const batch = document.batch();
  166. document.enqueueChanges( () => {
  167. batch.setAttribute( image, 'imageStyle', null );
  168. } );
  169. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  170. } );
  171. } );