image.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import Image from '../src/image';
  7. import ImageEngine from '../src/image/imageengine';
  8. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  9. import ImageTextAlternative from '../src/imagetextalternative';
  10. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  12. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  13. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  14. describe( 'Image', () => {
  15. let editorElement, model, editor, document, viewDocument;
  16. beforeEach( () => {
  17. editorElement = global.document.createElement( 'div' );
  18. global.document.body.appendChild( editorElement );
  19. return ClassicTestEditor
  20. .create( editorElement, {
  21. plugins: [ Image ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. document = model.document;
  27. viewDocument = editor.editing.view;
  28. } );
  29. } );
  30. afterEach( () => {
  31. editorElement.remove();
  32. return editor.destroy();
  33. } );
  34. it( 'should be loaded', () => {
  35. expect( editor.plugins.get( Image ) ).to.instanceOf( Image );
  36. } );
  37. it( 'should load ImageEngine plugin', () => {
  38. expect( editor.plugins.get( ImageEngine ) ).to.instanceOf( ImageEngine );
  39. } );
  40. it( 'should load Widget plugin', () => {
  41. expect( editor.plugins.get( Widget ) ).to.instanceOf( Widget );
  42. } );
  43. it( 'should load ImageTextAlternative plugin', () => {
  44. expect( editor.plugins.get( ImageTextAlternative ) ).to.instanceOf( ImageTextAlternative );
  45. } );
  46. describe( 'selection', () => {
  47. it( 'should create fake selection', () => {
  48. setModelData( model, '[<image alt="alt text" src="foo.png"></image>]' );
  49. expect( getViewData( viewDocument ) ).to.equal(
  50. '[<figure class="ck-widget ck-widget_selected image" contenteditable="false">' +
  51. '<img alt="alt text" src="foo.png"></img>' +
  52. '</figure>]'
  53. );
  54. expect( viewDocument.selection.isFake ).to.be.true;
  55. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'alt text image widget' );
  56. } );
  57. it( 'should create proper fake selection label when alt attribute is empty', () => {
  58. setModelData( model, '[<image src="foo.png" alt=""></image>]' );
  59. expect( getViewData( viewDocument ) ).to.equal(
  60. '[<figure class="ck-widget ck-widget_selected image" contenteditable="false">' +
  61. '<img alt="" src="foo.png"></img>' +
  62. '</figure>]'
  63. );
  64. expect( viewDocument.selection.isFake ).to.be.true;
  65. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  66. } );
  67. it( 'should remove selected class from previously selected element', () => {
  68. setModelData( model,
  69. '[<image src="foo.png" alt="alt text"></image>]' +
  70. '<image src="foo.png" alt="alt text"></image>'
  71. );
  72. expect( getViewData( viewDocument ) ).to.equal(
  73. '[<figure class="ck-widget ck-widget_selected image" contenteditable="false">' +
  74. '<img alt="alt text" src="foo.png"></img>' +
  75. '</figure>]' +
  76. '<figure class="ck-widget image" contenteditable="false">' +
  77. '<img alt="alt text" src="foo.png"></img>' +
  78. '</figure>'
  79. );
  80. model.change( writer => {
  81. const secondImage = document.getRoot().getChild( 1 );
  82. writer.setSelection( ModelRange.createOn( secondImage ) );
  83. } );
  84. expect( getViewData( viewDocument ) ).to.equal(
  85. '<figure class="ck-widget image" contenteditable="false">' +
  86. '<img alt="alt text" src="foo.png"></img>' +
  87. '</figure>' +
  88. '[<figure class="ck-widget ck-widget_selected image" contenteditable="false">' +
  89. '<img alt="alt text" src="foo.png"></img>' +
  90. '</figure>]'
  91. );
  92. } );
  93. } );
  94. } );