imageresizeediting.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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( 'conversion', () => {
  36. beforeEach( async () => {
  37. editor = await createEditor();
  38. } );
  39. it( 'upcasts 100px width correctly', () => {
  40. editor.setData( `<figure class="image" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  41. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '100px' );
  42. } );
  43. it( 'upcasts 50% width correctly', () => {
  44. editor.setData( `<figure class="image" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  45. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '50%' );
  46. } );
  47. it( 'downcasts 100px width correctly', () => {
  48. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="100px"></image>` );
  49. expect( editor.getData() )
  50. .to.equal( `<figure class="image image_resized" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  51. } );
  52. it( 'downcasts 50% width correctly', () => {
  53. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  54. expect( editor.getData() )
  55. .to.equal( `<figure class="image image_resized" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  56. } );
  57. it( 'removes style and extra class when no longer resized', () => {
  58. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  59. const imageModel = editor.model.document.getRoot().getChild( 0 );
  60. editor.model.change( writer => {
  61. writer.removeAttribute( 'width', imageModel );
  62. } );
  63. expect( editor.getData() )
  64. .to.equal( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  65. } );
  66. it( 'doesn\'t downcast consumed tokens', () => {
  67. editor.conversion.for( 'downcast' ).add( dispatcher =>
  68. dispatcher.on( 'attribute:width:image', ( evt, data, conversionApi ) => {
  69. conversionApi.consumable.consume( data.item, 'attribute:width:image' );
  70. }, { priority: 'high' } )
  71. );
  72. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  73. expect( editor.getData() )
  74. .to.equal( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  75. } );
  76. } );
  77. describe( 'schema', () => {
  78. beforeEach( async () => {
  79. editor = await createEditor();
  80. } );
  81. it( 'allows the width attribute', () => {
  82. expect( editor.model.schema.checkAttribute( 'image', 'width' ) ).to.be.true;
  83. } );
  84. it( 'defines width as a formatting attribute', () => {
  85. expect( editor.model.schema.getAttributeProperties( 'width' ) ).to.have.property( 'isFormatting', true );
  86. } );
  87. } );
  88. describe( 'command', () => {
  89. beforeEach( async () => {
  90. editor = await createEditor();
  91. } );
  92. it( 'defines the imageResize command', () => {
  93. expect( editor.commands.get( 'imageResize' ) ).to.be.instanceOf( ImageResizeCommand );
  94. } );
  95. } );
  96. function createEditor( config ) {
  97. return ClassicEditor
  98. .create( editorElement, config || {
  99. plugins: [ Paragraph, Image, ImageStyle, ImageResizeEditing ],
  100. image: {
  101. resizeUnit: 'px'
  102. }
  103. } )
  104. .then( newEditor => {
  105. focusEditor( newEditor );
  106. return newEditor;
  107. } );
  108. }
  109. } );