8
0

imagestyleediting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 ImageStyleEditing from '../../src/imagestyle/imagestyleediting';
  7. import ImageEditing from '../../src/image/imageediting';
  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. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. testUtils.createSinonSandbox();
  13. describe( 'ImageStyleEditing', () => {
  14. let editor, model, document, viewDocument;
  15. afterEach( () => {
  16. editor.destroy();
  17. } );
  18. describe( 'plugin', () => {
  19. beforeEach( () => {
  20. return VirtualTestEditor
  21. .create( {
  22. plugins: [ ImageStyleEditing ],
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. } );
  27. } );
  28. it( 'should be loaded', () => {
  29. expect( editor.plugins.get( ImageStyleEditing ) ).to.be.instanceOf( ImageStyleEditing );
  30. } );
  31. it( 'should load image editing', () => {
  32. expect( editor.plugins.get( ImageEditing ) ).to.be.instanceOf( ImageEditing );
  33. } );
  34. } );
  35. describe( 'init', () => {
  36. beforeEach( () => {
  37. return VirtualTestEditor
  38. .create( {
  39. plugins: [ ImageStyleEditing ],
  40. image: {
  41. styles: [
  42. { name: 'fullStyle', title: 'foo', icon: 'object-center', isDefault: true },
  43. { name: 'sideStyle', title: 'bar', icon: 'object-right', className: 'side-class' },
  44. { name: 'dummyStyle', title: 'baz', icon: 'object-dummy', className: 'dummy-class' },
  45. ]
  46. }
  47. } )
  48. .then( newEditor => {
  49. editor = newEditor;
  50. model = editor.model;
  51. document = model.document;
  52. viewDocument = editor.editing.view;
  53. } );
  54. } );
  55. it( 'should define image.styles config', () => {
  56. return VirtualTestEditor
  57. .create( {
  58. plugins: [ ImageStyleEditing ]
  59. } )
  60. .then( newEditor => {
  61. editor = newEditor;
  62. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleFull', 'imageStyleSide' ] );
  63. } );
  64. } );
  65. it( 'should set schema rules for image style', () => {
  66. const schema = model.schema;
  67. expect( schema.checkAttribute( [ '$root', 'image' ], 'imageStyle' ) ).to.be.true;
  68. } );
  69. it( 'should register separate command for each style', () => {
  70. expect( editor.commands.get( 'fullStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  71. expect( editor.commands.get( 'sideStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  72. expect( editor.commands.get( 'dummyStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  73. } );
  74. it( 'should convert from view to model', () => {
  75. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  76. expect( getModelData( model, { withoutSelection: true } ) )
  77. .to.equal( '<image imageStyle="sideStyle" src="foo.png"></image>' );
  78. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  79. '<figure class="ck-widget image side-class" contenteditable="false">' +
  80. '<img src="foo.png"></img>' +
  81. '</figure>' );
  82. } );
  83. it( 'should not convert from view to model if class is not defined', () => {
  84. editor.setData( '<figure class="image foo-bar"><img src="foo.png" /></figure>' );
  85. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  86. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  87. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  88. );
  89. } );
  90. it( 'should not convert from view to model when not in image figure', () => {
  91. editor.setData( '<figure class="side-class"></figure>' );
  92. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '' );
  93. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '' );
  94. } );
  95. it( 'should not convert from view to model if schema prevents it', () => {
  96. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  97. if ( ctx.endsWith( 'image' ) && attributeName == 'imageStyle' ) {
  98. return false;
  99. }
  100. } );
  101. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  102. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  103. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  104. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  105. );
  106. } );
  107. it( 'should convert model to view: adding attribute', () => {
  108. setModelData( model, '<image src="foo.png"></image>' );
  109. const image = document.getRoot().getChild( 0 );
  110. model.change( writer => {
  111. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  112. } );
  113. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  114. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  115. '<figure class="ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  116. );
  117. } );
  118. it( 'should convert model to view: removing attribute', () => {
  119. setModelData( model, '<image src="foo.png" imageStyle="sideStyle"></image>' );
  120. const image = document.getRoot().getChild( 0 );
  121. model.change( writer => {
  122. writer.setAttribute( 'imageStyle', null, image );
  123. } );
  124. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  125. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  126. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  127. );
  128. } );
  129. it( 'should convert model to view: change attribute', () => {
  130. setModelData( model, '<image src="foo.png" imageStyle="dummy"></image>' );
  131. const image = document.getRoot().getChild( 0 );
  132. model.change( writer => {
  133. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  134. } );
  135. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  136. // https://github.com/ckeditor/ckeditor5-image/issues/132
  137. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  138. '<figure class="ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  139. );
  140. model.change( writer => {
  141. writer.setAttribute( 'imageStyle', 'dummyStyle', image );
  142. } );
  143. expect( editor.getData() ).to.equal( '<figure class="image dummy-class"><img src="foo.png"></figure>' );
  144. // https://github.com/ckeditor/ckeditor5-image/issues/132
  145. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  146. '<figure class="ck-widget dummy-class image" contenteditable="false"><img src="foo.png"></img></figure>'
  147. );
  148. } );
  149. it( 'should not convert from model to view if already consumed: adding attribute', () => {
  150. editor.editing.downcastDispatcher.on( 'attribute:imageStyle', ( evt, data, conversionApi ) => {
  151. conversionApi.consumable.consume( data.item, 'attribute:imageStyle' );
  152. }, { priority: 'high' } );
  153. setModelData( model, '<image src="foo.png"></image>' );
  154. const image = document.getRoot().getChild( 0 );
  155. model.change( writer => {
  156. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  157. } );
  158. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  159. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  160. );
  161. } );
  162. it( 'should not set attribute if change was already consumed', () => {
  163. editor.editing.downcastDispatcher.on( 'attribute:imageStyle', ( evt, data, conversionApi ) => {
  164. conversionApi.consumable.consume( data.item, 'attribute:imageStyle' );
  165. }, { priority: 'high' } );
  166. setModelData( model, '<image src="foo.png" imageStyle="dummyStyle"></image>' );
  167. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  168. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  169. );
  170. } );
  171. it( 'should not convert from model to view if style is not present: adding attribute', () => {
  172. setModelData( model, '<image src="foo.png"></image>' );
  173. const image = document.getRoot().getChild( 0 );
  174. model.change( writer => {
  175. writer.setAttribute( 'imageStyle', 'foo', image );
  176. } );
  177. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  178. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  179. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  180. );
  181. } );
  182. it( 'should not convert from model to view if style is not present: change attribute', () => {
  183. setModelData( model, '<image src="foo.png" imageStyle="dummy"></image>' );
  184. const image = document.getRoot().getChild( 0 );
  185. model.change( writer => {
  186. writer.setAttribute( 'imageStyle', 'foo', image );
  187. } );
  188. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  189. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  190. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  191. );
  192. } );
  193. it( 'should not convert from model to view if style is not present: remove attribute', () => {
  194. setModelData( model, '<image src="foo.png" imageStyle="foo"></image>' );
  195. const image = document.getRoot().getChild( 0 );
  196. model.change( writer => {
  197. writer.setAttribute( 'imageStyle', null, image );
  198. } );
  199. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  200. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  201. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  202. );
  203. } );
  204. } );
  205. describe( 'config', () => {
  206. it( 'should fall back to defaults when no image.styles', () => {
  207. return VirtualTestEditor
  208. .create( {
  209. plugins: [ ImageStyleEditing ]
  210. } )
  211. .then( newEditor => {
  212. editor = newEditor;
  213. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleFull', 'imageStyleSide' ] );
  214. } );
  215. } );
  216. it( 'should not alter the image.styles config', () => {
  217. return VirtualTestEditor
  218. .create( {
  219. plugins: [ ImageStyleEditing ],
  220. image: {
  221. styles: [
  222. 'imageStyleSide'
  223. ]
  224. }
  225. } )
  226. .then( newEditor => {
  227. editor = newEditor;
  228. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleSide' ] );
  229. } );
  230. } );
  231. it( 'should not alter object definitions in the image.styles config', () => {
  232. return VirtualTestEditor
  233. .create( {
  234. plugins: [ ImageStyleEditing ],
  235. image: {
  236. styles: [
  237. { name: 'imageStyleSide' }
  238. ]
  239. }
  240. } )
  241. .then( newEditor => {
  242. editor = newEditor;
  243. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ { name: 'imageStyleSide' } ] );
  244. } );
  245. } );
  246. } );
  247. } );