imageresizeediting.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 absoluteContainer, editor, editorElement;
  20. before( () => {
  21. // This container is required to position editor element in a reliable element.
  22. // @todo ensure whether it's required after migrating the tests.
  23. absoluteContainer = document.createElement( 'div' );
  24. absoluteContainer.style.top = '50px';
  25. absoluteContainer.style.left = '50px';
  26. absoluteContainer.style.height = '1000px';
  27. absoluteContainer.style.width = '500px';
  28. absoluteContainer.style.position = 'absolute';
  29. document.body.appendChild( absoluteContainer );
  30. } );
  31. after( () => {
  32. absoluteContainer.remove();
  33. } );
  34. afterEach( () => {
  35. if ( editorElement ) {
  36. editorElement.remove();
  37. }
  38. if ( editor ) {
  39. return editor.destroy();
  40. }
  41. } );
  42. it( 'should be named', () => {
  43. expect( ImageResizeEditing.pluginName ).to.equal( 'ImageResizeEditing' );
  44. } );
  45. describe( 'conversion', () => {
  46. beforeEach( () => createEditor() );
  47. it( 'upcasts 100px width correctly', () => {
  48. editor.setData( `<figure class="image" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  49. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '100px' );
  50. } );
  51. it( 'upcasts 50% width correctly', () => {
  52. editor.setData( `<figure class="image" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  53. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '50%' );
  54. } );
  55. it( 'downcasts 100px width correctly', () => {
  56. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="100px"></image>` );
  57. expect( editor.getData() )
  58. .to.equal( `<figure class="image image_resized" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  59. } );
  60. it( 'downcasts 50% width correctly', () => {
  61. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  62. expect( editor.getData() )
  63. .to.equal( `<figure class="image image_resized" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  64. } );
  65. it( 'removes style and extra class when no longer resized', () => {
  66. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  67. const imageModel = editor.model.document.getRoot().getChild( 0 );
  68. editor.model.change( writer => {
  69. writer.removeAttribute( 'width', imageModel );
  70. } );
  71. expect( editor.getData() )
  72. .to.equal( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  73. } );
  74. it( 'doesn\'t downcast consumed tokens', () => {
  75. editor.conversion.for( 'downcast' ).add( dispatcher =>
  76. dispatcher.on( 'attribute:width:image', ( evt, data, conversionApi ) => {
  77. conversionApi.consumable.consume( data.item, 'attribute:width:image' );
  78. }, { priority: 'high' } )
  79. );
  80. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  81. expect( editor.getData() )
  82. .to.equal( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  83. } );
  84. } );
  85. describe( 'schema', () => {
  86. beforeEach( () => createEditor() );
  87. it( 'allows the width attribute', () => {
  88. expect( editor.model.schema.checkAttribute( 'image', 'width' ) ).to.be.true;
  89. } );
  90. it( 'defines width as a formatting attribute', () => {
  91. expect( editor.model.schema.getAttributeProperties( 'width' ) ).to.have.property( 'isFormatting', true );
  92. } );
  93. } );
  94. describe( 'command', () => {
  95. beforeEach( () => createEditor() );
  96. it( 'defines the imageResize command', () => {
  97. expect( editor.commands.get( 'imageResize' ) ).to.be.instanceOf( ImageResizeCommand );
  98. } );
  99. } );
  100. function createEditor( config ) {
  101. editorElement = document.createElement( 'div' );
  102. absoluteContainer.appendChild( editorElement );
  103. return ClassicEditor
  104. .create( editorElement, config || {
  105. plugins: [ Paragraph, Image, ImageStyle, ImageResizeEditing ],
  106. image: {
  107. resizeUnit: 'px'
  108. }
  109. } )
  110. .then( newEditor => {
  111. editor = newEditor;
  112. focusEditor( editor );
  113. return editor;
  114. } );
  115. }
  116. } );