imagestyleediting.js 11 KB

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