imagestyleediting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. describe( 'ImageStyleEditing', () => {
  14. let editor, model, document, viewDocument;
  15. testUtils.createSinonSandbox( 'ImageStyleEditing' );
  16. beforeEach( () => {
  17. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  18. testUtils.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: [ ImageEditing, 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. } );
  37. describe( 'init', () => {
  38. beforeEach( () => {
  39. return VirtualTestEditor
  40. .create( {
  41. plugins: [ ImageEditing, ImageStyleEditing ],
  42. image: {
  43. styles: [
  44. { name: 'fullStyle', title: 'foo', icon: 'object-center', isDefault: true },
  45. { name: 'sideStyle', title: 'bar', icon: 'object-right', className: 'side-class' },
  46. { name: 'dummyStyle', title: 'baz', icon: 'object-dummy', className: 'dummy-class' },
  47. ]
  48. }
  49. } )
  50. .then( newEditor => {
  51. editor = newEditor;
  52. model = editor.model;
  53. document = model.document;
  54. viewDocument = editor.editing.view;
  55. } );
  56. } );
  57. it( 'should define image.styles config', () => {
  58. return VirtualTestEditor
  59. .create( {
  60. plugins: [ ImageEditing, ImageStyleEditing ]
  61. } )
  62. .then( newEditor => {
  63. editor = newEditor;
  64. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'full', 'side' ] );
  65. } );
  66. } );
  67. it( 'should set schema rules for image style', () => {
  68. const schema = model.schema;
  69. expect( schema.checkAttribute( [ '$root', 'image' ], 'imageStyle' ) ).to.be.true;
  70. } );
  71. it( 'should register a command', () => {
  72. expect( editor.commands.get( 'imageStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  73. } );
  74. it( 'should convert from view to model', () => {
  75. editor.setData( '<figure class="image side-class"><img src="/assets/sample.png" /></figure>' );
  76. expect( getModelData( model, { withoutSelection: true } ) )
  77. .to.equal( '<image imageStyle="sideStyle" src="/assets/sample.png"></image>' );
  78. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  79. '<figure class="ck-widget image side-class" contenteditable="false">' +
  80. '<img src="/assets/sample.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="/assets/sample.png" /></figure>' );
  85. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<image src="/assets/sample.png"></image>' );
  86. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  87. '<figure class="ck-widget image" contenteditable="false"><img src="/assets/sample.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="/assets/sample.png" /></figure>' );
  102. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<image src="/assets/sample.png"></image>' );
  103. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  104. '<figure class="ck-widget image" contenteditable="false"><img src="/assets/sample.png"></img></figure>'
  105. );
  106. } );
  107. it( 'should convert model to view: adding attribute', () => {
  108. setModelData( model, '<image src="/assets/sample.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="/assets/sample.png"></figure>' );
  114. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  115. '<figure class="ck-widget image side-class" contenteditable="false"><img src="/assets/sample.png"></img></figure>'
  116. );
  117. } );
  118. it( 'should convert model to view: removing attribute', () => {
  119. setModelData( model, '<image src="/assets/sample.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="/assets/sample.png"></figure>' );
  125. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  126. '<figure class="ck-widget image" contenteditable="false"><img src="/assets/sample.png"></img></figure>'
  127. );
  128. } );
  129. it( 'should convert model to view: change attribute', () => {
  130. setModelData( model, '<image src="/assets/sample.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="/assets/sample.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="/assets/sample.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="/assets/sample.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="/assets/sample.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="/assets/sample.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="/assets/sample.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="/assets/sample.png" imageStyle="dummyStyle"></image>' );
  167. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  168. '<figure class="ck-widget image" contenteditable="false"><img src="/assets/sample.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="/assets/sample.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="/assets/sample.png"></figure>' );
  178. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  179. '<figure class="ck-widget image" contenteditable="false"><img src="/assets/sample.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="/assets/sample.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="/assets/sample.png"></figure>' );
  189. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  190. '<figure class="ck-widget image" contenteditable="false"><img src="/assets/sample.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="/assets/sample.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="/assets/sample.png"></figure>' );
  200. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  201. '<figure class="ck-widget image" contenteditable="false"><img src="/assets/sample.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: [ ImageEditing, ImageStyleEditing ]
  210. } )
  211. .then( newEditor => {
  212. editor = newEditor;
  213. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'full', 'side' ] );
  214. } );
  215. } );
  216. it( 'should not alter the image.styles config', () => {
  217. return VirtualTestEditor
  218. .create( {
  219. plugins: [ ImageEditing, ImageStyleEditing ],
  220. image: {
  221. styles: [
  222. 'side'
  223. ]
  224. }
  225. } )
  226. .then( newEditor => {
  227. editor = newEditor;
  228. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'side' ] );
  229. } );
  230. } );
  231. it( 'should not alter object definitions in the image.styles config', () => {
  232. return VirtualTestEditor
  233. .create( {
  234. plugins: [ ImageEditing, ImageStyleEditing ],
  235. image: {
  236. styles: [
  237. { name: 'side' }
  238. ]
  239. }
  240. } )
  241. .then( newEditor => {
  242. editor = newEditor;
  243. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ { name: 'side' } ] );
  244. } );
  245. } );
  246. } );
  247. } );