imageresizeediting.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /* global document */
  6. // ClassicTestEditor can't be used, as it doesn't handle the focus, which is needed to test resizer visual cues.
  7. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Image from '../../src/image';
  10. import ImageResizeEditing from '../../src/imageresize/imageresizeediting';
  11. import ImageResizeCommand from '../../src/imageresize/imageresizecommand';
  12. import ImageStyle from '../../src/imagestyle';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { focusEditor } from '@ckeditor/ckeditor5-widget/tests/widgetresize/_utils/utils';
  15. describe( 'ImageResizeEditing', () => {
  16. // 100x50 black png image
  17. const IMAGE_SRC_FIXTURE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAQAAAAAPLY1AAAAQklEQVR42u3PQREAAAgDoK1/' +
  18. 'aM3g14MGNJMXKiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiJysRFNMgH0RpujAAAAAElFTkSuQmCC';
  19. let editor, editorElement;
  20. beforeEach( () => {
  21. editorElement = document.createElement( 'div' );
  22. document.body.appendChild( editorElement );
  23. } );
  24. afterEach( async () => {
  25. if ( editorElement ) {
  26. editorElement.remove();
  27. }
  28. if ( editor ) {
  29. await editor.destroy();
  30. }
  31. } );
  32. it( 'should be named', () => {
  33. expect( ImageResizeEditing.pluginName ).to.equal( 'ImageResizeEditing' );
  34. } );
  35. describe( 'constructor()', () => {
  36. beforeEach( async () => {
  37. editor = await createEditor( {
  38. plugins: [ Paragraph, Image, ImageStyle, ImageResizeEditing ]
  39. } );
  40. } );
  41. it( 'should define the default value for config.image.resizeUnit', () => {
  42. expect( editor.config.get( 'image.resizeUnit' ) ).to.equal( '%' );
  43. } );
  44. it( 'should define the default value for config.image.resizeOptions', () => {
  45. expect( editor.config.get( 'image.resizeOptions' ) ).to.deep.equal( [ {
  46. name: 'imageResize:original',
  47. value: null,
  48. icon: 'original'
  49. },
  50. {
  51. name: 'imageResize:25',
  52. value: '25',
  53. icon: 'small'
  54. },
  55. {
  56. name: 'imageResize:50',
  57. value: '50',
  58. icon: 'medium'
  59. },
  60. {
  61. name: 'imageResize:75',
  62. value: '75',
  63. icon: 'large'
  64. } ] );
  65. } );
  66. } );
  67. describe( 'conversion', () => {
  68. beforeEach( async () => {
  69. editor = await createEditor();
  70. } );
  71. it( 'upcasts 100px width correctly', () => {
  72. editor.setData( `<figure class="image" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  73. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '100px' );
  74. } );
  75. it( 'upcasts 50% width correctly', () => {
  76. editor.setData( `<figure class="image" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  77. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '50%' );
  78. } );
  79. it( 'downcasts 100px width correctly', () => {
  80. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="100px"></image>` );
  81. expect( editor.getData() )
  82. .to.equal( `<figure class="image image_resized" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  83. } );
  84. it( 'downcasts 50% width correctly', () => {
  85. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  86. expect( editor.getData() )
  87. .to.equal( `<figure class="image image_resized" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  88. } );
  89. it( 'removes style and extra class when no longer resized', () => {
  90. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  91. const imageModel = editor.model.document.getRoot().getChild( 0 );
  92. editor.model.change( writer => {
  93. writer.removeAttribute( 'width', imageModel );
  94. } );
  95. expect( editor.getData() )
  96. .to.equal( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  97. } );
  98. it( 'doesn\'t downcast consumed tokens', () => {
  99. editor.conversion.for( 'downcast' ).add( dispatcher =>
  100. dispatcher.on( 'attribute:width:image', ( evt, data, conversionApi ) => {
  101. conversionApi.consumable.consume( data.item, 'attribute:width:image' );
  102. }, { priority: 'high' } )
  103. );
  104. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  105. expect( editor.getData() )
  106. .to.equal( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  107. } );
  108. } );
  109. describe( 'schema', () => {
  110. beforeEach( async () => {
  111. editor = await createEditor();
  112. } );
  113. it( 'allows the width attribute', () => {
  114. expect( editor.model.schema.checkAttribute( 'image', 'width' ) ).to.be.true;
  115. } );
  116. it( 'defines width as a formatting attribute', () => {
  117. expect( editor.model.schema.getAttributeProperties( 'width' ) ).to.have.property( 'isFormatting', true );
  118. } );
  119. } );
  120. describe( 'command', () => {
  121. beforeEach( async () => {
  122. editor = await createEditor();
  123. } );
  124. it( 'defines the imageResize command', () => {
  125. expect( editor.commands.get( 'imageResize' ) ).to.be.instanceOf( ImageResizeCommand );
  126. } );
  127. } );
  128. function createEditor( config ) {
  129. return ClassicEditor
  130. .create( editorElement, config || {
  131. plugins: [ Paragraph, Image, ImageStyle, ImageResizeEditing ],
  132. image: {
  133. resizeUnit: 'px'
  134. }
  135. } )
  136. .then( newEditor => {
  137. focusEditor( newEditor );
  138. return newEditor;
  139. } );
  140. }
  141. } );