imagestyleengine.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  50. '<figure class="image ck-widget side-class" contenteditable="false">' +
  51. '<img src="foo.png"></img>' +
  52. '</figure>' );
  53. } );
  54. it( 'should not convert from view to model if class is not defined', () => {
  55. editor.setData( '<figure class="image foo-bar"><img src="foo.png" /></figure>' );
  56. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  57. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  58. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  59. );
  60. } );
  61. it( 'should not convert from view to model when not in image figure', () => {
  62. editor.setData( '<figure class="side-class"></figure>' );
  63. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' );
  64. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '' );
  65. } );
  66. it( 'should not convert from view to model if schema prevents it', () => {
  67. document.schema.disallow( { name: 'image', attributes: 'imageStyle' } );
  68. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  69. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  70. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  71. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  72. );
  73. } );
  74. it( 'should convert model to view: adding attribute', () => {
  75. setModelData( document, '<image src="foo.png"></image>' );
  76. const image = document.getRoot().getChild( 0 );
  77. const batch = document.batch();
  78. document.enqueueChanges( () => {
  79. batch.setAttribute( image, 'imageStyle', 'side' );
  80. } );
  81. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  82. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  83. '<figure class="image ck-widget side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  84. );
  85. } );
  86. it( 'should convert model to view: removing attribute', () => {
  87. setModelData( document, '<image src="foo.png" imageStyle="side"></image>' );
  88. const image = document.getRoot().getChild( 0 );
  89. const batch = document.batch();
  90. document.enqueueChanges( () => {
  91. batch.setAttribute( image, 'imageStyle', null );
  92. } );
  93. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  94. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  95. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  96. );
  97. } );
  98. it( 'should convert model to view: change attribute', () => {
  99. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  100. const image = document.getRoot().getChild( 0 );
  101. const batch = document.batch();
  102. document.enqueueChanges( () => {
  103. batch.setAttribute( image, 'imageStyle', 'side' );
  104. } );
  105. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  106. // https://github.com/ckeditor/ckeditor5-image/issues/132
  107. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  108. '<figure class="image ck-widget side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  109. );
  110. document.enqueueChanges( () => {
  111. batch.setAttribute( image, 'imageStyle', 'dummy' );
  112. } );
  113. expect( editor.getData() ).to.equal( '<figure class="image dummy-class"><img src="foo.png"></figure>' );
  114. // https://github.com/ckeditor/ckeditor5-image/issues/132
  115. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  116. '<figure class="image ck-widget dummy-class" contenteditable="false"><img src="foo.png"></img></figure>'
  117. );
  118. } );
  119. it( 'should not convert from model to view if already consumed: adding attribute', () => {
  120. editor.editing.modelToView.on( 'addAttribute:imageStyle', ( evt, data, consumable ) => {
  121. consumable.consume( data.item, 'addAttribute:imageStyle' );
  122. }, { priority: 'high' } );
  123. setModelData( document, '<image src="foo.png"></image>' );
  124. const image = document.getRoot().getChild( 0 );
  125. const batch = document.batch();
  126. document.enqueueChanges( () => {
  127. batch.setAttribute( image, 'imageStyle', 'side' );
  128. } );
  129. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  130. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  131. );
  132. } );
  133. it( 'should not convert from model to view if already consumed: removing attribute', () => {
  134. editor.editing.modelToView.on( 'removeAttribute:imageStyle', ( evt, data, consumable ) => {
  135. consumable.consume( data.item, 'removeAttribute:imageStyle' );
  136. }, { priority: 'high' } );
  137. setModelData( document, '<image src="foo.png" imageStyle="side"></image>' );
  138. const image = document.getRoot().getChild( 0 );
  139. const batch = document.batch();
  140. document.enqueueChanges( () => {
  141. batch.setAttribute( image, 'imageStyle', null );
  142. } );
  143. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  144. '<figure class="image ck-widget side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  145. );
  146. } );
  147. it( 'should not convert from model to view if already consumed: change attribute', () => {
  148. editor.editing.modelToView.on( 'changeAttribute:imageStyle', ( evt, data, consumable ) => {
  149. consumable.consume( data.item, 'changeAttribute:imageStyle' );
  150. }, { priority: 'high' } );
  151. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  152. const image = document.getRoot().getChild( 0 );
  153. const batch = document.batch();
  154. document.enqueueChanges( () => {
  155. batch.setAttribute( image, 'imageStyle', 'side' );
  156. } );
  157. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  158. '<figure class="image ck-widget dummy-class" contenteditable="false"><img src="foo.png"></img></figure>'
  159. );
  160. } );
  161. it( 'should not convert from model to view if style is not present: adding attribute', () => {
  162. setModelData( document, '<image src="foo.png"></image>' );
  163. const image = document.getRoot().getChild( 0 );
  164. const batch = document.batch();
  165. document.enqueueChanges( () => {
  166. batch.setAttribute( image, 'imageStyle', 'foo' );
  167. } );
  168. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  169. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  170. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  171. );
  172. } );
  173. it( 'should not convert from model to view if style is not present: change attribute', () => {
  174. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  175. const image = document.getRoot().getChild( 0 );
  176. const batch = document.batch();
  177. document.enqueueChanges( () => {
  178. batch.setAttribute( image, 'imageStyle', 'foo' );
  179. } );
  180. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  181. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  182. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  183. );
  184. } );
  185. it( 'should not convert from model to view if style is not present: remove attribute', () => {
  186. setModelData( document, '<image src="foo.png" imageStyle="foo"></image>' );
  187. const image = document.getRoot().getChild( 0 );
  188. const batch = document.batch();
  189. document.enqueueChanges( () => {
  190. batch.setAttribute( image, 'imageStyle', null );
  191. } );
  192. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  193. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  194. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  195. );
  196. } );
  197. } );