imagestyleediting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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( [ 'full', 'side' ] );
  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 a command', () => {
  70. expect( editor.commands.get( 'imageStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  71. } );
  72. it( 'should convert from view to model', () => {
  73. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  74. expect( getModelData( model, { withoutSelection: true } ) )
  75. .to.equal( '<image imageStyle="sideStyle" src="foo.png"></image>' );
  76. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  77. '<figure class="ck ck-widget image side-class" contenteditable="false">' +
  78. '<img src="foo.png"></img>' +
  79. '</figure>' );
  80. } );
  81. it( 'should not convert from view to model if class is not defined', () => {
  82. editor.setData( '<figure class="image foo-bar"><img src="foo.png" /></figure>' );
  83. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  84. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  85. '<figure class="ck ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  86. );
  87. } );
  88. it( 'should not convert from view to model when not in image figure', () => {
  89. editor.setData( '<figure class="side-class"></figure>' );
  90. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '' );
  91. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '' );
  92. } );
  93. it( 'should not convert from view to model if schema prevents it', () => {
  94. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  95. if ( ctx.endsWith( 'image' ) && attributeName == 'imageStyle' ) {
  96. return false;
  97. }
  98. } );
  99. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  100. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  101. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  102. '<figure class="ck ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  103. );
  104. } );
  105. it( 'should convert model to view: adding attribute', () => {
  106. setModelData( model, '<image src="foo.png"></image>' );
  107. const image = document.getRoot().getChild( 0 );
  108. model.change( writer => {
  109. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  110. } );
  111. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  112. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  113. '<figure class="ck ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  114. );
  115. } );
  116. it( 'should convert model to view: removing attribute', () => {
  117. setModelData( model, '<image src="foo.png" imageStyle="sideStyle"></image>' );
  118. const image = document.getRoot().getChild( 0 );
  119. model.change( writer => {
  120. writer.setAttribute( 'imageStyle', null, image );
  121. } );
  122. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  123. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  124. '<figure class="ck ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  125. );
  126. } );
  127. it( 'should convert model to view: change attribute', () => {
  128. setModelData( model, '<image src="foo.png" imageStyle="dummy"></image>' );
  129. const image = document.getRoot().getChild( 0 );
  130. model.change( writer => {
  131. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  132. } );
  133. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  134. // https://github.com/ckeditor/ckeditor5-image/issues/132
  135. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  136. '<figure class="ck ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  137. );
  138. model.change( writer => {
  139. writer.setAttribute( 'imageStyle', 'dummyStyle', image );
  140. } );
  141. expect( editor.getData() ).to.equal( '<figure class="image dummy-class"><img src="foo.png"></figure>' );
  142. // https://github.com/ckeditor/ckeditor5-image/issues/132
  143. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  144. '<figure class="ck ck-widget dummy-class image" contenteditable="false"><img src="foo.png"></img></figure>'
  145. );
  146. } );
  147. it( 'should not convert from model to view if already consumed: adding attribute', () => {
  148. editor.editing.downcastDispatcher.on( 'attribute:imageStyle', ( evt, data, conversionApi ) => {
  149. conversionApi.consumable.consume( data.item, 'attribute:imageStyle' );
  150. }, { priority: 'high' } );
  151. setModelData( model, '<image src="foo.png"></image>' );
  152. const image = document.getRoot().getChild( 0 );
  153. model.change( writer => {
  154. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  155. } );
  156. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  157. '<figure class="ck ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  158. );
  159. } );
  160. it( 'should not set attribute if change was already consumed', () => {
  161. editor.editing.downcastDispatcher.on( 'attribute:imageStyle', ( evt, data, conversionApi ) => {
  162. conversionApi.consumable.consume( data.item, 'attribute:imageStyle' );
  163. }, { priority: 'high' } );
  164. setModelData( model, '<image src="foo.png" imageStyle="dummyStyle"></image>' );
  165. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  166. '<figure class="ck ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  167. );
  168. } );
  169. it( 'should not convert from model to view if style is not present: adding attribute', () => {
  170. setModelData( model, '<image src="foo.png"></image>' );
  171. const image = document.getRoot().getChild( 0 );
  172. model.change( writer => {
  173. writer.setAttribute( 'imageStyle', 'foo', image );
  174. } );
  175. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  176. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  177. '<figure class="ck ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  178. );
  179. } );
  180. it( 'should not convert from model to view if style is not present: change attribute', () => {
  181. setModelData( model, '<image src="foo.png" imageStyle="dummy"></image>' );
  182. const image = document.getRoot().getChild( 0 );
  183. model.change( writer => {
  184. writer.setAttribute( 'imageStyle', 'foo', image );
  185. } );
  186. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  187. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  188. '<figure class="ck ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  189. );
  190. } );
  191. it( 'should not convert from model to view if style is not present: remove attribute', () => {
  192. setModelData( model, '<image src="foo.png" imageStyle="foo"></image>' );
  193. const image = document.getRoot().getChild( 0 );
  194. model.change( writer => {
  195. writer.setAttribute( 'imageStyle', null, image );
  196. } );
  197. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  198. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  199. '<figure class="ck ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  200. );
  201. } );
  202. } );
  203. describe( 'config', () => {
  204. it( 'should fall back to defaults when no image.styles', () => {
  205. return VirtualTestEditor
  206. .create( {
  207. plugins: [ ImageStyleEditing ]
  208. } )
  209. .then( newEditor => {
  210. editor = newEditor;
  211. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'full', 'side' ] );
  212. } );
  213. } );
  214. it( 'should not alter the image.styles config', () => {
  215. return VirtualTestEditor
  216. .create( {
  217. plugins: [ ImageStyleEditing ],
  218. image: {
  219. styles: [
  220. 'side'
  221. ]
  222. }
  223. } )
  224. .then( newEditor => {
  225. editor = newEditor;
  226. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'side' ] );
  227. } );
  228. } );
  229. it( 'should not alter object definitions in the image.styles config', () => {
  230. return VirtualTestEditor
  231. .create( {
  232. plugins: [ ImageStyleEditing ],
  233. image: {
  234. styles: [
  235. { name: 'side' }
  236. ]
  237. }
  238. } )
  239. .then( newEditor => {
  240. editor = newEditor;
  241. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ { name: 'side' } ] );
  242. } );
  243. } );
  244. } );
  245. } );