imagestyleediting.js 11 KB

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