imagestyleediting.js 11 KB

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