imagestyleengine.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
  6. import ImageStyleEngine from 'ckeditor5/image/imagestyle/imagestyleengine.js';
  7. import ImageEngine from 'ckeditor5/image/imageengine.js';
  8. import ImageStyleCommand from 'ckeditor5/image/imagestyle/imagestylecommand.js';
  9. import { getData as getModelData, setData as setModelData } from 'ckeditor5/engine/dev-utils/model.js';
  10. import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js';
  11. describe( 'ImageStyleEngine', () => {
  12. let editor, document, viewDocument;
  13. beforeEach( () => {
  14. return VirtualTestEditor.create( {
  15. plugins: [ ImageStyleEngine ],
  16. image: {
  17. styles: {
  18. imageStyleDummy: { title: 'Dummy style', icon: 'dummy-icon', value: 'dummy', className: 'image-style-dummy' }
  19. }
  20. }
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. document = editor.document;
  25. viewDocument = editor.editing.view;
  26. } );
  27. } );
  28. it( 'should be loaded', () => {
  29. expect( editor.plugins.get( ImageStyleEngine ) ).to.be.instanceOf( ImageStyleEngine );
  30. } );
  31. it( 'should load image engine', () => {
  32. expect( editor.plugins.get( ImageEngine ) ).to.be.instanceOf( ImageEngine );
  33. } );
  34. it( 'should set schema rules for image style', () => {
  35. const schema = document.schema;
  36. expect( schema.check( { name: 'image', attributes: [ 'imageStyle', 'src' ], inside: '$root' } ) ).to.be.true;
  37. } );
  38. it( 'should register command', () => {
  39. expect( editor.commands.has( 'imagestyle' ) ).to.be.true;
  40. const command = editor.commands.get( 'imagestyle' );
  41. expect( command ).to.be.instanceOf( ImageStyleCommand );
  42. } );
  43. it( 'should convert from view to model', () => {
  44. editor.setData( '<figure class="image image-style-side"><img src="foo.png" /></figure>' );
  45. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image imageStyle="side" src="foo.png"></image>' );
  46. } );
  47. it( 'should not convert from view to model if class is not defined', () => {
  48. editor.setData( '<figure class="image foo-bar"><img src="foo.png" /></figure>' );
  49. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  50. } );
  51. it( 'should not convert from view to model when not in image figure', () => {
  52. editor.setData( '<figure class="image-style-side"></figure>' );
  53. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '' );
  54. } );
  55. it( 'should not convert from view to model if schema prevents it', () => {
  56. document.schema.disallow( { name: 'image', attributes: 'imageStyle' } );
  57. editor.setData( '<figure class="image image-style-side"><img src="foo.png" /></figure>' );
  58. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  59. } );
  60. it( 'should convert model to view: adding attribute', () => {
  61. setModelData( document, '<image src="foo.png"></image>' );
  62. const image = document.getRoot().getChild( 0 );
  63. const batch = document.batch();
  64. document.enqueueChanges( () => {
  65. batch.setAttribute( image, 'imageStyle', 'side' );
  66. } );
  67. expect( editor.getData() ).to.equal( '<figure class="image image-style-side"><img src="foo.png"></figure>' );
  68. } );
  69. it( 'should convert model to view: removing attribute', () => {
  70. setModelData( document, '<image src="foo.png" imageStyle="side"></image>' );
  71. const image = document.getRoot().getChild( 0 );
  72. const batch = document.batch();
  73. document.enqueueChanges( () => {
  74. batch.setAttribute( image, 'imageStyle', null );
  75. } );
  76. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  77. } );
  78. it( 'should convert model to view: change attribute', () => {
  79. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  80. const image = document.getRoot().getChild( 0 );
  81. const batch = document.batch();
  82. document.enqueueChanges( () => {
  83. batch.setAttribute( image, 'imageStyle', 'side' );
  84. } );
  85. expect( editor.getData() ).to.equal( '<figure class="image image-style-side"><img src="foo.png"></figure>' );
  86. } );
  87. it( 'should not convert from model to view if already consumed: adding attribute', () => {
  88. editor.editing.modelToView.on( 'addAttribute:imageStyle', ( evt, data, consumable ) => {
  89. consumable.consume( data.item, 'addAttribute:imageStyle' );
  90. }, { priority: 'high' } );
  91. setModelData( document, '<image src="foo.png"></image>' );
  92. const image = document.getRoot().getChild( 0 );
  93. const batch = document.batch();
  94. document.enqueueChanges( () => {
  95. batch.setAttribute( image, 'imageStyle', 'side' );
  96. } );
  97. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  98. '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>'
  99. );
  100. } );
  101. it( 'should not convert from model to view if already consumed: removing attribute', () => {
  102. editor.editing.modelToView.on( 'removeAttribute:imageStyle', ( evt, data, consumable ) => {
  103. consumable.consume( data.item, 'removeAttribute:imageStyle' );
  104. }, { priority: 'high' } );
  105. setModelData( document, '<image src="foo.png" imageStyle="side"></image>' );
  106. const image = document.getRoot().getChild( 0 );
  107. const batch = document.batch();
  108. document.enqueueChanges( () => {
  109. batch.setAttribute( image, 'imageStyle', null );
  110. } );
  111. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  112. '<figure class="image ck-widget image-style-side" contenteditable="false"><img src="foo.png"></img></figure>'
  113. );
  114. } );
  115. it( 'should not convert from model to view if already consumed: change attribute', () => {
  116. editor.editing.modelToView.on( 'changeAttribute:imageStyle', ( evt, data, consumable ) => {
  117. consumable.consume( data.item, 'changeAttribute:imageStyle' );
  118. }, { priority: 'high' } );
  119. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  120. const image = document.getRoot().getChild( 0 );
  121. const batch = document.batch();
  122. document.enqueueChanges( () => {
  123. batch.setAttribute( image, 'imageStyle', 'side' );
  124. } );
  125. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  126. '<figure class="image ck-widget image-style-dummy" contenteditable="false"><img src="foo.png"></img></figure>'
  127. );
  128. } );
  129. it( 'should not convert from model to view if style is not present: adding attribute', () => {
  130. setModelData( document, '<image src="foo.png"></image>' );
  131. const image = document.getRoot().getChild( 0 );
  132. const batch = document.batch();
  133. document.enqueueChanges( () => {
  134. batch.setAttribute( image, 'imageStyle', 'foo' );
  135. } );
  136. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  137. } );
  138. it( 'should not convert from model to view if style is not present: change attribute', () => {
  139. setModelData( document, '<image src="foo.png" imageStyle="dummy"></image>' );
  140. const image = document.getRoot().getChild( 0 );
  141. const batch = document.batch();
  142. document.enqueueChanges( () => {
  143. batch.setAttribute( image, 'imageStyle', 'foo' );
  144. } );
  145. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  146. } );
  147. it( 'should not convert from model to view if style is not present: remove attribute', () => {
  148. setModelData( document, '<image src="foo.png" imageStyle="foo"></image>' );
  149. const image = document.getRoot().getChild( 0 );
  150. const batch = document.batch();
  151. document.enqueueChanges( () => {
  152. batch.setAttribute( image, 'imageStyle', null );
  153. } );
  154. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  155. } );
  156. } );